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
Solved! Go to Solution.
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.
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.
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_
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.
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.
Thanks. I assume that there is no method that can get a map click point, right?
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.
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_
Thanks. Will take a look at the samples.
i got a message saying "can't be downloaded securely". Please help. Thanks...
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.
Thanks. I got it. I am going to take a look at it.