I am using ArcGIS Pro 2.9
Using ArcPy I have to initiate multiple queries every day on 60+ layers and all these queries are piling up in Definition query tab for all these layers. I am unable to delete the queries using
lyr.definitionQuery = ""
OR
lyr.definitionQuery = None
both just deactivate the queries.
Is there a way to reuse the existing definition query again, like if using query name, using ArcPy?
Update:
Even replacing query did not help, what I tried is as follow
Old_DT = lyr.definitionQuery
New_DT = lyr.definitionQuery.replace(Old_DT,DT)
lyr.definitionQuery = New_DT
Still New query is added in query definition list.
-
Have you explored using CIM to alter the definition query? Most fine tweaking of layers in ArcPro can be done with that.Hornbydd– Hornbydd2022年03月11日 09:33:22 +00:00Commented Mar 11, 2022 at 9:33
-
1Similar questions have been asked and answered on Esri Community/GeoNet. See ArcGIS Pro - Modifying Layer Definition query via ArcPYbixb0012– bixb00122022年03月11日 16:33:53 +00:00Commented Mar 11, 2022 at 16:33
-
Yes, I saw above post and everything regarding CIM went over my head, hence a new post.Abdul Rauf– Abdul Rauf2022年03月12日 10:27:11 +00:00Commented Mar 12, 2022 at 10:27
2 Answers 2
You can use CIM to manipulate multiple definition queries that a layer may have, below is some sample code to demonstrate this. In this example the layer already has a Query Definition called "SillyQuery" imposed upon it. As you have spotted simply editing it causes a new query to be inserted into the list. This seems to be default behaviour that one cannot override but as it appears to be the query added to the end of the list, this code simply removes it and overwrites the list, this is how it stops the ever growing list of query definitions.
import arcpy
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers('Cables')[0] # Example layer
# Return the layer's CIM definition
l_cim = l.getDefinition('V2')
# Get Feature Table object
fTab = l_cim.featureTable
# Get the Query Definition Name, this assumes one is active
expName = fTab.definitionExpressionName
# If SillyQuery is active then adjust the expression
if expName == "SillyQuery":
fTab.definitionExpression = "OBJECTID = 2"
# This causes an insert of a new entry into the list
# Remove new entry in list
aList = fTab.definitionFilterChoices
aList = aList[:-1]
fTab.definitionFilterChoices = aList
# Push the changes back to the layer object
l.setDefinition(l_cim)
-
This Works as intended. Have not worked with CIM before, will look into this further. for CIM i have two more questions a) can we loop through queries? b) as i said earlier "" or None is not deleting queries. Can we delete queries with CIM?Abdul Rauf– Abdul Rauf2022年03月12日 11:43:03 +00:00Commented Mar 12, 2022 at 11:43
-
Also If the SillyQuery does not exists how can i create it?Abdul Rauf– Abdul Rauf2022年03月12日 11:46:45 +00:00Commented Mar 12, 2022 at 11:46
-
1In the code
aList
is the list of queries. You could insert/delete queries. I would recommend you get the CIM viewer installed it will help you understand the object model you need to navigate.Hornbydd– Hornbydd2022年03月13日 01:46:38 +00:00Commented Mar 13, 2022 at 1:46
For those not quite ready to dive into the CIM, you can use the listDefinitionQueries() method to see all of the definition queries associated with a layer (as a list of dictionaries), and then use the companion updateDefinitionQueries() method to overwrite/remove/otherwise maniuplate the various definition queries associated with said layer.
Example:
layer.listDefinitionQueries()
[{'name': 'Query 1',
'sql': "Field1 = 'Value 1'",
'isActive': False},
{'name': 'Query 2',
'sql': "Field2 = 'Value 2'",
'isActive': True}]
layer.updateDefinitionQueries(
[{'name': 'Query 1',
'sql': "Field1 = 'Value 3'",
'isActive': True}])
After running the updateDefinitionQueries() method as above, the listDefinitionQueries() method would subsequently return the following:
layer.listDefinitionQueries()
[{'name': 'Query 1',
'sql': "Field1 = 'Value 3'",
'isActive': True}])
To clear existence of all existing definition queries on a layer, you can simply run:
layer.updateDefinitionQueries([{}])