Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

In a sentence, how do you test in Arcpy whether a feature class has any selected records or not? More info below to explain:

I am doing a batch process in which a sampling layer (10km grids) is used to select polygon records (of which there are about 100,000 in total). My script returns the list of polygon IDs that intersect each 10km grid square. I then do some further processing/summarizing using that list of polygon IDs.

In instances where there are polygons within the sample square, ArcGIS performs a selection on the polygons, and it's relatively easy to iterate through those records using a searchcursor. Note I'm using v10.0.

But in instances where there are no selected records (ie there are no polygons intersecting my 10km sample square), the searchcursor would return ALL records in the database. So what's the most robust way to test programatically whether a feature class has a selection or not?

I know you can use arcpy.GetCount_management to return a number that tells you how many records are selected, and if that number = the number of records in the entire layer then you can infer that either none, or all are selected.

But that's not the same as determining if there is a selection or not. What's the best way to do that in Arcpy? Thank you

share|improve this question

marked as duplicate by Jason Scheirer, Mapperz Feb 11 at 17:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
I don't understand how you proceed. If your selection doesn't return any record, then it will be empty, it won't include all records. Then using GetCount is not ambiguous. Can you show your code? –  GISGe Feb 11 at 10:46
    
But that's the point, there is no selection, therefore getcount returns all records rather than zero. There's no such thing as a selection that doesn't return a record; rather, it's a query (whether by attribute or location) that returns no selection. Therefore getcount returns all records. In any event I have a solution which I'll post in a sec... –  TreeMan Feb 11 at 11:03

1 Answer 1

After some more digging, this post answers my question: http://blog.technicallyliving.com/2013/08/arcpy-testing-for-selected-features/

desc=arcpy.Describe("layer_name")
desc.FIDSet

u'3; 4; 5; 6'

The author says "FIDSet will return a semicolon delineated string of selected FIDs. When none are selected, it is blank". Problem solved, I think. Any other solutions would be welcome

share|improve this answer
    
This is a pretty classic question and IMO the solution you found is best. –  KHibma Feb 11 at 14:51
    
As noted in the link provided by @Jason Scheirer, using sel_count = len(desc.fidSet.split(";")) will effectively give you the equivalent of a GetSelectionCount utility, which would make it easier to validate using if sel_count > 0: do something. –  RyanDalton Feb 11 at 16:25

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