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

2516
14
11-07-2017 12:06 PM
AhnaMiller2
Occasional 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
RobertScheitlin__GISP
MVP Emeritus

Yes I have to agree with Xander here, I do not think it is possible for a GP process to know of a client side selection in WAB using the GP Widget.

ChrisHolmes
Occasional Contributor III

Hi Ahna, curious if you found a solution?

Thanks

0 Kudos
AhnaMiller2
Occasional Contributor

Hello Chris, I ended up using a work around that identified a range of features a user could select and run the function for.My database had more than 20000 features, so I set that a selection range had to be between 1 & 200, If a user did not select anything, it would read it as if all 20000 features were selected & so the function would not run. Not the most ideal solution, but a work around nonetheless. I'd be happy to share this part of the code logic with you if it would help.

0 Kudos
MattDriscoll1
New Contributor II

Could you please share the code, I am running into the same problem. Thank you!

0 Kudos
AhnaMiller2
Occasional Contributor

Hi Matt,

Here's the code  I ended up using to get around this- I basically set it so that a user had to select between 1 - ### features to run the script. This way, if a user does not select between 1 and ##(say 25) , the  script will attempt to run on the entire data set, which will then throw the error message in Line 12

featureLayer = arcpy.GetParameter(0) #this line respects the selection
listA =[]

##Add selected features to listA as needed

if len(listA) in range (1,26): #this is the line that ensures something gets selected as long as the dataset is more than 25 features
     with arcpy.da.UpdateCursor([insertlayerhere], ([comma separated fields here])) as uc:
          for row in uc:
                    uc.updateRow(row)
     msg1= "OBJECTID(s) {0} will be InsertActionHere".format(listA)
else:
     msg1="Please select between 1 and 25 valid features."

#pass message 1
arcpy.SetParameterAsText(1, msg1)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

""""
instead of line ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍6 / passing everything into a list the following code also works:
   features = int(arcpy.GetCount_management(featureLayer).getOutput(0))‍


"""‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hope this makes sense, let me know if not, I'd be happy to discuss further! 

0 Kudos