Select to view content in your preferred language

Include an Extent in Choose Field Value Python Script

341
0
10-05-2023 01:14 AM
AnsgarG
Emerging Contributor

Hello, 

I'm trying to select features based on the code from the ArcGIS Blog Generating a choice list from a field.

But in my Script i use two fields to select the features. The script works, but needs a long time to handle big datasets, so i wanted to add an extent to delimit my input feature class. I tried to do it with arcpy.env.extent but this isnt working. 

Since this all needs to happen in advance, the validation code needs to be adjusted:

import arcpy
class ToolValidator:
  # Class to add custom behavior and properties to the tool and tool parameters.

    def __init__(self):
        # set self.params for use in other function
        self.params = arcpy.GetParameterInfo()
        self.spatial_extent = None

    def initializeParameters(self):
        # Customize parameter properties.
        # This gets called when the tool is opened.
        return

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before
        # standard validation.
        
        fc_param = self.params[0]
        extent_param = self.params[1]
        field1_param = self.params[2]
        value1_param = self.params[3]
        field2_param = self.params[4]
        value2_param = self.params[5]
        
        
        if extent_param.value:
            self.spatial_extent = extent_param.value
        else:
            self.spatial_extent = None

        if fc_param.value and field1_param.value and value1_param.value and field2_param.value:
            fc, col2 = str(fc_param.value), str(field2_param.value)
            wc = f"{field1_param.value} = '{str(value1_param.value)}'" if value1_param.value is not None else None
            if wc is not None:
                # Apply spatial extent filter if it's defined
                if self.spatial_extent:
                    arcpy.env.extent = self.spatial_extent
                value2_param.filter.list = [str(val) for val in sorted(
                    set(row.getValue(col2) for row in arcpy.SearchCursor(fc, fields=col2, where_clause=wc) if
                        row.getValue(col2) is not None))]
            else:
                value2_param.filter.list = []
            if value2_param.value not in value2_param.filter.list:
                value2_param.value = value2_param.filter.list[0]

        elif fc_param.value and field1_param.value:
            fc, col1 = str(fc_param.value), str(field1_param.value)
            # Apply spatial extent filter if it's defined
            if self.spatial_extent:
                arcpy.env.extent = self.spatial_extent
            value1_param.filter.list = [str(val) for val in sorted(
                set(row.getValue(col1) for row in arcpy.SearchCursor(fc, fields=col1) if row.getValue(col1) is not None))]
            if value1_param.value not in value1_param.filter.list:
                value1_param.value = value1_param.filter.list[0]

        return

How could i solve this? 
Thanks in advance.

 

0 Kudos
0 Replies