Is it possible to create a GP tool with input that accepts selected features in ArcMap?

1106
2
Jump to solution
11-16-2019 04:34 AM
RemigijusPankevičius
New Contributor III

Basically the title is the description too.

(Some related stuff is in this question: https://community.esri.com/thread/243774-arcpyparameter-how-to-create-the-interactive-feature-input-... )

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Assume you want to just use features that are highlighted/selected on the map and/or in the table, then you specify you 'input' to the tool as a 'featurelayer'.  The tool input for that parameter won't allow you to navigate to disk to select it by name, so you will have to select the 'layer' from the list of layers displayed in the table of contents. (only a drop down to select a layer, with no folder beside the dropdown

Now, if you specify a 'featureclass', you can either select its representation as a layer in the map or you can navigate to its location on disk. (ther is the dropdown and a folder icon beside it)

So a  featurelayer parameter means that the data has to be loaded and displayed.  In that case IF you have a selection, then only the selection is used in geoprocessing tools.  If you have and selection BUT you navigate to disk to locate the dataset, the selection will be ignored, since the disk incarnation knows nothing about the selection.  This is a handy option if you want to run a tool twice, one with a selection and one without

View solution in original post

2 Replies
DanPatterson_Retired
MVP Emeritus

Assume you want to just use features that are highlighted/selected on the map and/or in the table, then you specify you 'input' to the tool as a 'featurelayer'.  The tool input for that parameter won't allow you to navigate to disk to select it by name, so you will have to select the 'layer' from the list of layers displayed in the table of contents. (only a drop down to select a layer, with no folder beside the dropdown

Now, if you specify a 'featureclass', you can either select its representation as a layer in the map or you can navigate to its location on disk. (ther is the dropdown and a folder icon beside it)

So a  featurelayer parameter means that the data has to be loaded and displayed.  In that case IF you have a selection, then only the selection is used in geoprocessing tools.  If you have and selection BUT you navigate to disk to locate the dataset, the selection will be ignored, since the disk incarnation knows nothing about the selection.  This is a handy option if you want to run a tool twice, one with a selection and one without

RemigijusPankevičius
New Contributor III

Thank you, Dan. With your help I am moving somewhere. I used datatype='GPFeatureLayer' for the input parameter.

It's not clear for me what type paramInputFeatures.value is (methods ['addrow', 'exporttostring', 'getrow', 'gettruevalue', 'getvalue', 'loadfromstring', 'removerow', 'setcolumns', 'setrow', 'setvalue']) but I managed to filter out layers with selection in amateur way

It's also a mystery for me what type of parameter is the layer I get. If I compare with documentation for Layer class here (Layer—Help | ArcGIS Desktop ) it's not the same, for example, if I try to query layer.isBroken property I get an error: AttributeError: LayerObject: Get attribute isBroken does not exist.

    def getParameterInfo(self):
        """Define parameter definitions"""

        paramInputFeatures = arcpy.Parameter(
            displayName="Input features",
            name="in_feature_set",
            datatype='GPFeatureLayer',
            parameterType="Required",
            direction="Input",
            multiValue = True)

    def execute(self, parameters, messages):
        """The source code of the tool."""
        paramInputFeatures = parameters[0]

        arcpy.AddMessage('paramInputFeatures.value is: %s' % dir(paramInputFeatures.value))
        inputsAsText = paramInputFeatures.value.exporttostring().split(';')

        i = 0
        for val in paramInputFeatures.values:
            inputText = inputsAsText; i += 1;
            arcpy.AddMessage('Input value [%d] = %s' % (i, inputText))
            # arcpy.AddMessage(dir(val))
            if callable(getattr(val, 'getselectionset', None)):
                # Presence of the method getselectionset() indicates that input is value
                # http://desktop.arcgis.com/en/arcmap/10.5/analyze/arcpy-mapping/layer-class.htm
                arcpy.AddMessage('Value is layer selection, dataSource=%s' % val.dataSource)
                selectionSet = val.getselectionset()
                if selectionSet is not None:
                    # selectionSet is list of long-s: object IDs
                    arcpy.AddMessage(selectionSet)
                else:
                    # Do something with whole layer without selected features, if needed
                    arcpy.AddMessage('Skipping this layer as there are no selected features in it.')
            else:
                arcpy.AddMessage('Skipping input as it is not a layer.')


Output when 1 layer is selected:

Running script AttachTool...
Just pretending to do something...
paramInputFeatures.value is: ['addrow', 'exporttostring', 'getrow', 'gettruevalue', 'getvalue', 'loadfromstring', 'removerow', 'setcolumns', 'setrow', 'setvalue']
Input value [1] = 'Group Layer\ML-2057'
Value is layer selection, dataSource=C:\DATA\Test-Attach\152287.gdb\Markanordningslinje
[4765L, 5162L]
Completed script AttachTool...

0 Kudos