ArcGIS Pro - Modifying Layer Definition query via ArcPY

8801
10
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
10 Replies
JimPuyda
New Contributor

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