0

I'm trying to set a definition query within a Python Toolbox tool in ArcGIS Desktop. The code below works when I run it in the ArcGIS Python window, but does not perform the definition query or provide any error messages when I run the script tool that this code is part of. How do I set a definition query up to work in a script tool? This code (along with other code) is within the def execute(self, parameters, messages): block of the toolbox template that ESRI provides (https://desktop.arcgis.com/en/arcmap/10.3/analyze/creating-tools/a-template-for-python-toolboxes.htm)

import arcpy.mapping as mp
 mxd = mp.MapDocument('CURRENT')
 queryValue1 = 2
 queryValue2 = 1000 
 lyrs = mp.ListLayers (mxd) 
 for l in lyrs:
 if l.name.endswith('rockfall'):
 l.definitionQuery = "{} = {}".format(arcpy.AddFieldDelimiters(lyrs, 'gridcode'), queryValue1) + ' AND ' + "{} > {}".format(arcpy.AddFieldDelimiters(lyrs,'Shape_Area'), queryValue2) 
 arcpy.RefreshActiveView()
 arcpy.RefreshTOC()
asked May 21, 2020 at 21:00
1
  • 1
    Please edit your post to indicate exactly what error you're getting. Also, please check the indentation on your code. It could be a copy/paste thing, but could also be a source of error if intended. Commented May 21, 2020 at 22:35

2 Answers 2

2

Without knowing exactly what error you're getting, it's hard to help troubleshoot for you.

However, I noticed that you're passing a list of layers into the AddFieldDelimiters function.

You've defined lyrs as a list of layers in the MXD. The tool takes a string as per the documentation. Which, I believe would be l.name, in your case.

EDIT: Your MXD is defined as the "CURRENT" MXD. This won't work in the python toolbox because it may be run outside of an MXD session. You'll have to define the MXD as a paramter.

answered May 21, 2020 at 22:33
2
  • I've edited my answer to include what I think it is. Commented May 21, 2020 at 22:45
  • That was a good guess, but it didn't work unfortunately. However, this made me realize I was thinking about this problem the wrong way - I'll post the solution that ended up working for me below. Commented May 22, 2020 at 19:43
0

I ended up solving this by using the arcpy.Select_analysis tool (https://desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/select.htm) to replace all of the code I was using in the definition query. This selected everything I was interested in and exported it to a new file.

I applied the same logic as before (see lines 3,4 and 8 of my original post) in an SQL Query in the 'where clause' of the Select tool.

queryValue1 = 2
queryValue2 = 1000
where_clause = "{} = {}".format('gridcode', queryValue1) + ' AND ' + "{} > {}".format('Shape_Area', queryValue2)
arcpy.Select_analysis(input_features, output_feature_class, where_clause)
answered May 22, 2020 at 19:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.