I'm trying to develop a python code that will apply the same definition query to all layers in a specified group. I currently have a working code that will apply a definition query to all layers in the Table of Contents, but unfortunately I need it to apply the syntax to a specific group of layers. Any ideas?
I've seen this post:
Create a definition query for groups of layers: Is it possible?
But when I set it up specific to my mxd project, nothing happens.
import arcpy
#Variables to form query syntax
#-------------------------------------------
#field in Attribute table
field = 'Country \n'
queryField = '"%s"' % field.strip() #add double quotes field (for query syntax)
#value in specified field
value = "'Canada'"
#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
Best Answer
Okay I got it, thanks anyways! It's a little bit more elegant now.