Using arcpy I'm trying to get the results of a selection, or what is currently selected as a list.
Basically I'm attempting to build an addin which on click will either perform an operation where I invoke SelectLayerByAttribute_management (or ByLocation) to select some features, or the user performs a selection ByAttribute (or ByLocation), and while they have their selection hightlighted executes my snippet by pressing a button on the toolbar. In each case I want to get a list of results to perform a specific task on only certain selected features.
I can't find out how to get a list of the currently selected features, or find a listener for selection having been made so I can enable the button, and ensure I can perform my operation on an existing selection.
Best Answer
To iterate over the layers in the map, get a reference to the current map document and list the layers within it:
To just get the number of selected features, try leveraging the
Describe
object'sfidSet
property:You can check this count to determine whether any features are selected, or just do
if desc.fidSet:
which should accomplish the same.To get a particular field's values for each selected feature within a layer, use a
SearchCursor
(either thearcpy.SearchCursor
orarcpy.da.SearchCursor
variety), which honors the selection on layers. See the other questions linked in the comments above.Interactive session in ArcMap's Python window to demonstrate how you might do this: