Select to view content in your preferred language

HELP!python select records and export map2PDF

3431
5
Jump to solution
02-08-2013 11:13 AM
by Anonymous User
Not applicable
Original User: christi_nelson

Hi,

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.  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!
0 Kudos
1 Solution

Accepted Solutions
ChristiNelson1
Deactivated User
I determined that my SQL statement in my whereclause was incorrect.  I added a couple of print statements to troubleshoot and was successfully able to change and run it.  Here is the corrected whereClause (SQL statement).

for row in rows:         print row.getValue('APN_NU_1')         APN = row.getValue('APN_NU_1')         print "||" + APN + "||"         whereClause = "APN_NU_1 = '" + APN + "'"         print "|" + whereClause + "|"         arcpy.SelectLayerByAttribute_management(parcelLayer, "NEW_SELECTION", whereClause)  


I'm still not sure why I didn't have to make a feature layer before my selection, but I did figure out what the True/False means in the layer.getSelectedExtent(True) means:

getSelectedExtent ({symbolized_extent})

A value of True will return the layer's symbolized extent; otherwise, it will return the geometric extent. The symbolized extent takes into account the area the symbology covers so that it does not get cut off by the data frame's boundary.

(The default value is True)

View solution in original post

0 Kudos
5 Replies
by Anonymous User
Not applicable
Original User: JeffMoulds

Try this...

For each county (in your case, parcel) that matches the where clause in the Search Cursor, I select it, then set the extent of the data frame to be that of the selected feature (plus 10%), then I export it it to pdf.

import arcpy 
mxd = arcpy.mapping.MapDocument(r"C:\Temp\temp.mxd")
df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]
Layer = arcpy.mapping.ListLayers(mxd, "*Counties*")[0]
county = 'Red River'
sql1 = "\"NAME\" = " + "\'" + county + "\'"
rows = arcpy.SearchCursor(Layer, sql1)
for row in rows:
    print row.getValue('OBJECTID')
    oid = str(row.getValue('OBJECTID'))
    sql2 = "\"OBJECTID\" = " + oid
    arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", sql2)
    df.extent = Layer.getSelectedExtent(False)
    df.scale = df.scale * 1.1
    arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\RedRiver" + oid + ".pdf")
0 Kudos
ChristiNelson1
Deactivated User
Try this...

For each county (in your case, parcel) that matches the where clause in the Search Cursor, I select it, then set the extent of the data frame to be that of the selected feature (plus 10%), then I export it it to pdf.

import arcpy 
mxd = arcpy.mapping.MapDocument(r"C:\Temp\temp.mxd")
df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]
Layer = arcpy.mapping.ListLayers(mxd, "*Counties*")[0]
county = 'Red River'
sql1 = "\"NAME\" = " + "\'" + county + "\'"
rows = arcpy.SearchCursor(Layer, sql1)
for row in rows:
    print row.getValue('OBJECTID')
    oid = str(row.getValue('OBJECTID'))
    sql2 = "\"OBJECTID\" = " + oid
    arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", sql2)
    df.extent = Layer.getSelectedExtent(False)
    df.scale = df.scale * 1.1
    arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\RedRiver" + oid + ".pdf")


Thanks, Jeff I will try this and let you know.  In the mean time, can you please tell me why you would set the Layer.getSelectedExtent to False?  What does this do and what would happen if you set it to True?
0 Kudos
ChristiNelson1
Deactivated User
Hi Jeff,

I tried your method and it definitely runs and creates the pdfs.  But it doesn't zoom into the parcel and the pdf doesnt show the highlighted (selected) parcel.  See attached .pdf.  Ideas?  I used the script below. 

Additional questions regarding your script: 
1.  Why did you not have to create a feature layer (MakeFeatureLayerManagement) before performing a selectbyattribute?  All the documentation I've read says you must make a feature layer first.
2. What do the *asterisks* do in your arpy.ListLayers (mxd, "*Counties*") script?
3. I also ran the script below with  df.extent = parcelLayer.getSelectedExtent (True) but it didn't make a difference. What does the True/False do?

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

workspace = arcpy.env.workspace = r"E:/Projects/PSL"
print workspace


arcpy.env.overwriteOutput = True

#Define map document
mxDoc = arcpy.mapping.MapDocument(r"E:/Projects/PSL/PSL_Parcel.mxd")
print mxDoc

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

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



#Set up a Search Cursor to go thru the attribute table and get row values to update the layout text 
#First set up variable, then read the row values and verify
recordsCounted = 0
rows = arcpy.SearchCursor(parcelLayer)
for row in rows:
        print row.getValue('APN_NU_1')
        APN = row.getValue('APN_NU_1')

        whereClause = """ 'APN_NU_1' = APN """
        
        arcpy.SelectLayerByAttribute_management(parcelLayer, "NEW_SELECTION", whereClause)
        df.extent = parcelLayer.getSelectedExtent (False)
        df.scale = df.scale * 1.1
        
        
        recordsCounted += 1

        pdfLocation = 'E:\\Projects\\PSL\\temp\\' 
        pdfName = pdfLocation + APN + '.pdf'
    
        arcpy.mapping.ExportToPDF(mxDoc, pdfName)
0 Kudos
by Anonymous User
Not applicable
Original User: christi_nelson

pdf is attached

PS - I am running ArcGIS 10.0  (ArcInfo level)
0 Kudos
ChristiNelson1
Deactivated User
I determined that my SQL statement in my whereclause was incorrect.  I added a couple of print statements to troubleshoot and was successfully able to change and run it.  Here is the corrected whereClause (SQL statement).

for row in rows:         print row.getValue('APN_NU_1')         APN = row.getValue('APN_NU_1')         print "||" + APN + "||"         whereClause = "APN_NU_1 = '" + APN + "'"         print "|" + whereClause + "|"         arcpy.SelectLayerByAttribute_management(parcelLayer, "NEW_SELECTION", whereClause)  


I'm still not sure why I didn't have to make a feature layer before my selection, but I did figure out what the True/False means in the layer.getSelectedExtent(True) means:

getSelectedExtent ({symbolized_extent})

A value of True will return the layer's symbolized extent; otherwise, it will return the geometric extent. The symbolized extent takes into account the area the symbology covers so that it does not get cut off by the data frame's boundary.

(The default value is True)
0 Kudos