Select to view content in your preferred language

Tool Validator / Values List Help

1474
8
Jump to solution
05-13-2013 09:52 AM
AllenSmith
Deactivated User
All,

I am trying to populate a Values List in a tool from selected features.  I have 2 feature classes (Counties and Places).  I am trying to narrow down the Placees that will populate the Values List.  I would like the user to specify the County and then the Values List would be populated by selecting the Places inside the County(SelectByLocation).  I am thinking that the Tool Validator will be used but not sure if/how.  Would it be possible to select the County by mouse click?  Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
For performance reasons, it would make more sense to tag the places with the County beforehand, you don't want to do significant geoprocessing while a user is staring at the dialog box -- a delay of even a second is a long time in that situation.

Here's an example of how this would work. The code below assumes:

Parameter 0 is the county polygon with fields CNAME and CCODE
Parameter 1 is the place points with fields PNAME and CCODE
Parameter 2 is the county name (picklist)
Parameter 3 is the place name (picklist)

This code is untested! For guidance, see the help article "Programming a Tool Validator Class".

def updateParameters():   if self.params[0].value:     # get dict of county names and codes     counties = {}     rows = arcpy.SearchCursor(self.params[0].value,"","","CNAME;CCODE")     for row in rows:       nm = row.getValue("CNAME")       cd = row.getValue("CCODE")           counties[nm] = cd     del row, rows     # set up picklist (filter) of county names     cnames = counties.keys()     self.param[2].filter.list = sorted(cnames)   # set up picklist of place names for this county   if self.params[1].value and self.params[2].value:     county_code = counties[self.params[2].value]     where = "CCODE = {0}".format(county_code)     rows = arcpy.SearchCursor(self.params[1].value,"",where,"PNAME;CCODE")     pnames = []     for row in rows:       pnames.append(row.getValue("PNAME"))     del row, rows     pnames = sorted(set(pnames)) # uniqueize and sort     self.params[3].filter.list = pnames

View solution in original post

0 Kudos
8 Replies
curtvprice
MVP Alum
For performance reasons, it would make more sense to tag the places with the County beforehand, you don't want to do significant geoprocessing while a user is staring at the dialog box -- a delay of even a second is a long time in that situation.

Here's an example of how this would work. The code below assumes:

Parameter 0 is the county polygon with fields CNAME and CCODE
Parameter 1 is the place points with fields PNAME and CCODE
Parameter 2 is the county name (picklist)
Parameter 3 is the place name (picklist)

This code is untested! For guidance, see the help article "Programming a Tool Validator Class".

def updateParameters():   if self.params[0].value:     # get dict of county names and codes     counties = {}     rows = arcpy.SearchCursor(self.params[0].value,"","","CNAME;CCODE")     for row in rows:       nm = row.getValue("CNAME")       cd = row.getValue("CCODE")           counties[nm] = cd     del row, rows     # set up picklist (filter) of county names     cnames = counties.keys()     self.param[2].filter.list = sorted(cnames)   # set up picklist of place names for this county   if self.params[1].value and self.params[2].value:     county_code = counties[self.params[2].value]     where = "CCODE = {0}".format(county_code)     rows = arcpy.SearchCursor(self.params[1].value,"",where,"PNAME;CCODE")     pnames = []     for row in rows:       pnames.append(row.getValue("PNAME"))     del row, rows     pnames = sorted(set(pnames)) # uniqueize and sort     self.params[3].filter.list = pnames
0 Kudos
AllenSmith
Deactivated User
Curtis,

Thanks for the help.  Could you clarify where and how this code gets applied?  When I add the script into the toolbox do I set my parameters here at the Add Script dialog box?  I am a little confused with the Tool Validator stuff.  Thanks again.
0 Kudos
curtvprice
MVP Alum
  Could you clarify where and how [validation] code gets applied?


No, tool validation before your main script runs (that's the idea!) - the code is edited in the script tool properties/validation tab.

If you have made the jump to Python toolboxes, the tool validation class is in a separate area of your code. (I still like doing it the old way -- less work!)
0 Kudos
AllenSmith
Deactivated User
Ok, I just figured that out!  (thanks for the help).  I get a runtime error saying that the update Parameters() takes no arguments (1 given).  In my script that this will be used for, I am updating a tbale using sys.argv statements.  I am used to putting these in order in the parameters section of the Add Script  dialog box.  I entered the first 4 sys.argv statements as you suggested (parameters 0-3) and I think herein lies my problem.  Any other suggestions?  Thanks for the prompt responses.
0 Kudos
JoelCalhoun
Deactivated User
I'm not sure what your code is but sys.argv starts at 1 whereas getparameter starts at 0.
So when you are assigning the input parameters your first sys.argv should be 1 and so on.

Please post your code if this is not your issue.


Joel
0 Kudos
curtvprice
MVP Alum
It's best practice to use arcpy.GetParameterAsText() instead of sys.argv[]. The main reason for this is that objects are passed more efficiently and robustly (especially if the string representation is large or complicated).

GetParameter() can also be used, but in most cases it's a lot easier to handle and debug a string representation than get the object directly.

None of these options can be used in validation code -- there you must access the parameters as members of the self.params list.
0 Kudos
AllenSmith
Deactivated User
I've been in the field for the last couple of days so a big thanks to those who responded.  Hopefully you will still be able to help.  I am using sys.argv statements to allow the user to populate the fields in the table.  One of those fields will be populated by a dropdown list limited (hopefully by Tool Validator) to a subset of cities that lie within a selected county.  The County can either be user designated (sysargv) or preferably selected via mouse.  The code I am using is below:


#Import modules
import os
import sys
import arcpy

#Set Map Document
mxd = arcpy.mapping.MapDocument("Current")

#Set Overwrite Option
arcpy.env.overwriteOutput = True




#Sets parameters (attributes)
County = sys.argv[1]
Place = sys.argv[2]
CountyName = sys.argv[3]
PlaceName = sys.argv[4]
Office = sys.argv[5]
Forester = sys.srgv[6]
Activity = sys.argv[7]
Persons = sys.argv[8]
Underserverd = sys.argv[9]
NameLast = sys.argv[10]
NameFirst = sys.argv[11]


#Create a new row and fill in fields
rows = arcpy.InsertCursor("CommunityLevelActivity")
row = rows.newRow()
row.TFSOffice = Office
row.TFSForester = Forester
row.Activity = Activity
row.RecipientLast = NameLast
row.RecipientFirst = NameFirst
row.Persons = Persons
row.Underserved = Underserved
row.Place = PlaceName

rows.insertRow(row)
del row
del rows

#Calculate Date Field
expression = datetime.datetime.now()
arcpy.CalculateField_management("CommunityLevelActivity", "DateComplete", expression, "PYTHON_9.3")

# Refresh map to show all changes
arcpy.RefreshActiveView()
mxd.save()

# Refresh map to show all changes
arcpy.RefreshActiveView()
mxd.save()

del mxd, 


One of the fields in the table is "Place" and this is the one I would like (think I need to use the Tool validator on) to populate using places (PlacesFC -a point FC) that are within a selected county Fc (CountyFC -a polygon FC).  I am not sure how to include Curt's code into the Tool Validator.

Thanks

Thanks
0 Kudos
curtvprice
MVP Alum
  I am not sure how to include Curt's code into the Tool Validator.


This is complex enough you really need to read the online help before you go any further:
Desktop 10.1 Help: Customizing script tool behavior

The code I gave you is designed to be used in the code box you see when you right click on a script tool  and select  Properties and go to the Validation tab.
0 Kudos