Select to view content in your preferred language

Search data in multiple input feature layers in specific database custom GP tool

416
3
11-01-2023 11:30 AM
CarlosK
New Contributor III

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?

@EarlMedina @JoshuaBixby 

Tags (3)
0 Kudos
3 Replies
CarlosK
New Contributor III

can someone help me here with this one?

@AlfredBaldenweck @BlakeTerhune @DanPatterson @RhettZufelt 

0 Kudos
BlakeTerhune
MVP Regular Contributor

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.

arcpy.Describe(the_layer_obj).catalogPath
 

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.

HaydenWelch
Occasional Contributor II

 

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.

 

0 Kudos