Using Python to set a definition query in ArcPro

4823
8
01-18-2019 12:43 PM
AmyRoust
Occasional Contributor III

I'm rewriting a Python 2.x script so that it can be used in Pro, and I'm stuck on setting a definition query for a layer. Here's what I tried in Pro's Python Window:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]

for lyr in m.listLayers("Parcel"):
   if lyr.supports("DEFINITIONQUERY"):
   lyr.definitionQuery = 'JOINPIN = 201-02-0-00-00-012.01-0'

Then I thought maybe the definition query had to be written like a SQL statement, so I tried:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]

for lyr in m.listLayers("Parcel"):
   if lyr.supports("DEFINITIONQUERY"):
   lyr.definitionQuery = 'Join Pin is Equal to 201-02-0-00-00-012.01-0'

Neither option generated an error message, but no definition query is set on the Parcel layer.

Appreciate any help I can get! Documentation for moving from 2.x to 3.x for ArcPro doesn't have a lot of code examples that I can copy.

0 Kudos
8 Replies
BrittneyWhite1
Esri Contributor

Two things to check:

1. Is your lyr.definitionQuery line indented below your if line? If not, make sure it is indented.

2. What's the data type for your JOINPIN field? If it is a string, the value needs be in single quotes. It may look something like: lyr.definitionQuery = "JOINPIN = '201-02-0-00-00-012.01-0'".

You can also check out the SQL syntax section in the help: Write a query in the query builder—ArcGIS Pro | ArcGIS Desktop 

AmyRoust
Occasional Contributor III

My indents are good, and I tried the quotes like you have in your example (JOINPIN is a string). Still no luck. Other ideas?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

/blogs/dan_patterson/2016/08/14/script-formatting 

Amy....

Based on your pasted code, the last line in both examples need to be indented after the if statement.

It probably looks good on your screen but...

Code formatting helps ensure proper indentation from application to geonet

0 Kudos
AmyRoust
Occasional Contributor III

Good tip, thanks. Let's try this:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers("Parcel"):
     if lyr.supports("DEFINITIONQUERY"):
     lyr.definitionQuery = "JOINPIN = '201-02-0-00-00-012.01-0'"
0 Kudos
DarrenWiens2
MVP Honored Contributor

Have you tried adding print statements to ensure it's getting past the 'for' and 'if' how you expect? Also, check the lyr.definitionQuery before and after attempting to change.

0 Kudos
AmyRoust
Occasional Contributor III

Of all things, it appears that I just needed an asterisk after my layer name to make it work:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("agmap")[0]
for lyr in m.listLayers("Parcel*"):
    lyr.definitionQuery = "JOINPIN = '201-02-0-00-00-012.01-0'"
AmyRoust
Occasional Contributor III

So, I just upgraded to Pro 2.4.0 and the syntax that I originally marked as correct above is no longer working. This used to work:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("agmap")[0]
for lyr in m.listLayers("Parcel*"):
    lyr.definitionQuery = "JOINPIN = '201-02-0-00-00-012.01-0'"‍‍‍‍

Anyone have any suggestions on what might have changed? Maybe it has to do with the fact that you can have multiple definition queries in Pro and only have one active?

0 Kudos
PeterBandura
New Contributor

I do not know if it is still relevant, but I have version 2.5.2 and your code is working for me

0 Kudos