I currently have a python script tool that reads in a feature class and (among other processes) subsets the feature class based on a value in one field, 'REGION'. Instead of having all subsets created, I'm now seeking to be able to run the tool for certain regions and am trying to figure out how to have a parameter in the tool give a drop down list of the unique values within the REGION field. I've gotten up to the point where I can select the region (which I don't even need as a parameter since it will never change and always be based off of REGION), but I'm stumped on how to have a parameter of a drop down of a list of unique values in REGION.
Here is a the code I'm trying to use. I've been looking at http://desktop.arcgis.com/en/arcmap/10.5/analyze/creating-tools/customizing-script-tool-behavior.htm but don't quite understand how to have the set of REGION values appear in the python tool GUI.
import arcpy
from arcpy.sa import *
featureClass = arcpy.GetParameterAsText(0)
class ToolValidator(object):
def _int_(self):
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
return
def updateParameters(self):
rows = arcpy.da.SearchCursor(featureClass, REGION)
self.params[0].filter.list = sorted(list(set(r.getValue('REGION') for r in rows)))
del rows
return
def updateMessages(self):
return
def new_feature_class():
# 1. For each feature class selected create a new feature class for every unique region within it.
for j in featureClasses.split(';'):
def unique_values(table, field):
with arcpy.da.SearchCursor(table, field) as cursor:
return sorted({row[0] for row in cursor})
uniqueRegionList = unique_values(j, 'REGION')
print (uniqueRegionList)
for i in uniqueRegionList:
in_features = j
out_feature_class = str(i) + str(timePeriod) + in_features[-5:]
where_clause = " \"REGION\" = '%s'" % (i)
arcpy.Select_analysis(in_features, out_feature_class, where_clause)
print(out_feature_class)
new_feature_class()
Edited to add Validation Code:
import arcpy
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""
def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
return
def updateParameters(self):
rows = arcpy.da.SearchCursor(featureClass, REGION)
self.params[1].filter.list = sorted(list(set(r.getValue('REGION') for r in rows)))
del rows
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
-
You are looking at ArcGIS Pro documentation but are you using ArcGIS Pro or ArcGIS Desktop 10.x?PolyGeo– PolyGeo ♦2018年05月25日 23:41:34 +00:00Commented May 25, 2018 at 23:41
1 Answer 1
At the moment your ToolValidator code is trying to apply a filter list to your first parameter (featureClasses).
You need to add a third parameter of type string (e.g. Region Value), then in your ToolValidator change this
self.params[0].filter.list = sorted(list(set(r.getValue('REGION') for r in rows)))
to
self.params[2].filter.list = sorted(list(set(r.getValue('REGION') for r in rows)))
You may also want to pull the bit of code that creates the unique list of REGION values out of the def updateParameters(self):
block otherwise it will fire every time any of the parameters is altered which could cause performance issues.
-
Thanks Dan, I still can't get it to work. In the script have: featureClass = arcpy.GetParameterAsText(0) regionValue = arcpy.GetParameterAsText(1) and self.params[2].filter.list = sorted(list(set(r.getValue('REGION') for r in rows))) In the tool properties I have two parameters listed: feature class (data type: feature class) and regionValue (data type: String), but no drop down appears in the GUI for regionValue. If I set regionValue to filter: Value List a drop down arrow appears, but options are blank.ENIAC-6– ENIAC-62018年05月29日 17:26:14 +00:00Commented May 29, 2018 at 17:26
-
The
self.params
is calling a list of your parameters (indexed from zero), so if you only have two parameters and you want to assign the value list to the second one you would need to useself.params[1].filter.list = ...
.Dan– Dan2018年05月30日 01:03:29 +00:00Commented May 30, 2018 at 1:03 -
Thanks much. I had also tried self.params[1].filter.list and couldn't get that to run either...ENIAC-6– ENIAC-62018年05月30日 23:59:32 +00:00Commented May 30, 2018 at 23:59
-
Do you have your ToolValidation code in the main part of your script or under the Validation tab in the GUI? It needs to be in the GUI.Dan– Dan2018年05月31日 00:37:30 +00:00Commented May 31, 2018 at 0:37
-
Screenshot the Validation tab in the GUI so I can see what code you have there.Dan– Dan2018年05月31日 00:38:58 +00:00Commented May 31, 2018 at 0:38
Explore related questions
See similar questions with these tags.