2

A newbie to python here. Ive been trying to query all layers on a mxd and have sucessfully implemented a script by advice on here. What I need to do now is to firstly make the query a parameter that the user inputs (always from the same field) and also an option to clear the sql query from the layers.

Currently I have:

import arcpy

#Variables to form query syntax
#field in Attribute table
field = 'Source'
queryField = '"%s"' % field.strip()   #add double quotes field (for query syntax)
#value in specified field
value = "'Integrated Geological Map'"
#concatenate query syntax
queryStr = str(queryField) + "=" + str(value)
#--------------------------------------------
mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DEFINITIONQUERY"):
        lyr.definitionQuery = queryStr
arcpy.RefreshActiveView()
del mxd
  • 1
    Change your assigment to Value =arcpy.getParameterAsText (0), then in the script tool properties add an input parameter of type string. You can add some logic so that if input is blank, clear defQ. – Ben S Nadler Jun 7 '16 at 3:53
  • What @Ben said will do the trick. The best option is to limit the parameter within the script tool to be one of a set list, and add an option of None. In your code use an IF statement test for None and clear the definition query as required. Then use an ELSE statement to handle setting the definition query. – Adam Jun 13 '16 at 9:28
1

To prepare your script for receiving a parameter from the user, assign value to arcpy.getParameterAsText(0). The zero in the assignment identifies this as the first parameter you will set in the script tool.

Next, modify your script to work with the parameter. Add an IF/ELSE statement to handle the option to clear the query: value = arcpy.getParameterAsText(0) if value == 'NONE': queryStr = '' else: queryStr = str(queryField) + "=" + str(value)

Your existing FOR loop should then function as expected and set the query.

Once these changes are made in the script, you'll need to configure the script tool in ArcMap. If you are sure that the values in the field you plan to query are not going to change, you can set them manually when editing the script tool. Right-click the tool and select Properties, then choose the Parameters tab.

  • Add a suitable Display Name for the parameter and set the Data Type to 'String'.
  • Under Parameter Properties, set the Filter to 'Value List'.
  • Click the ellipsis next to the Filter, and in the resulting Value List box, enter the possible query values, making sure to include the option for NONE as well as to match the case for the values as they appear in the attribute table (I make mine all caps for ease). Parameters and Filter List

Once this is done, you should be able to run the script tool and select the required value to set the query, or select NONE to have the definition query cleared.

Tool with query parameters

If you want to take the tool a step further, you can add tool validation to obtain the values for the filter from the relevant field on one of your layers. The image below shows how this can be done. I've posted it as an image rather than a code block so that I can highlight the line numbers which contain changes from the standard tool validation. If you do choose to use tool validation, you'll need to insert the NONE option to the list being built since it doesn't appear in the field being used to populate the filter list - this is line 17 below, and the value is underlined in red .

Tool validation script

  • Thanks for your in depth response Adam, much appreciated!! – polecat Jun 16 '16 at 16:20
  • No problem. Don't forget to accept it if it solves your problem. – Adam Jun 16 '16 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.