ArcMap

Combining Data Driven Pages with Python and arcpy.mapping

You can use arcpy.mapping, the Python module that allows you to interact with your map documents and layers, in conjunction with your data driven pages to do much more than make simple map books. For example, most complete map book products require title pages, overview maps, and sometimes pages of text or report information. In the past, putting these types of map books together could be time consuming, and usually required extra steps to assemble pages into a single document. At ArcGIS 10 putting together this type of map book product can be automated with a simple Python script.

The following example exports a title page, an overview map, and a series of data driven pages and combines them, along with a PDF file containing a list of contact information, into a single multi-page PDF file:

import arcpy, os, string
#Create final output PDF file
finalPdf = arcpy.mapping.PDFDocumentCreate(r”C:MyProjectMyAtlas.pdf”)

#Export Title page and append to final PDF file
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyTitlePage.mxd”)
tmpPdf = r”C:MyProjectMyTitlePage.pdf”
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf

#Export Overview Map and append to final PDF file
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyOverviewMap.mxd”)
tmpPdf = r”C:MyProjectMyOverviewMap.pdf”
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf

#Export Data Driven Pages and append to final PDF file
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlasPages.mxd”)
tmpPdf = r”C:MyProjectMyAtlasPages.pdf”
ddp = mxd.dataDrivenPages
ddp.exportToPDF(tmpPdf, “ALL”)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf

#Append Contact Information to final PDF file
finalPdf.appendPages(r”C:MyProjectContactInfo.pdf”)

#Update the properties of the final pdf to show thumbnail view
finalPdf.updateDocProperties(pdf_open_view=”USE_THUMBS”,
pdf_layout=”SINGLE_PAGE”)

del finalPdf

You can also use python in conjunction with data driven pages to cycle through each page, and perform one of the many operations supported by arcpy.mapping. For example, you can update a layer’s symbology, update some layout text, and export or print each map. This example demonstrates how to cycle through all your data driven pages and export them as PNG files:
import arcpy

#Specify the map document
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlasPages.mxd”)

#Export each of the data driven pages
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
print “Exporting page {0} of {1}”.format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
arcpy.mapping.ExportToPNG(mxd, r”C:MyProjectOutputMyAtlas_Page” + str(pageNum) + “.png”)
del mxd

See the following blog entry for more info on exporting and printing using arcpy.mapping: http://blogs.esri.com/Dev/blogs/arcgisdesktop/archive/2010/02/01/ArcPy-and-Geoprocessing_2620_-it_1920_s-for-Developers-too.aspx

arcpy.mapping opens up many possibilities for the types of map books you can create. For example, you can create a thematic atlas with multiple pages specifying a different theme on each page. The following example zooms to a selected parcel, toggles on different layer visibility and exports the layout for multiple themes in order to create a parcel report with a soil map, a flood map and a zoning map:

import arcpy, os

#Specify output path and final output PDF
outPath = r”C:MyProjectoutput\”
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + “ParcelReport.pdf”)

#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyParcelMap.mxd”)
df = arcpy.mapping.ListDataFrames(mxd, “Layers”)[0]

#Select a parcel using the LocAddress attribute and zoom to selected
parcelLayer = arcpy.mapping.ListLayers(mxd, “Parcels”, df)[0]
arcpy.SelectLayerByAttribute_management(parcelLayer, “NEW_SELECTION”, “”LocAddress” = ’519 Main St’”)
df.zoomToSelectedFeatures()

#Turn on visibility for each theme and export the page
lyrList = [“Soils”, “Floodplains”, “Zones”]
for lyrName in lyrList:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

     #Export each theme to a temporary PDF and append to the final PDF
tmpPdf = outPath + lyrName + “_temp.pdf”
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
lyr.visible = False
del lyr, tmpPdf
del mxd, df, finalPdf

Frequently map books require a separate layout for a left and right page in order to create a wider margin in the middle when the pages are bound together. Or some map books require page layouts using different orientations, where some of the maps are portrait and some are landscape.   In this example we use two layouts, one for the left and one for the right. Both layouts have multiple pages created using data driven pages. The following script exports all the left pages and all the right pages and assembles them together into a single PDF file:

import arcpy, os

# Specify left and right map documents and create final output PDF file
mxdLeft = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlas_left.mxd”)
mxdRight = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlas_right.mxd”)
finalPdf = arcpy.mapping.PDFDocumentCreate(r”C:MyProjectMyAtlas.pdf”)

#Export left (odd) pages to temporary PDF files
for pgNumLeft in range(1, mxdLeft.dataDrivenPages.pageCount + 1, 2):
mxdLeft.dataDrivenPages.currentPageID = pgNumLeft
arcpy.mapping.ExportToPDF(mxdLeft, r”C:MyProjectTemp_Page” + str(pgNumLeft) + “.pdf”)

#Export right (even) pages to temporary PDF files
for pgNumRight in range(2, mxdRight.dataDrivenPages.pageCount + 1, 2):
mxdRight.dataDrivenPages.currentPageID = pgNumRight
arcpy.mapping.ExportToPDF(mxdRight, r”C:DataDemosHamiltonCoDemooutputAtlas_Page” + str(pgNumRight) + “.pdf”)

#Append all pages into final PDF file and clean up temporary PDF files
for pgNum in range(1, mxdLeft.dataDrivenPages.pageCount + 1,):
tmpPdf = r”C:DataDemosHamiltonCoDemooutputAtlas_Page” + str(pgNum) + “.pdf”
if os.path.exists(tmpPdf):
finalPdf.appendPages (tmpPdf)
os.remove(tmpPdf)
del mxdLeft, mxdRight, finalPdf, tmpPdf

When you combine the multiple pages, and dynamic layout elements from data driven pages along with the things you can do with arcpy.mapping there are many possibilities. Arcpy.mapping makes it easy to write powerful Python scripts that automate many of the redundant map book and atlas compilation and production tasks.

These are just a small sample.  There is a lot more you can do with arcpy.mapping.  See an overview at:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000032000000.htm

You can find additional sample scripts at Code Gallery

Next Article

Ten ways to get the most out of ArcGIS Living Atlas

Read this article