Finally had a chance to take a stab at this script. Below is what I came up with, but first, some comments and explanation.
- Use the ListFields function and the
OID
type to return the name of the OID field. This will give you the unique identifiers of the selected features. It takes the guesswork out of getting the spelling right. It also avoids the issue of having joined tables together at some point and having multiple OID fields, but not knowing which one is the official one.
- I added an informational function to help with building the queries. The
AddFieldDelimiters
function will put in the appropriate field delimiters depending on whether the field is coming from a shapefile, FGDB, PGDB, or other source. This eliminates a lot of the chance for error. AddFieldDelimiters(arcpy)
- When setting up your Search Cursor, limit the number of fields that you are returning. This improves performance. Also, I switched to using the Data Access cursor which is specifically for returning attribute data. Data Access SearchCursor
- In both situations where you were building a query string, I moved it out of the destination function, into its own variable. This let's you build the query separately without having to deal with the surrounding function. It also let's you utilize it at different times if need be. This help reference is very useful: SQL reference for query expressions used in ArcGIS
- I moved the setting of the scale out of the
for
loop. Since you want the same scale in every export, there is no reason to zoom to the feature, then set the scale, then pan to recenter the extent. Zoom to the correct scale once, then pan around to the appropriate extent. Make sure that your coordinate systems are set properly for the data frame and also for your layer.
- In your original script, in order to select the feature, you were building a query using the
FID
field, and matching it to the count of features in your SearchCursor
. This would not work because the FID
of a feature does not change even when it is in a SearchCursor. I set up your SearchCursor to return the OID
value which I then used as part of the Select query. Using as many functions as possible to return values, instead of entering them by hand, reduces the chance for error. This is relevant with the field name of the OID
field, and the field delimiters.
- At the end, I included the code provided by @Tom to append all of the exported ".pdf" files to a single ".pdf". This is from his answer to your other questions: Possible to use wildcard in .pdf Append?
On to the code:
mxd = arcpy.mapping.MapDocument("C:\\temp\\Graffiti_DM3.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
graffitilyr=arcpy.mapping.ListLayers(mxd, "*", df)[0]
#Find OID Field Name
oidfieldname = arcpy.ListFields(graffitilyr,"","OID")[0].name
#Longer but possibly more robust method
#oidfield = arcpy.ListFields(graffitilyr,"","OID")[0]
#oidfieldname = oidfield.name
queryfield = "Incident_Z"
newquery = arcpy.AddFieldDelimiters(graffitilyr,queryfield) + " = 10003"
graffitilyr.definitionQuery=newquery
#Fill List of features - Limit attribute return to OID field only for speed. Include other fields as necessary.
features = arcpy.da.SearchCursor(graffitilyr,"OID@")
count = 1
print "let's get started"
#Set Dataframe Scale
#Parameter instructions require use of numerical(doubl) value, so no quotes are necessary.
df.scale = 1000
for feature in features:
df.rotation = "0"
#Build Select Query
selectexpression = arcpy.AddFieldDelimiters(graffitilyr,oidfieldname) + " = " + feature[0]
arcpy.SelectLayerByAttribute_management(graffitilyr, "NEW_SELECTION", selectexpression)
#Pan to Selected Feature Extent
df.panToExtent(graffitilyr.getSelectedExtent())
arcpy.RefreshActiveView()
outpdf="C:\\temp\\" + str(count) + ".pdf"
arcpy.mapping.ExportToPDF(mxd,outpdf)
#Add pdf to list for append to complete pdf
pdfList.append(outpdf)
#Increment count
count += 1
#Create new pdf to consolidate all pages
pdfpath="C:\\temp\\pdfbook.pdf"
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfpath)
#Append all pdf pages in list to one new pdf
for aPdf in pdfList:
pdfDoc.appendPages(aPdf)
pdfDoc.saveAndClose()
del features
del pdfDoc
Give this a shot and see how it works. The problems that I could see arising may relate to capitalization of function names. I sometimes miss which letters need to be capitalized, and Python can be finicky about this.
Hopefully this process has helped improve your knowledge of Python and arcpy for ArcGIS. There is a lot of utility in learning both. I encourage you to check out the help resources for ArcGIS:
Reviewing the documentation and code samples in these places will help you immensely if you are interested in becoming better at Python.
for feature in features:
loop that are causing your issues. The reason you are getting the same image for each feature is that you are not changing the zoom extent when you rotate through your features. This would happen in thedf.extent
function that you have commented out. Figure out how to make that adjust and you should start to see differences. Also, your scale isn't being set, so this could be affecting the labeling at output. I encourage you, as in your other question, to look at the help, and try to better understand arcpy coding.