Programmatically select layer definition query

767
2
Jump to solution
11-08-2021 11:21 AM
Labels (1)
DeanAnderson2
Occasional Contributor II

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. 

DeanAnderson2_0-1636399086048.png

 

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

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")

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

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")

 


Have a great day!
Johannes
DeanAnderson2
Occasional Contributor II

That is it!  Have not used CIM before so this is a great introduction to it. 

 

Thanks 

 

0 Kudos