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?
GetCount_management is one way
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)
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
I haven't tried this with AGOL data, but getSelectionSet() on the arcpy.mapping.Layer class might be worth a look.
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:
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?
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.
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?
xander_bakker , yes. It should detect the selection made in the WAB by the current user.
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.