3

I am using ArcGIS Pro 3.x.

I'm trying to make a script to automate changing the definition query for multiple layers in a group layer in Pro. The definition query is essentially just using a project ID number ("TDS_WBS_CODE") to filter the data to a specific project.

The goal is to change the active definition query for multiple layers in a group layer and not have a list of unused queries.

My current code seems to fail at the "# Change the Definition query of target layers" comment.

import arcpy

# access current pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")

# access current map frame
m = aprx.activeMap

# access layers in TOC
lyrList = m.listLayers()

# list of target layer names
ADP_GIS_EDIT = ["ADP_GIS_EDIT\FiberSpliceEnclosure", "ADP_GIS_EDIT\AccessPoint", 
                "ADP_GIS_EDIT\FiberSplitter", "ADP_GIS_EDIT\Pole",
                "ADP_GIS_EDIT\FiberCable", "ADP_GIS_EDIT\Conduit", "ADP_GIS_EDIT\ServingArea"]

# loop that grabs "PortConnection" table
for m in aprx.listMaps():
    for tbl in m.listTables():
        if tbl.longName == "ADP_GIS_EDIT\PortConnection":
            print(tbl.longName)

# loop that grabs target features within "ADP_GIS_EDIT" group layer
for l in lyrList:
    if l.isGroupLayer:
        lyrs = l.listLayers()
        for lyr in lyrs:
            if lyr.longName in ADP_GIS_EDIT:
                # Change the Definition query of target layers
                cim_lyr = lyr.getDefinition("V2")
                if cim_lyr.featureTable.definitionFilterChoices:
                    cim_lyr.featureTable.definitionFilterChoices[0].definitionExpression = "TDS_WBS_CODE = 'TC-211606035'"
                    cim_lyr.featureTable.definitionExpression = None

                else:
                    cim_lyr.featureTable.definitionExpression = None

                    lyr.setDefinition(cim_lyr)

2 Answers 2

3

I usually set the definition query using the definitionQuery property:

lyr.definitionQuery = "TDS_WBS_CODE = 'TC-211606035'"

And clear the definitionQueries using:

lyr.updateDefinitionQueries([])
0
0

Here's the code that worked!

import arcpy

# access current pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")

# access current map frame
m = aprx.activeMap

# access layers in TOC
lyrList = m.listLayers()

# list of target layer names
ADP_GIS_EDIT = ["ADP_GIS_EDIT\FiberSpliceEnclosure", "ADP_GIS_EDIT\AccessPoint", "ADP_GIS_EDIT\FiberSplitter", "ADP_GIS_EDIT\Pole",
                "ADP_GIS_EDIT\FiberCable", "ADP_GIS_EDIT\Conduit", "ADP_GIS_EDIT\ServingArea"]

# CHANGE THIS ACCORDING TO PROJECT WBS CODE!!!
WBS = "TDS_WBS_CODE = 'TC-221608179'"

# loop that grabs "PortConnection" table
for m in aprx.listMaps():
    for tbl in m.listTables():
        if tbl.longName == "ADP_GIS_EDIT\PortConnection":
            # Change the Definition query of target table
            tbl.updateDefinitionQueries([])
            tbl.definitionQuery = WBS
        else:
            tbl.updateDefinitionQueries([])

# loop that grabs target features within "ADP_GIS_EDIT" group layer
for l in lyrList:
    if l.isGroupLayer:
        lyrs = l.listLayers()
        for lyr in lyrs:
            if lyr.longName in ADP_GIS_EDIT:
            # Change the Definition query of target layers
                lyr.updateDefinitionQueries([])
                lyr.definitionQuery = WBS
            else:
                lyr.updateDefinitionQueries([])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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