I have a large number of groups created, with each group having many layers. I have looked for a way to set a definition query for all the layers in each group but haven't found anything. Is it even possible, maybe with model builder or python? I have ArcMap 9.3 and 10.
1 Answer
This function should handle this.
import arpcy
def definitionByGroup(definition = "", groupname = "", mxd = "CURRENT"):
#create layer list for current map document
layerlist = arcpy.mapping.ListLayers(arcpy.mapping.MapDocument(mxd))
for layer in layerlist:
if layer.isFeatureLayer:
groups = layer.longName.split('\\')
if groupname == "" and layer.supports("DEFINITIONQUERY"):
layer.definitionQuery = definition
if len(groups) > 1 and groupname in groups and layer.supports("DEFINITIONQUERY"):
layer.definitionQuery = definition
arcpy.RefreshActiveView()
Copy paste into the arcpy window and it will be available.
Usage:
Change definition to "OBJECTID = 1" for all feature layers in group "Test" in the current map
definitionByGroup("OBJECTID = 1", "Test")
Same change, but in the map stored at "C:\test.mxd"
definitionByGroup("OBJECTID = 1", "Test", "C:\text.mxd")
Clear definitions on all feature layers in group "Test"
definitionByGroup("", "Test")
Set definition on all feature layers in current map
definitionByGroup("OBJECTID = 1")
Clear all definitions in current map
definitionByGroup()
Clear all definitions in map at "C:\text.mxd"
definitionByGroup("", "", "C:\text.mxd")
This function will also scan nested groups, matching on any layer of the group nesting.
If you want this to be an importable script into ArcMap, save this to a .py file in the pythonpath. e.g. save as definitionByGroup.py and then import and use with:
import definitionByGroup
definitionByGroup.definitionByGroup(...)