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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 18, 2023 at 14:10

3 Answers 3

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([])
answered Apr 18, 2023 at 15:28
0
1

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([])
answered Apr 18, 2023 at 16:24
0

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

answered Jun 12, 2024 at 15:42
1
  • 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. Commented Jun 12, 2024 at 16:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.