I have a script tool which is supposed to apply a definition query to all layers in a feature service in the map and then remove all empty feature layers from the Contents pane. The tool can successfully apply the definition query but fails to remove the empty layers. I put the code to remove the empty layers in a separate script tool and it works! Any idea why that code won't work in the same script as the definition query code?
def script_tool(param0, param1,param2):
"""Script code goes below"""
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.activeMap
for lyr in m.listLayers():
if lyr.supports("DEFINITIONQUERY"):
#If the Clear Queries option is checked:
if param2:
lyr.definitionQuery = ""
else:
#Apply definition query to all layers in the map
lyr.definitionQuery = param1
#Remove empty layers
arcpy.conversion.ExportFeatures(lyr, "temp")
results = arcpy.GetCount_management("temp")
count = results.getOutput(0)
if count == '0':
m.removeLayer(lyr)
aprx.save()
return
if __name__ == "__main__":
param0 = arcpy.GetParameterAsText(0)
param1 = arcpy.GetParameterAsText(1)
param2 = arcpy.GetParameterAsText(2)
script_tool(param0, param1,param2)
You don't need to run an export to get a count of your layer. GetCount respects a definition query anyway, so you can run it on the original layer, without creating a temp layer. So you can remove line 18 and replace (what was) line 19 target object with lyr instead of "temp". Like this...
#Remove empty layers
results = arcpy.GetCount_management(lyr)
count = results.getOutput(0)
if count == '0':
m.removeLayer(lyr)
Without being able to see what parameter 2 is (it's not in your attached toolbox tool), then it's tricky to know what is going on there. Your if statement simply says:
if param2:
Normally you would give that statement a value e.g.:
if param2 is True:
Which should work if your param 2 is a boolean checkbox (as suggested by your comments). All that would happen in that case is all definition queries would be reset
Hi Richard, thanks for your reply. I thought exporting the view service layer was necessary to use with GetCount but apparently it isn't. Even without exporting the layer, the removeLayer functionality doesn't work when in the same script as the definition query. I also changed the attached tool to the version which has the Boolean checkbox.
The raw text works for me in a python window (without the if/else statement), so I can only assume it has something to do with that. Unfortunately I can't see an attachment at all now. If you reupload it, I'll take a look.
Mark,
I've tested it, and by making the modification that I suggested above it works for me. Only tweak was as below for line 18 in your script:
if param1 is True:
lyr.definitionQuery = ""
So the tool will both apply the definition query and remove the empty layers for you?
Yes, it works. Removes the empty one, leaves the one with records once I'd made that change
For some reason the tool won't execute the whole script the first time I click Run. It only applies the definition query. I have to click Run again for it to remove the empty layers!
Does it throw any error or warning messages in your geoprocessing history when you've just clicked it once? And have you amended line 18 to match mine?