Check that features have been selected

1328
5
05-24-2012 06:48 AM
BrianPangtay
Occasional Contributor
Can one check if any features have been selected in a Python script?

I want to check if any features have been selected prior to running the script. Otherwise, the script would try to process all the records in a feature class. Thus, if no features are selected, the script would tell the user to select some features and then exit the script.

Thanks for your help, Brian
Tags (2)
0 Kudos
5 Replies
BruceNielsen
Occasional Contributor III
One method I've used in the past is to compare the number of features in the input layer (arcpy.GetCount_management) with the number in the layer's source data (arcpy.describe::CatalogPath). If they are the same, nothing has been selected.
0 Kudos
NobbirAhmed
Esri Regular Contributor
Your selection is on a layer, right? if so then use FIDSet as follows:

dsc = arcpy.Describe("LayerName")

selection_set = dsc.FIDSet
if len(selection_set) == 0:
    # no feature is selected
0 Kudos
JoseSanchez
Occasional Contributor III
How do you write this same code in 9.3.1?
0 Kudos
NobbirAhmed
Esri Regular Contributor
I think it is the same for 9.3 as well. Follow this link - you will see FIDSet property on a described object for a layer:

http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Layer_properties
0 Kudos
deleted-user-8KkqhMYcTNGx
Occasional Contributor

I found that this worked for me:

layers = arcpy.mapping.ListLayers(mxd)

for layer in layers:

    desc = arcpy.Describe(layer)

    selcount = desc.fidSet

    if layer.name == "san_Wye":

        print layer.name

        if selcount <> "":

            print "{0} features selected".format(len(desc.fidSet.split(';')))

        if selcount == "":

            print "no features selected"

0 Kudos