1

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

  • 1
    It's been a very long time since I worked on an arcpy tool so I cannot provide a specific answer, but you may want to try using a tool validation script, which can execute blocks of Python code when the user changes based on user input: (pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/…) I think this code needs to go INTO a .tbx file, which means you can't see or edit it unless you open it up in ArcMap itself. A working example of this is in a toolbox I made here: github.com/iboates/raster_stamp, so maybe you can explore it. – wfgeo Feb 24 at 13:09
0

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
  • Awesome, I'll give that a shot. I'm pretty new to building tools like this, so I'll have to do some playing around. I'd imagine I can read the FC type of the first feature class and use that as a filter when they select the second, to make sure they are the same? – Dante Feb 25 at 14:38
  • Of course. You can do that with the Describe function on the FC like desc = arcpy.Describe("your/fc/here") desc.shapeType # return e.g. "Polygon" (String) and build another list of FC with the same ListFeatureClasses passing the shapeType to its feature_type param. – umbe1987 Feb 25 at 14:48
  • You're very welcome. If my answer solved your issue, please feel free to mark it as accepted, otherwise tell me if you need more info. – umbe1987 Feb 25 at 15:29
  • Oof. Didn't even see that option. There you go. Thanks again. – Dante Feb 25 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.