Select to view content in your preferred language

Change Definition Query of Multiple Layers with Python

2437
2
12-15-2010 09:42 AM
CraigLimbert
Occasional Contributor
Can anyone help me with a python script that will change the definition query of two layers to the same thing. I have 2 layers that I want to change the definition query on at the same time to the same value(s).
ex) Layer1 Definition Query ---[pid] = 4
     Layer2 Definition Query ---[pid] = 4

I want to be able to update both definition queries in one step using python.
Tags (2)
0 Kudos
2 Replies
ChrisMathers
Deactivated User
Well you would create two layer objects with arcpy.mapping and then iterate over them changing the definition queries using the definitionQuery attribute of the layer.

Create_Layers
for layer in layers:
     layer.definitionQuery='[pid] = 4'

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/
0 Kudos
DanielJohns
Frequent Contributor
import arcpy
queryStr = "Hello World"
mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
  if lyr.supports("DEFINITIONQUERY"): 
  lyr.definitionQuery = queryStr

del mxd
0 Kudos