check for selected features

2541
6
Jump to solution
01-03-2013 09:13 AM
MikeLouwrens
Occasional Contributor III
Hi,

I trying to create an arcpy script that will run only if there are selected features in a layer, however I can't figure out how to get arcpy to test for selected features.

I've read posts about using GetCount, however this seems to only work if there is a difference in the number of selected features to the full feature count of the layer.  I want my script to run even if EVERY feature in the layer is selected (but ONLY if features are selected).

Is there a way I can do this?

Cheers,
Mike.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChrisFox3
Occasional Contributor III
Layer Describe properties has an FIDSet property which returns a semicolon-delimited string of selected feature IDs. If the string is empty no features are selected:

desc = arcpy.Describe(lyr) if not desc.FIDSet  == '':    print 'Layer has a selection'

View solution in original post

0 Kudos
6 Replies
CarlSunderman
Occasional Contributor
I would think that you could get an initial count and assign as say numRecords then run a selection and set another get count as selRecords. then check if the selected count is null
0 Kudos
by Anonymous User
Not applicable
Hi,

I trying to create an arcpy script that will run only if there are selected features in a layer, however I can't figure out how to get arcpy to test for selected features.

I've read posts about using GetCount, however this seems to only work if there is a difference in the number of selected features to the full feature count of the layer.  I want my script to run even if EVERY feature in the layer is selected (but ONLY if features are selected).

Is there a way I can do this?

Cheers,
Mike.

Yes, there are many ways to do this, here is one:

result = int(arcpy.GetCount_management(Parcels).getOutput(0))
if result:
    # do something
else:
    print 'No selected features'
0 Kudos
MikeLouwrens
Occasional Contributor III
Yes, there are many ways to do this, here is one:

result = int(arcpy.GetCount_management(Parcels).getOutput(0))
if result:
    # do something
else:
    print 'No selected features'

This returns a true result even if I haven't selected anything.  This only returns a false result if I run a selection and nothing is selected...

Mike.
0 Kudos
ChrisFox3
Occasional Contributor III
Layer Describe properties has an FIDSet property which returns a semicolon-delimited string of selected feature IDs. If the string is empty no features are selected:

desc = arcpy.Describe(lyr) if not desc.FIDSet  == '':    print 'Layer has a selection'
0 Kudos
MikeLouwrens
Occasional Contributor III
I would think that you could get an initial count and assign as say numRecords then run a selection and set another get count as selRecords. then check if the selected count is null
🙂

this is what I have done - I ran a selection on objectid = 1000000000000 (a feature that should never exist...) then my getcount will return 0 if nothing else selected, or the number of selected features if other features are already selected.

arcpy.SelectLayerByAttribute_management(layer.name,"ADD_TO_SELECTION",'"OBJECTID" = 1000000000000')

featurecount = int(arcpy.GetCount_management(layer.name).getOutput(0))


if featurecount > 0:
    #Do Something
else:
    arcpy.SelectLayerByAttribute_management(layer.name, "CLEAR_SELECTION")

This seems to work 😄

Cheers,
Mike.
0 Kudos
MikeLouwrens
Occasional Contributor III
Layer Describe properties has an FIDSet property which returns a semicolon-delimited string of selected feature IDs. If the string is empty no features are selected:

desc = arcpy.Describe(lyr)
if not desc.FIDSet  == '':
   print 'Layer has a selection'


oh! that works too.  thanks for that 🙂

Cheers,
Mike.
0 Kudos