2

I am trying to put together a little python script to check for whether there is a selection. The dataset I have is not robust enough to have an definition query on it, so I want it to just check whether there is a selection. arcpy.GetCount_management does this, and when I use the Describe function it does this too, but whenever I don't have a selection it is giving me the total number of rows in the dataset. From what I've read in the literature, the Describe function at least should return a zero value, because none are selected. Can anyone tell me why I can never get a zero value from either of these tools?

This is the piece of code I've been playing with most recently

len(arcpy.Describe("Footpath_centreline_Layer").FIDSet.split(';'))

so when I have nothing selected, 0 out of 16 records - I get a return of 16, and I need for it to be 0. I'm not sure how to achieve this, given my very limited python knowledge.

4

FIDSet returns an empty string if nothing is selected.

splitting an empty string returns a list containing one element - and the element is an empty string!

If sep is specified as any string, the result will be a list containing one element which is an empty string.

So, you need to first check to see if if len FIDSet is > 0 before splitting it.

| improve this answer | |
  • I'm rather limited in my python knowledge...do you mean if I try 'arcpy.Describe("Footpath_centreline_Layer").FIDSet' ? When I did this I got 'u'1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16'' and there are only 16 lines in the layer. Are you able to explain more please? – hlane Aug 2 '16 at 4:43
  • That would indicate there are 16 features currently selected in your layer. If you click the "clear selection" button and run it again you should get u'', indicating that nothing is selected. – Kirk Kuykendall Aug 2 '16 at 14:13
2

There's a nice walk-through of what you're trying to do here: http://joelmccune.com/get-layer-selected-feature-count-using-python/

As mentioned by Joel at his website, another way to get an integer count of selected records is to use arcpy.GetCount_management(myfeatureclass), as explained by Esri: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/get-count.htm

Something like:

result = arcpy.GetCount_management(lyrfile)
count = int(result.getOutput(0))

This will return the number of records in the whole dataset if nothing is selected, though. So, as @Kirk said, to check whether there's a selection at all, check whether len(FIDSet) > 0.

| improve this answer | |
0

Try using layer.GetSelectionSet()

mxd = arcpy.mapping.MapDocument("CURRENT")
layerName = 'my_layer'
lyr = arcpy.mapping.ListLayers(mxd, layerName)[0]

selCount = 0
if lyr.getSelectionSet(): # If there are selected features
    selCount = len(lyr.getSelectionSet())

print selCount # Print number of selected features

Printing selCount will return the number of selected features. If there are no selected features it'll skip over the if and return a total of 0

| improve this answer | |

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.