[GIS] How to Export to PDF after zooming to extent of each feature that meets SQL where clause

arcgis-10.1arcgis-desktoparcpycursorsql

I realize that I've asked two similar questions recently, but following suggestions in the comment chain, I'll make this a separate question (separate as it is not the no output problem in ArcPy: (SearchCursor/export to tiff ) with no output or the produces empty feature problem described Arcpy: SQL error in select_analysis produces empty feature). To be clear, the question is explicitly stated as: How to print a .pdf layout for each record that meets a where clause.

Based on suggestions from @PolyGeo, I'm trying the approach of using a definitionQuery to get the values and then a combination of a search cursor with a panToExtent to print each record. Here is the code:

import arcpy

mxd = arcpy.mapping.MapDocument("C:\\temp\\Graffiti_DM1.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
outpdf="C:\\temp\\1003_zip4.pdf"
where_clause='"Incident_Z" = ' + "'10033'"
graffitidata="C:\\temp\\itscomplicated.shp"

cursor = arcpy.UpdateCursor(graffitidata)

graffitilyr=arcpy.mapping.ListLayers(mxd, "itscomplicated", df)[0]
graffitilyr.definitionQuery=where_clause
for cur in cursor:
    cur.rotation = "0"
    cur.scale = "1000"
    outFile = expdir + "\\" + cur.name + ".pdf"
    cur.panToExtent(graffitilyr.getSelectedExtent())
    arcpy.mapping.ExportToPDF(mxd,outpdf)


del cursor

However, I'm running into the following problem.

Traceback (most recent call last):
  File "C:\temp\defquery_for_in.py", line 15, in <module>
    cur.rotation = "0"
  File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
    return setattr(self._arc_object, attr, ao)
RuntimeError: ERROR 999999: Error executing function.

If anyone has any suggestions concerning the above error, or suggestions concerning the general setup of the code (specifically the addition of the search cursor within the where clause) I'd be greatly appreciateive.

This is the current version of what I'm working with as suggested by @PolyGeo, however it is producing pdfs that have the layout but no data (see comment below).

import arcpy
mxd = arcpy.mapping.MapDocument("C:\\temp\\Graffiti_DM1.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
graffitilyr=arcpy.mapping.ListLayers(mxd, "itscomplicated", df)[0]
where_clause = '"Incident_Z" = ' + "'10003'"
graffitilyr.definitionQuery=where_clause
features = arcpy.SearchCursor(graffitilyr)
count = 1


for feature in features:
        df.rotation = "0"
        df.extent = graffitilyr.getSelectedExtent(True)
        df.scale = "1000"
        outpdf="C:\\temp\\" + str(count) + ".pdf"
        arcpy.mapping.ExportToPDF(mxd,outpdf)
        count = count+1

del features

Best Answer

I think the following (untested) code will do very close to what you are asking.

It assumes that you have a layer in the first data frame of your MXD called "itscomplicated".

If it "just works" without errors but needs modifications to try and tweak it to precisely what you need be sure to keep backups that you can revert to as you modify then test each part at a time.

import arcpy

mxd = arcpy.mapping.MapDocument("C:\\temp\\Graffiti_DM1.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
graffitilyr=arcpy.mapping.ListLayers(mxd, "itscomplicated", df)[0]
where_clause = '"Incident_Z" = ' + "'10033'"  # Assume this will select multiple polygons  
graffitilyr.definitionQuery=where_clause
features = arcpy.SearchCursor(graffitilyr)
count = 1
for feature in features:
    df.rotation = "0"
    outFile = expdir + "\\" + cur.name + ".pdf"
    df.extent = graffitilyr.getSelectedExtent(False)
    df.scale = "1000"
    outpdf="C:\\temp\\pdf" + str(count) + ".pdf"
    arcpy.mapping.ExportToPDF(mxd,outpdf)
    count = count+1