Select to view content in your preferred language

How to identify if 0 records are selected in a layer using python

3308
14
11-07-2017 12:06 PM
AhnaMiller2
Frequent Contributor

I am currently working on a script that reads the number of features selected in a layer and returns a message or does a function based on how many records are selected. 

If 0 are selected:  it tells the user to "make a selection"

If >10, it tells users "too many features selected, must be less than 10"

If they select between 1-10 features, it runs the rest of the function.

I have the following logic:

features = int(arcpy.GetCount_management (featureLayer).getOutput(0))

if features<1:

 arcpy.addmessage(msg1)

elif features>10:

arcpy.addmessage(msg2)

else:

function proceeds....

The issue with this logic is that if no features are selected (if features<1:) it reads it as the count of the entire feature class and so returns msg2. 

I tried instead to use the logic of 

desc = arcpy.Describe (Featureclass)

if desc.FIDset ==' ':

arcpy.addmessage (msg1)

This second logic works in ArcGIS desktop, but will not work in AGOL if I publish as a Geoprocessing service.

Can anyone think of any workarounds that will recognize if 0 records are selected?

0 Kudos
14 Replies
DanPatterson_Retired
MVP Emeritus

GetCount_management is one way

0 Kudos
AhnaMiller2
Frequent Contributor

Thanks Dan, the issue with GetCount_management is that if I have nothing selected, it sees all the records in the layer so it retruns message 2 (too many features selected) instead of message1 (no features selected)

0 Kudos
DanPatterson_Retired
MVP Emeritus
while True:
    n = input("Enter a number between 1 and 10:  ")
    if int(n) > 0 and int(n) <11:
        print('done')
        break 
    

Enter a number between 1 and 10:  0

Enter a number between 1 and 10:  11

Enter a number between 1 and 10:  2


done

then  you could switch the idea up a bit in a while loop perhaps  while 

But you mentioned agol... hmmm don't do it, but the logic is

0 Kudos
JamesMacKay3
Frequent Contributor

I haven't tried this with AGOL data, but getSelectionSet() on the arcpy.mapping.Layer class might be worth a look.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Ahna Miller , can you clarify where you want to execute the Python code?

When you use:

desc = arcpy.Describe(Featureclass)
if desc.FIDset != '':
    # you have a selection
    print("Number of selected features: {}".format(len(desc.FIDSet.split(";"))))

you should not pass in a "FeatureClass" (reference to a featureclass stored on disk) since it will never have a selection. You will have to reference a layer in a ArcMap or ArcGIS Pro session to make this work.

Your comment:

This second logic works in ArcGIS desktop, but will not work in AGOL if I publish as a Geoprocessing service.

... has confused me.

There are two things that needs clarification:

  • "will not work in AGOL"; are you loading a hosted feature service in Desktop and run the code in desktop?
  • "if I publish as a Geoprocessing service"; where is the geoprocessing service published? Not is AGOL because as far as I know you can only publish this in server. 

Since you posted this question in the ArcGIS Python API space, you might be working with this API in a Jupyter notebook, but then the question arises if you would reference a web map with your layer how will you make the selection? So my question is, what is your specific use case, what would you like to achieve and where and for whom?

AhnaMiller2
Frequent Contributor

Hi Xander,

To clarify- I am attempting to use this python code as a geoprocessing tool in AGOL webapp builder. I test from desktop before publishing as a GP service, but ultimately the script needs to work as a GP service from the geoprocessing widget in WAB.

0 Kudos
XanderBakker
Esri Esteemed Contributor

So, I still have a question about which selection made where should be detected by the GP service executed in WAB? It should detect the selection made in the WAB by the current user?

0 Kudos
AhnaMiller2
Frequent Contributor

xander_bakker‌ , yes. It should detect the selection made in the WAB by the current user. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

I don't think a GP service run at the server is capable of detecting a selection that "lives" at a client. You should probably code this.

Let me tag rscheitlin‌ and rastrauch who will probably have an answer to this.