ArcGIS Pro - Modifying Layer Definition query via ArcPY

10271
12
10-08-2019 07:36 PM
janrykr1
Emerging Contributor

Let's say I need to add a new statement to an existing Def query to all layers in a group which would like like something like this:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers():
 if lyr.supports ("longname"):
 parent = lyr.longName.split('\\')
 
 if parent [0] == "Group":
 print (lyr.longName)
  if lyr.supports("DEFINITIONQUERY"):
   oldDefQuery = lyr.definitionQuery
   lyr.definitionQuery = None
   newDefQuery = oldDefQuery + " CODE = 0" 
   lyr.definitionQuery = newDefQuery‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

What happens after running the script is that the old definition query becomes inactive and the newly created query is added and becomes active. However they both remain the same query name and seem to clash with each other so that no feature pass through the query (don't mind the fieldnames):

There seem to be no detailed explanation on the definitionQuery property at Layer—ArcPy | ArcGIS Desktop 

So the questions are: 

  • How to achieve this to work properly:
    • Modify the existing one? - This used to work in ArcMAP
    • Remove the initial query completely and place a new modified query?
  • Is there any documentation on how to control the functions in the definition query tab on layer properties via Arcpy?? 

Thanks heaps!

0 Kudos
12 Replies
MikeLachance1
Regular Contributor

Hi Carson,

Here is the part where I apply a definition query. Hope this helps:

 

 

 

if lyr.name in layer_list:
                
            def_q = YOUR_DEF_QUERY
                
            cim_layer = lyr.getDefinition('V3')
            
            if cim_layer.featureTable.definitionFilterChoices:
                cim_layer.featureTable.definitionFilterChoices[0].definitionExpressionName = ''
                cim_layer.featureTable.definitionFilterChoices[0].definitionExpression = None
                cim_layer.featureTable.definitionExpression = None
            
                cim_layer.featureTable.definitionFilterChoices[0].definitionExpression = def_q   
                cim_layer.featureTable.definitionExpression = def_q
                
            else:
                cim_layer.featureTable.definitionExpression = def_q
            
            lyr.setDefinition(cim_layer)   

 

 

First I check if there are any definition queries already on the layer. If there are I clear them out. Then I apply mine using the .definitionExpression method. Finally I set the layer definition.

 

0 Kudos
LindsayRaabe_FPCWA
MVP Regular Contributor

For others looking for help in this space. 

I've just tested the below line in a python script that looks at a set map (Data Editing) in the Current project, and had the last 2 lines repeated for all the different layer names that I wanted it to update. It successfully deleted the definition query from each layer. 

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Data Editing")[0]

for lyr in m.listLayers("Layer 1"):    ###Repeat these 2 lines for each layer
    lyr.definitionQuery = None

for lyr in m.listLayers("Layer 2"):    
    lyr.definitionQuery = None

I plan to write a simple script that can be used on any project and map which will just go through all layers in the current map and wipe and definition queries. Variations of this could include a 3rd line for each layer that adds a replacement query in the originals place. 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
JimP-notUniqueAtAll
Emerging Contributor

Sorry,  lyr.definitionQuery = None    does not work.  It still is keeping all query clauses.   Frustrating.