Parcel fabric has default definition queries Taxlot_lines - (RetiredByRecord Is NULL). I have successfully added additional definition queries to support other business needs and can manually change between them. Is there any way to change which definition query to use with Python. Also I create a new definition query with the layer property but would rather not delete and create them all the time. Especially when parcel fabric comes with some already.
Solved! Go to Solution.
If you know the query names, you can do something like this:
def change_definition_query(layer, query_name):
"""Changes the definition query of a layer in ArcGIS Pro.
layer: arcpy.mp.Layer object, the layer that the query will be assigned to
query_name: str, the name of the query that is assigned to the layer
The query has to exist in the layer. If it doesn't, this function throws a ValueError.
Calling this function with query_name=None will remove the active query.
"""
cim = lyr.getDefinition('V2')
query = None
if query_name is not None:
queries = cim.featureTable.definitionFilterChoices
try:
query = {q.name: q.definitionExpression for q in queries}[query_name]
except KeyError:
raise ValueError("Query '{}' not found!".format(query_name)) from None
cim.featureTable.definitionExpression = query
cim.featureTable.definitionExpressionName = query_name
layer.setDefinition(cim)
lyr = arcpy.mp.ArcGISProject("current").activeMap.listLayers("Layer")[0]
change_definition_query(lyr, "Query 1")
change_definition_query(lyr, "DistanceNotNull")
change_definition_query(lyr, None)
change_definition_query(lyr, "ThisWillThrowAnError")
If you know the query names, you can do something like this:
def change_definition_query(layer, query_name):
"""Changes the definition query of a layer in ArcGIS Pro.
layer: arcpy.mp.Layer object, the layer that the query will be assigned to
query_name: str, the name of the query that is assigned to the layer
The query has to exist in the layer. If it doesn't, this function throws a ValueError.
Calling this function with query_name=None will remove the active query.
"""
cim = lyr.getDefinition('V2')
query = None
if query_name is not None:
queries = cim.featureTable.definitionFilterChoices
try:
query = {q.name: q.definitionExpression for q in queries}[query_name]
except KeyError:
raise ValueError("Query '{}' not found!".format(query_name)) from None
cim.featureTable.definitionExpression = query
cim.featureTable.definitionExpressionName = query_name
layer.setDefinition(cim)
lyr = arcpy.mp.ArcGISProject("current").activeMap.listLayers("Layer")[0]
change_definition_query(lyr, "Query 1")
change_definition_query(lyr, "DistanceNotNull")
change_definition_query(lyr, None)
change_definition_query(lyr, "ThisWillThrowAnError")
That is it! Have not used CIM before so this is a great introduction to it.
Thanks