christi_nelson

HELP! arcpy.mapping select records loop, export each to PDF map

Discussion created by christi_nelson on Feb 8, 2013
Latest reply on Feb 11, 2013 by mzcoyle
hi ALL,


I posted my question under map automation, but it really is a python question having to do with arcpy.mapping.  I am creating a tool in python that will automate (essentially) the following process:

Find the first layer in the map document (which is the parcelLayer)
Create a feature layer from this layer so that it can be selected
Get a cursor to go thru the table and find the parcel number field "APN_NU_1"
For each parcel number, select the parcel and export the map to a pdf
Repeat until there are no more records to go thru.

My script seems to be successfully iterating thru the records fine, but the pdf is not displaying the selection on the selected parcel boundary. In other words, the parcel that is selected should be highlighted on the map and then exported to pdf.

What am I missing? I have provided the code in the text and have also attached the map document and geodatabase. My parcel feature class (for testing) only has two records, fyi.

#import modules and define workspace
import arcpy
import os
import traceback

workspace = arcpy.env.workspace = r"C:/temp"
print workspace


arcpy.env.overwriteOutput = True

#Define map document
mxDoc = arcpy.mapping.MapDocument(r"C:/temp/PSL_Parcel.mxd")
print mxDoc

#List the first dataframe (named Layers) in the map document
df = arcpy.mapping.ListDataFrames(mxDoc, "Layers") [0]


#List first map layer (which is the parcels layer) in dataframe
parcelLayer = arcpy.mapping.ListLayers(mxDoc,"",df) [0]
print parcelLayer
parcelField = "APN_NU_1"

#Create a feature layer to make selections on
selectingLayer = arcpy.MakeFeatureLayer_management(parcelLayer, "parcelLayer_lyr")


#Set up cursor:
rows = arcpy.SearchCursor(selectingLayer)

recordsCounted = 0

#Find the field, select each record and get values 
for row in rows:
    APN = row.getValue(parcelField)
    whereClause = "APN_NU_1 = '%s'" % APN

    arcpy.management.SelectLayerByAttribute(selectingLayer, "NEW_SELECTION", whereClause)
    print row.getValue(parcelField)



    recordsCounted += 1

    
#Export map with selected record to PDF
    pdfLocation = 'C:\\temp\\'
    
    pdfName = pdfLocation + APN + '.pdf'
    
    arcpy.mapping.ExportToPDF(mxDoc, pdfName)


    
print recordsCounted                              
del row, rows


#Clean up feature layers from memory and to debug and or rerun script
arcpy.Delete_management(selectingLayer)


Thanks in advance for your help,
Christi

PS. to get the attacments go to the other thread at http://forums.arcgis.com/threads/77304-HELP!python-select-records-and-export-map2PDF?p=271478#post271478

PSS. I was only able to attach a .zip containing the shapefile for the parcels, rather than the geodatabase and feature class. So, the path for this would have to change in the script and you would have to add it to the map as the first layer in the TOC.

Also, save all to C:/temp!

Outcomes