Monday, 14 October 2019

arcpy - Limiting feature class parameter to specific geodatabase?


I have a tool that will update the geometry of a feature class based on the geometry of another, using a common identifier.


It works great but the way I have it set up, you have to first select the gdb holding the two feature classes, then select each feature class.



Is there a way, once the gdb (which defines the workspace) is selected, to then limit the options for the two feature class parameters to those found in the selected gdb?


When I run it, I know they have to be in the same gdb, but I'd like to share it with colleagues as a tool and I'd like it to be at least a little fool-proof, considering it was fool-made.


Here's the code:


import arcpy

arcpy.env.workspace = arcpy.GetParameterAsText(0)
UpdateData = arcpy.GetParameterAsText(1)
UpdateField = arcpy.GetParameterAsText(2)
ProjectData = arcpy.GetParameterAsText(3)
ProjectField = arcpy.GetParameterAsText(4)


geometries = {key:value for (key,value) in arcpy.da.SearchCursor(UpdateData, [UpdateField, 'SHAPE@'])}

notfound = []

with arcpy.da.UpdateCursor(ProjectData, [ProjectField, 'SHAPE@']) as cursor:
for row in cursor:
try:
row[1] = geometries[row[0]]
cursor.updateRow(row)

except:
notfound.append(row[0])

GUI Screenshot



Answer



First, I would create a list of the feature classes in the selected gdb and insert it in a dropdown menu within your toolbox.


To create the list of featureclasses you can use arcpy.ListFeatureClasses to create the list (check the link for options like wildcard or FC type if you want to limit your results).


Then, if you want a parameter to be able to handle a list of values rather than just one value, set the MultiValue property to Yes.


enter image description here


Then, as @wfgeo suggested, to inser a dropdown in your toolbox, go in the Validation tab in the properties of your script from ArcCatalog and change the def updateParameters(self): to something like (not tested, surely tou need to adjust it a little):



def updateParameters(self):
# If database has been set
if self.params[0]:
# set list with values to choose from for UpdateData variable
self.params[1].filter.list = # your list created with ListFeatureClasses goes here
# set list with values to choose from for ProjectData variable
self.params[3].filter.list = # your list created with ListFeatureClasses goes here

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...