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)
3 Answers 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([])
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([])
I wrote a code to do this but in arcmap, I'm working in a solution to arcgis pro, as soon as I have it, I'll share it
https://space4geomatics-spa.blogspot.com/2022/11/batch-modify-definition-query.html
-
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.2024年06月12日 16:31:09 +00:00Commented Jun 12, 2024 at 16:31
Explore related questions
See similar questions with these tags.