Hi,
I am creating an GP tool which has one of the params of type GPLayer and also it is multi-value. We can select multiple layers.
I have to loop through the selected input layer params and run some business logic based on feature layers data. Right now I am using SearchCursor to do that. But I can have cases where there are 2 databases and user can add same feature layers from the 2 DBs in the active map. Now in GP tool we will have same features from 2 different DBs but SearchCursor will not work correctly since it works on feature name and both features will have the same name. How to search data in both features in their respective dbs?
Also how can we get the table name from these feature layers?
can someone help me here with this one?
Where you access the parameters in the execute portion, get it with the value property instead of valueAsText. From there, you can get the catalogPath from Describe.
Use the catalogPath when you create the cursor.
Apologies if this is oversimplified for your use case. I haven't worked with multi-value inputs.
import arcpy
def getParameterInfo(self):
# Ask User for Input Features
features = arcpy.Parameter(
displayName="Input Features",
name="in_features",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input",
multiValue=True)
params = [features]
return params
def execute(self, parameters, messages):
# Get the layers from the parameters
layers = parameters[0].values
# Using data access (returns a python dictionary of properties, easy to debug)
sources = [arcpy.da.Describe(layer.valueAsText)['catalogPath'] for layer in layers]
# Using the arcpy Describe object (returns a arcpy Describe object, harder to debug)
# sources = [arcpy.Describe(source).catalogPath for source in sources]
def business_logic_process(featureclass:str) -> None:
"""Process a feature class
@featureclass: The feature class to process
"""
# Implement business logic here
pass
# Iterate data sources
processed = []
for source in sources:
if source in processed:
arcpy.AddMessage(f"Skipping {source}, already processed")
continue
try:
business_logic_process(source)
except Exception as e:
arcpy.AddError(f"Failed to process {source}, {e}")
return
That should be a good starting point. I added some checks to make sure you don't accidentally process the same source twice (if someone duplicated a layer), and also added a try except block that will allow the rest of the features to be processed if one throws an error during your logic function.