<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Include an Extent in Choose Field Value Python Script in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/include-an-extent-in-choose-field-value-python/m-p/1335272#M73712</link>
    <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to select features based on the code from the ArcGIS Blog&amp;nbsp;&lt;A href="https://www.esri.com/arcgis-blog/products/arcgis-desktop/analytics/generating-a-choice-list-from-a-field/?rmedium=blogs_esri_com&amp;amp;rsource=/esri/arcgis/2011/08/25/generating-a-choice-list-from-a-field/" target="_self"&gt;Generating a choice list from a field.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since this all needs to happen in advance, the validation code needs to be adjusted:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;How could i solve this?&amp;nbsp;&lt;BR /&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Oct 2023 08:14:00 GMT</pubDate>
    <dc:creator>AnsgarG</dc:creator>
    <dc:date>2023-10-05T08:14:00Z</dc:date>
    <item>
      <title>Include an Extent in Choose Field Value Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/include-an-extent-in-choose-field-value-python/m-p/1335272#M73712</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to select features based on the code from the ArcGIS Blog&amp;nbsp;&lt;A href="https://www.esri.com/arcgis-blog/products/arcgis-desktop/analytics/generating-a-choice-list-from-a-field/?rmedium=blogs_esri_com&amp;amp;rsource=/esri/arcgis/2011/08/25/generating-a-choice-list-from-a-field/" target="_self"&gt;Generating a choice list from a field.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since this all needs to happen in advance, the validation code needs to be adjusted:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;How could i solve this?&amp;nbsp;&lt;BR /&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 08:14:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/include-an-extent-in-choose-field-value-python/m-p/1335272#M73712</guid>
      <dc:creator>AnsgarG</dc:creator>
      <dc:date>2023-10-05T08:14:00Z</dc:date>
    </item>
  </channel>
</rss>

