Arcgis Pro 2.9 ArcPy Get Selected Feature

4624
10
Jump to solution
05-03-2022 12:21 PM
YinghongLi1
Occasional Contributor

If a user selects a feature from the map by clicking,  can I use Python to get this selected feature?  I am from ArcObject and am learning ArcPy.  From the reference document and samples, I have not found a way to get the selected feature from the map directly.  I am wondering if ArcPy has classes/functions to interact with maps.

Thanks

 

0 Kudos
4 Solutions

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

You'll want to get familiar with the arcpy.mp sub module. In general, you'll define a project, then identify a map in that project, then a layer in that map, then call the layer's getSelectionSet () method. This only gives you a list of objectId values for the selected features, not the actual feature. If you want access to the feature, use a data access cursor. Or, if you want to run a tool on the selected feature, you can usually just use the layer with the selection as the input and it will only run on the selection.

View solution in original post

BlakeTerhune
MVP Regular Contributor

Interesting question. If you were making a Python Toolbox, I think you could use a GPFeatureRecordSetLayer parameter. In the getParameterInfo function of the tool, you would create a feature class that outputs to a memory workspace, then make a feature layer from the feature class, and set the value of the GPFeatureRecordSetLayer parameter to the feature layer. This creates an input on the tool to draw a feature on the map.

I've seen this done in ArcMap, but not in ArcGIS Pro so the process might be slightly different.

View solution in original post

RhettZufelt
MVP Frequent Contributor

Not sure in Pro as I have not looked into Add Ins for Pro, but in ArcMap, getting the map click point was fairly simple using an Add In.

Good example here.

R_

View solution in original post

YinghongLi1
Occasional Contributor

Thanks.  I got the downloads.

It is an addin file so i can't check the source file.  I need some Python code to guide my tool creation.  

View solution in original post

0 Kudos
10 Replies
BlakeTerhune
MVP Regular Contributor

You'll want to get familiar with the arcpy.mp sub module. In general, you'll define a project, then identify a map in that project, then a layer in that map, then call the layer's getSelectionSet () method. This only gives you a list of objectId values for the selected features, not the actual feature. If you want access to the feature, use a data access cursor. Or, if you want to run a tool on the selected feature, you can usually just use the layer with the selection as the input and it will only run on the selection.

YinghongLi1
Occasional Contributor

Thanks.  I assume that there is no method that can get a map click point, right?

0 Kudos
BlakeTerhune
MVP Regular Contributor

Interesting question. If you were making a Python Toolbox, I think you could use a GPFeatureRecordSetLayer parameter. In the getParameterInfo function of the tool, you would create a feature class that outputs to a memory workspace, then make a feature layer from the feature class, and set the value of the GPFeatureRecordSetLayer parameter to the feature layer. This creates an input on the tool to draw a feature on the map.

I've seen this done in ArcMap, but not in ArcGIS Pro so the process might be slightly different.

RhettZufelt
MVP Frequent Contributor

Not sure in Pro as I have not looked into Add Ins for Pro, but in ArcMap, getting the map click point was fairly simple using an Add In.

Good example here.

R_

YinghongLi1
Occasional Contributor

Thanks.  Will take a look at the samples.

0 Kudos
YinghongLi1
Occasional Contributor

i got a message saying "can't be downloaded securely".  Please help.  Thanks...

 

0 Kudos
RhettZufelt
MVP Frequent Contributor

Looks like that page hasn't been updated to https yet.

Here is the download link in https.

R_

0 Kudos
YinghongLi1
Occasional Contributor

Thanks.  I got the downloads.

It is an addin file so i can't check the source file.  I need some Python code to guide my tool creation.  

0 Kudos
YinghongLi1
Occasional Contributor

Thanks.  I got it.  I am going to take a look at it.

0 Kudos