19

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?

32

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
  • 6
    But the problem though is that if you get all the features returned you don't know if ALL or NONE were selected. – Matej Apr 21 '14 at 14:01
  • 2
    Does this also apply to selected features in feature class ? – Jio Sep 4 '15 at 18:22
12

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']
  • 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...). – Sven Dec 31 '12 at 20:38
  • 4
    Both cursor types are available at 10.1. You don't have to use the new arcpy.da cursors. – blah238 Dec 31 '12 at 20:53
  • 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. – nickbrick Oct 18 '17 at 16:28
  • Great tip @Sam Flarity, this is a nice (faster?) alternative to using arcpy.SearchCursor or arcpy.da.SearchCursor – grego Jun 14 '18 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.