Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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(...)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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