Python script using definition query, zoom to feature, and export to PDF.

146
6
10-29-2020 11:14 AM
New Contributor

Hi,

I am having an issue with getting this script to work. I wrote this a while ago and when I first wrote it, it worked perfectly. Now I am trying to use it again and I can't get it to work. 

I need a PDF zoomed to each feature (Parcel), only showing one parcel at a time. The script should make a definition query for each parcel ID, zoom to that parcel, and export the map as a PDF. Everything works except it won't zoom in properly.

Can anyone figure out why this might be?

I have attached the code.

Thanks,

Erin

Code:

import arcpy
from arcpy.mapping import *


mapDoc = arcpy.mapping.MapDocument(r"C:\Users\CulpE\Desktop\Parcels.mxd")
dataFrames = ListDataFrames(mapDoc)
dataFrame = dataFrames[0]
adjParcel = r"C:\Users\CulpE\Desktop\parcels.shp"
parcel_layer = "Parcel"
lyrs = arcpy.mapping.ListLayers(mapDoc, '', dataFrame)

fieldList = ['PROP_ID']

for lyr in lyrs:
   if lyr.name == parcel_layer:
      with arcpy.da.SearchCursor(lyr, fieldList) as cursor:
         for row in cursor:
            lyr.definitionQuery = "PROP_ID ='" + str(row[0]) + "'"
            print "PROP_ID='" + str(row[0]) + "'"
            ext = lyr.getExtent()
            dataFrame.extent = ext
            dataFrame.scale *= 1.5
            ExportToPDF (mapDoc, r"C:\Users\CulpE\Desktop\Parcel_" + str(row[0]) + ".pdf", "PAGE_LAYOUT")

del mapDoc
print "Closed map document"

Reply
0 Kudos
6 Replies
MVP Regular Contributor

Can you post your code on here rather than a zip file please?

Reply
0 Kudos
New Contributor

I updated the post with the script.

Reply
0 Kudos
MVP Regular Contributor

Had a gander, always helps to debug with some print statements as you go along to identify what's happening as it runs.  Also print the objects being returned to check on them.

I' not sure on this definition query:

lyr.definitionQuery = "PROP_ID ='" + str(row[0]) + "'"

I think it should be (if PROP_ID field is a string?):

"'" + "PROP_ID = " + "'" + str(row[0]) + "'" + "'"

should be (if PROP_ID field is a number?):

'"' + "PROP_ID = " + str(row[0])  + "'"

my brain has given up on me, but basically the query should be formed to look like this string i.e. when printed it still retains a single quote surrounding it:

definition_query = 'PROP_ID = 265FG'

or if PROP_ID field is a number:

definition_query = 'PROP_ID = 265'

I'd recommend forming definition_query as a variable, print it to make sure it looks OK, then assign it to lyr.definitionQuery.

also NB PROP_ID would also be quoted in the SQL query if a shapefile just to add some fun to the mix.

Reply
0 Kudos
New Contributor

Thank you so much! I will try this!

Reply
0 Kudos
Esteemed Contributor

Does the script zoom in on any parcels and just has an issue with certain parcels?

Reply
0 Kudos
New Contributor

 No, it seems to zoom out to all parcels for half of the. and then zooms to the same parcel for the other half.

Reply
0 Kudos