Select to view content in your preferred language

Use Python to set definition query in Pro issue

923
7
Jump to solution
09-12-2023 08:49 AM
ScoutStanley
Occasional Contributor

Hi, 

I am trying to set up definition query in Pro. Purpose of the script is that it loops through table and creates new map based on the row value. However, I can't get my definition query to work. If I specify a specific pin number  such as "DGCO.GIS_OWNER.ParcelQueue.JOINPIN = 56" the script will work fine, but I am needing to run through multiple row values, so the script will crash after "row number 56". Any ideas on how to get definition query to work through loop?

#Set definition query on Parcels using PIN
        for lyr in m.listLayers("Parcel*"):
            if lyr.supports("DefinitionQuery"):
                lyr.definitionQuery = "DGCO.GIS_OWNER.ParcelQueue.JOINPIN = " + PIN

Below is PIN defined, which works and pulls the correct row value. However, my definition query above won't recognize it. 

PIN = i.getValue(SearchField)
0 Kudos
2 Solutions

Accepted Solutions
ScoutStanley
Occasional Contributor

Issue solved... had to include "'" between PIN. Example below

#Set definition query on Parcels using PIN
        for lyr in m.listLayers("Parcel"):
            if lyr.supports("DefinitionQuery"):
                lyr.definitionQuery = "DGCO.GIS_OWNER.ParcelQueue.JOINPIN = " + "'" + PIN + "'"

 

View solution in original post

0 Kudos
BlakeTerhune
MVP Regular Contributor

I highly recommend levering string literals in Python with f""

PEP 498 – Literal String Interpolation | peps.python.org

lyr.definitionQuery = f"DGCO.GIS_OWNER.ParcelQueue.JOINPIN = '{PIN}'"

View solution in original post

7 Replies
ScoutStanley
Occasional Contributor

Issue solved... had to include "'" between PIN. Example below

#Set definition query on Parcels using PIN
        for lyr in m.listLayers("Parcel"):
            if lyr.supports("DefinitionQuery"):
                lyr.definitionQuery = "DGCO.GIS_OWNER.ParcelQueue.JOINPIN = " + "'" + PIN + "'"

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

I highly recommend levering string literals in Python with f""

PEP 498 – Literal String Interpolation | peps.python.org

lyr.definitionQuery = f"DGCO.GIS_OWNER.ParcelQueue.JOINPIN = '{PIN}'"
RhettZufelt
MVP Notable Contributor

Agreed.  However, by looking at the database name in the path, I assumed this was most likely pre python 3 and the .format works for both.

R_

0 Kudos
BlakeTerhune
MVP Regular Contributor

@RhettZufelt wrote:

Agreed.  However, by looking at the database name in the path, I assumed this was most likely pre python 3 and the .format works for both.

R_


True! This only works in Python 3. Since @ScoutStanley mentioned Pro, I assumed Python 3.

0 Kudos
RhettZufelt
MVP Notable Contributor

Looks like maybe I should have read that better, skimmed past the pro.

Do love the f strings, makes query expressions much easier.

R_

0 Kudos
ScoutStanley
Occasional Contributor

Oh nice, I hadn't learned about f, added it to the script! Thanks for the tip! 

0 Kudos
RhettZufelt
MVP Notable Contributor

Assuming PIN is numeric, maybe this will work?

lyr.definitionQuery = "DGCO.GIS_OWNER.ParcelQueue.JOINPIN = {0}".format(PIN)

R_

0 Kudos