24

I have a question regarding selections in ArcGIS for Desktop. Assumed I have one layer in ArcMap and I have selected two of five features.

Is it possible to get a list of all selected features by using Python?

It would be fine if there is a way to get one special (or all) attribute(s) of the selected features stored in a list that can be written into a txt file.

Is it possible to do this in ArcGIS for Desktop?

3 Answers 3

36

Any time you have a selection on a layer a cursor object will only return the selected rows.

for row in arcpy.SearchCursor("name_of_layer_with_selection"):
    print row.field1, row.field2
2
  • 7
    But the problem though is that if you get all the features returned you don't know if ALL or NONE were selected. Commented Apr 21, 2014 at 14:01
  • 2
    Does this also apply to selected features in feature class ? Commented Sep 4, 2015 at 18:22
15

the Describe function will also return a list. I am not sure if this is faster than the cursor method but I have fond this to be a useful tool. The resulting list is the object id's for the selection set.

import arcpy

aa = arcpy.Describe("someFC")
ss = aa.FIDset
tt = ss.split("; ")
Print tt

[u'1363', u'1364', u'1365', u'1367', u'1369', u'1370']
4
  • Good solution too! Sadly I am not able to set two times the green heel. This solution makes the script also independent from different ArcGIS Versions, because in ArcGIS 10.1 the cursors are called in a different way than in ArcGIs 10.0 (ArcGIS 10.1 arcpy.da.SearchCursor, ArcGIS 10.0 arcpy.SearchCursor...). Commented Dec 31, 2012 at 20:38
  • 4
    Both cursor types are available at 10.1. You don't have to use the new arcpy.da cursors. Commented Dec 31, 2012 at 20:53
  • 1
    This answer gives a way to check for empty selection, which would prevent inadvertently calling a tool on an entire feature class when in fact it was zero features that happened to fulfill your selection criteria. Commented Oct 18, 2017 at 16:28
  • Great tip @Sam Flarity, this is a nice (faster?) alternative to using arcpy.SearchCursor or arcpy.da.SearchCursor Commented Jun 14, 2018 at 0:15
0

Whether ArcGIS Desktop/ArcMap or ArcGIS Pro, the best approach for determining a selection set on a layer or table view is using the getSelectionSet() method of Layer - ArcGIS Pro | Documentation or Layer - ArcMap | Documentation.

Calling layer.getSelectionSet() is better than calling describe.FIDSet for both performance and functional reasons. On the performance side, having to instantiate a Describe object is an extra step and cost when one can get the same information from a layer directly. More importantly, getSelectionSet() recognizes the difference between no selection and a selection with no records whereas FIDSet returns the same empty string in both situations.

>>> arcpy.management.SelectLayerByAttribute(layer, 'NEW_SELECTION', "1=1")
<Result 'layer'>
>>> print(layer.getSelectionSet())
{1, 2, 3, 4, 5}

>>> arcpy.management.SelectLayerByAttribute(layer, 'NEW_SELECTION', "1=2")
<Result 'layer'>
>>> print(layer.getSelectionSet())
set()

>>> arcpy.management.SelectLayerByAttribute(layer, 'CLEAR_SELECTION')
<Result 'layer'>
>>> print(layer.getSelectionSet())
None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.