Select to view content in your preferred language

Need to change a layer's definition query in two maps (main and inset) of a single layout.

777
6
07-26-2023 10:10 AM
Shauna-RaeBrown
Occasional Contributor II

I've written a python script (arcpy) that takes the text from an email containing a ticket number and lat/long coordinates.  I've been able to extract the data containing the ticket number, lat/long coordinates that form a rectangle, and other data.  This data is a list that updates a featureclass in a file gdb.  I've also updated/changed the layout's name to reflect the ticket number.  The feature class/layer is used in both the main map and a reference or inset map.  The aprx has 2 maps, and one layout. 

What I can't figure out is how to change the layer's definition query, so that I can zoom to or pan to the new feature.  I think I need to use the Cartographic Information Model (cim), but I'm not sure how to change the layer's definition query.

I'm working in ArcGIS Pro 2.9

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

CIM, layer class, definition query is read/write

Layer—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
Shauna-RaeBrown
Occasional Contributor II

@DanPatterson Thanks for the link!  Maybe I missed something.

Here's the link for Pro 2.9 - https://pro.arcgis.com/en/pro-app/2.9/arcpy/mapping/layer-class.htm

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

code examples at the bottom if that is what you are looking for


... sort of retired...
0 Kudos
Shauna-RaeBrown
Occasional Contributor II

@DanPatterson, Thanks for your help.  I've been working on adapting example 4, but I keep getting an error.  I think it has to do with the ticketNo = "2023070700400.002".  That field in my feature class is a text field.  I've tried tickeNo = str(2023070700400.002) and I get the same error.  Of course it could have to do with my sql query.  I'm stumped.  Thanks for ANY help that you can offer.

aprx = arcpy.mp.ArcGISProject("CURRENT")
ticketNo = "2023070700400.002"
m = aprx.listMaps()[0]
l = m.listLayers("AZ811 BlueStake")[0]
if l.supports('DefinitionQuery'):
  #Get the list of definition queries
  dql = l.listDefinitionQueries('Query 1')[0]
  #Clear active definition query, otherwise the update will fail if there is already an active query
  for dq in dql:
    dq['isActive'] = False
  #Create a new definition query and append it to the list
  dql.append({'name': 'Query 1', 'sql': "tktNum = ticketNo", 'isActive': True})
  #Update the definition queries with the newly modified list
  l.updateDefinitionQueries(dql)

Traceback (most recent call last):
  File "<string>", line 10, in <module>
TypeError: 'str' object does not support item assignment

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

You changed it from a list to a string in line 10 so you can't cycle through it as you appear to want to do.

Their line is

dql = l.listDefinitionQueries()

... sort of retired...
0 Kudos
Shauna-RaeBrown
Occasional Contributor II

I mostly figured it out!  However, it created another Query 1 with the correct definition query, and it added the definition query to both maps.  See my code below.

aprx = arcpy.mp.ArcGISProject("CURRENT")
ticketNo = "2023070700400.002"
fieldname = "tktNum"
newName = str.format(ticketNo)
for m in aprx.listMaps():
  for l in m.listLayers("AZ811 BlueStake"):
    if l.supports('DefinitionQuery'):
      #Get the list of definition queries
      dql = l.listDefinitionQueries('Query 1')
      #Clear active definition query, update will fail if there is already an active query
      for dq in dql:
        dq['isActive'] = False
      #Create a new definition query and append it to the list
      dql.append({'name': 'Query 1', 'sql': """{} = '{}'""".format(fieldname,newName), 'isActive': True})
      #Update the definition queries with the newly modified list
      l.updateDefinitionQueries(dql)

 Now, I just need to figure out how to have just the one definition query on the layer.  Thanks for your help @DanPatterson !

0 Kudos