Programmically Populate Value List in ArcGIS Tool Interface

2027
3
Jump to solution
03-05-2013 12:30 PM
MikeMacRae
Occasional Contributor III
I'm just trying to set up the parameters in a tool I've built in ArcGIS. What I want to do is read in a feature class or table and get the values in a specific field called "SITE". The field contains a bunch of site names. What I want to do is read in the values of that field and populate them into the 'Value List' tool parameter property

I've been looking at the tool properties and I though I can use the 'Obtained From' property, but it looks like I can only obtain field names, but not that values in the field. Is there anyway else to do this? Is there anyways to read in the values from the SITE field and populate it to a value list programmically?

Thanks, Mike
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RaphaelR
Occasional Contributor II
Hi Mike,

you´ll have to use the ToolValidator Class to do this:
for a sample tool with 3 parameters where you can first select a Layer then select the field and get the chosen field´s values:
FC (datatype: Layer)
Field (datatype: Field)
Value (datatype: String)

on the validation tab change the validator class code:
class ToolValidator:   """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."""     import arcpy     self.params = arcpy.GetParameterInfo()    def initializeParameters(self):     """Refine the properties of a tool's parameters.  This method is     called when the tool is opened."""          self.params[1].parameterDependencies = [0]   # set the parameter dependency for Field to the FC           return    def updateParameters(self):     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""      import arcpy     FC = self.params[0].value     fldName = self.params[1].value     vList = []     #use a cursor to get values of selected field into vList     rows = arcpy.SearchCursor(FC)     for row in rows:       fldVal = str(row.getValue(fldName))       vList.append(fldVal)      self.params[2].filter.list = vList  #fill the Values Parameter with the selected field´s values            return    def updateMessages(self):     """Modify the messages created by internal validation for each tool     parameter.  This method is called after internal validation."""     return

View solution in original post

3 Replies
RaphaelR
Occasional Contributor II
Hi Mike,

you´ll have to use the ToolValidator Class to do this:
for a sample tool with 3 parameters where you can first select a Layer then select the field and get the chosen field´s values:
FC (datatype: Layer)
Field (datatype: Field)
Value (datatype: String)

on the validation tab change the validator class code:
class ToolValidator:   """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."""     import arcpy     self.params = arcpy.GetParameterInfo()    def initializeParameters(self):     """Refine the properties of a tool's parameters.  This method is     called when the tool is opened."""          self.params[1].parameterDependencies = [0]   # set the parameter dependency for Field to the FC           return    def updateParameters(self):     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""      import arcpy     FC = self.params[0].value     fldName = self.params[1].value     vList = []     #use a cursor to get values of selected field into vList     rows = arcpy.SearchCursor(FC)     for row in rows:       fldVal = str(row.getValue(fldName))       vList.append(fldVal)      self.params[2].filter.list = vList  #fill the Values Parameter with the selected field´s values            return    def updateMessages(self):     """Modify the messages created by internal validation for each tool     parameter.  This method is called after internal validation."""     return
MarcusBrown
New Contributor II

Thanks very much. This is exactly what I needed to accomplish and I learned about validation in the processes.

0 Kudos
MikeMacRae
Occasional Contributor III
Thanks Rapheal. I haven't played with the validator before. This is exactly what I was looking for. Thanks for taking the time.

Mike
0 Kudos