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.
-
3This may have already been partially answered: Can I get a list of my selected features in ArcGIS by using Python code? Also see: How to access data from a selected feature in ArcGIS 10?Axel Esteban– Axel Esteban2013年10月02日 17:23:47 +00:00Commented Oct 2, 2013 at 17:23
-
Hi, I've read and worked through those posts, but neither allow me to get the results of a selection made by a user unless I know which layer it was made in. Is there a way to get a selection in an unknown layer with only knowing the .gdb?mtpultz– mtpultz2013年10月05日 06:00:16 +00:00Commented Oct 5, 2013 at 6:00
2 Answers 2
To iterate over the layers in the map, get a reference to the current map document and list the layers within it:
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd):
for layer in layers:
# do something with 'layer'
To just get the number of selected features, try leveraging the Describe
object's fidSet
property:
desc = arcpy.Describe(layer)
sel_count = len(desc.fidSet.split(";"))
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 the arcpy.SearchCursor
or arcpy.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:
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> layers = arcpy.mapping.ListLayers(mxd)
>>> for layer in layers:
... desc = arcpy.Describe(layer)
... if desc.fidSet:
... print layer.name, "has {0} features selected:".format(len(desc.fidSet.split(';')))
... for row in arcpy.da.SearchCursor(layer, "*"):
... print row
...
atlantic_hurricanes_2000 has 2 features selected:
(220, (-86.60000000039969, 30.500000000299735), datetime.datetime(2000, 9, 22, 0, 0), u'12:00:00', 30.5, -86.6, 1006.0, 35.0, u'tropical storm', u'Helene', datetime.datetime(2000, 9, 22, 12, 0))
(221, (-85.40000000009991, 31.599999999900035), datetime.datetime(2000, 9, 22, 0, 0), u'18:00:00', 31.6, -85.4, 1010.0, 25.0, u'tropical depression', u'Helene', datetime.datetime(2000, 9, 22, 18, 0))
atlantic_hurricanes_2000_lines has 1 features selected:
(8, (-70.38282674742592, 29.09090367649801), 84.23001922499401, u'Helene', 34.76190476190476, None)
-
2This works well, but FIDSet will be set to
''
if no features are selected instead ofNone
. This will result in a count of 1 (''.split(';') == ['']
!) instead of 0 andif desc.FIDSet
will also be true in that case (at least with ArcGIS 10.2.1).Evil Genius– Evil Genius2017年09月08日 17:43:34 +00:00Commented Sep 8, 2017 at 17:43
I haven't tried this, but you could call ListLayers, then cycle through all possible layers and use the getSelectedExtent method, which returns an Extent object; if the extent isn't null/zero, the layer is selected. Then you'd know which layer the user has selected, and you can use a cursor or CopyFeatures_management on them. See the following links:
Hope this helps.
Explore related questions
See similar questions with these tags.