Python to select page name and export current page using data driven pages

1497
9
01-06-2012 05:35 AM
MollyWatson
New Contributor III
I am creating a thematic mapbook in Python using some arcpy.mapping functions.  One map in the mapbook is a section map which utilizes data driven pages.  I would like the user defined parameter string of the section, township, range to be used to get to the correct data driven page.  Then I want to export only the current page to PDF.  The index field in the data driven pages is called SEC_FULL.  This is also the field used to name the pages.   I am getting stuck on how to write this in Python.  Here's what I have so far which does export the map as a PDF, but it is the wrong page. It is not finding the correct page from the user defined parameter.

#Import arcpy module
import string
import arcpy
import os
from arcpy import mapping
from os import sep as bs

# Script arguments
STR = arcpy.GetParameterAsText(0)

#Export Section Map
mxd = arcpy.mapping.MapDocument(r"V:\gislu\Planning\DeveloperKit\SectionMap.mxd")
df = mapping.ListDataFrames(mxd, "STR Map")[0]
ddp = mxd.dataDrivenPages
Layer = mapping.ListLayers(mxd, "Section Lines", df)[0]
clause = "\"SEC_FULL\" = '" + STR + "'"
arcpy.AddMessage("SELECTING: " + clause)
arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", clause)
arcpy.AddMessage(arcpy.GetCount_management(Layer).getOutput(0))
df.zoomToSelectedFeatures()
df.scale = 9300
arcpy.RefreshActiveView()
pageID = ddp.getPageIDFromName("SEC_FULL")
ddp.currentPageID = pageID
tmpPdf = r"V:\gislu\Planning\DeveloperKit\output\Section_temp.pdf"
ddp.exportToPDF(tmpPdf, 'CURRENT', resolution = 175)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf
Tags (2)
0 Kudos
9 Replies
JeffWard
Occasional Contributor III
I think your problem might be your clause string.  I tried typing the same text in the Python window and got a runtime error.  What do you want the clause value to be?
Jeff Ward
Summit County, Utah
0 Kudos
JeffWard
Occasional Contributor III
Scratch that, I needed to create the STR variable.
Jeff Ward
Summit County, Utah
0 Kudos
JeffWard
Occasional Contributor III
I don't think you need to go through all of the selection code.  If you replace "SEC_FULL" with the STR variable, it should change to the section entered by the user.

pageID = ddp.getPageIDFromName(STR)
ddp.currentPageID = pageID


Then you can get rid of all of the selectLayerByAttribute_management code.

As long as the user enters the section correctly it should work.  You might want to put some error handling code in to make sure they type the section correctly.
Jeff Ward
Summit County, Utah
0 Kudos
JeffWard
Occasional Contributor III
You will also want to use os.remove(tmpPdf) to delete the temporary pdf file.  The del command is for object cleanup which you have a bunch of - ddp, df, Layer, and mxd.  Where you don't need to do the layer selection, I think the only objects you need are the mxd and ddp objects.
Jeff Ward
Summit County, Utah
0 Kudos
JeffWard
Occasional Contributor III
#Import arcpy module
import string
import arcpy
import os
from arcpy import mapping
from os import sep as bs


# Script arguments
STR = arcpy.GetParameterAsText(0)

#Export Section Map
mxd = arcpy.mapping.MapDocument(r"V:\gislu\Planning\DeveloperKit\SectionMap.mxd")
ddp = mxd.dataDrivenPages
pageID = ddp.getPageIDFromName(STR)
ddp.currentPageID = pageID
tmpPdf = r"V:\gislu\Planning\DeveloperKit\output\Section_temp.pdf"
ddp.exportToPDF(tmpPdf, 'CURRENT', resolution = 175)
finalPdf.appendPages(tmpPdf)
os.remove(tmpPdf)
del mxd, ddp


the line:
finalPdf.appendPages(tmpPdf)

probably won't work because finalPdf isn't defined.  I am assuming you have it defined in code that you didn't include in your post.
Jeff Ward
Summit County, Utah
0 Kudos
MollyWatson
New Contributor III
Thanks Jeff. I do have the os.remove(tmpPDF) command at the end of the script to remove all of the temporary pdfs.  I also have defined the finalPDF at the beginning of the script which I did not include.  I tried your suggestion of removing the selection piece and replacing the SEC_FULL with the STR parameter. This is what the new code section looks like:

#Select and Zoom To Section Map, Export, and Append
mxd = arcpy.mapping.MapDocument(r"V:\gislu\Planning\DeveloperKit\SectionMap.mxd")
df = mapping.ListDataFrames(mxd, "STR Map")[0]
ddp = mxd.dataDrivenPages
pageID = ddp.getPageIDFromName(STR)
ddp.currentPageID = pageID
tmpPdf = r"V:\gislu\Planning\DeveloperKit\output\Section_temp.pdf"
ddp.exportToPDF(tmpPdf, 'CURRENT', resolution = 175)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf, ddp

I typed the user defined parameter exactly the same way it is shown in the SEC_FULL field in the attribute table.  I got the following error:

<type 'exceptions.ValueError'>: u'T1S R70W S30'
Failed to execute (MapbookSection).

Any other suggestions?
0 Kudos
JeffWard
Occasional Contributor III
Have you tried hard coding the text into the program?  It may be a problem with getting the STR variable from the GetParameterAsText function. 

pageID = ddp.getPageIDFromName('T1S R70W S30')


If that works maybe try placing your GetParameterAsText function in the str() function.

STR = str(arcpy.GetParameterAsText(0))
Jeff Ward
Summit County, Utah
0 Kudos
MollyWatson
New Contributor III
That worked! Thank you so much! The file size of the final mapbook is too large to attach. If you would like a copy of the entire script let me know.  It writes a title page, TOC, vicinity map, section map, and several thematic maps zoomed in on a particular property and appends it into 1 PDF. We got the idea from Clark County, WA here: http://gis.clark.wa.gov/gishome/WebOrders/?pid=goDevPacket
0 Kudos
JeffBarrette
Esri Regular Contributor
There are a couple of already existing samples on the resource center:

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=A910AB18-1422-2418-3418-3885D...

There is one called Print Data Driven Page(s).

Jeff
0 Kudos