Exporting Data Driven Pages in Extent

With the introduction of Data Driven Pages, ESRI has made creating Map Books a breeze. You create the index layer which represents a grid, enable data driven pages, export to PDF and you have Map Book. Details here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00sr00000006000000

But what if you have an mxd with Data Driven Pages already set up, and you need to export only certain pages which fall within an extent? Of course, you could open ArcMap, figure out what page numbers you need, and export them that way. Another way is to write a small Python script that does the figuring out of page numbers for you. Once finished you can use the script as is, you could put it in a script tool, model, or call it from another application and run it without the need to even open ArcMap.

Start by importing the arcpy library and setting up your workspace and scratchworkspace:

import arcpy
mxd = arcpy.mapping.MapDocument("C:\Users\codergrl\Maps\MapBook.mxd")
scratchWorkspace = "C:\Users\codergrl\Documents\ArcGIS\Default.gdb"

Define your dataframe and get the extent:

df = arcpy.mapping.ListDataFrames(mxd)[0]
extent = df.extent

Create an array of points using the 4 points of the extent polygon. Don’t forget to add the first point again at the end to close your polygon:

pointsArray = arcpy.Array()

Using the array of vertices above, create a polygon and save it into your scratch database:

extentPoly = arcpy.Polygon(pointsArray)
arcpy.CopyFeatures_management(extentPoly, "%s\extentPoly" %(scratchWorkspace))

Define your data driven pages and get the index layer:

ddp = mxd.dataDrivenPages
indexLayer = mxd.dataDrivenPages.indexLayer

To retrieve the data driven pages that are in the extent, intersect the index layer with the polygon layer created above. To do this, call the Select By Location tool from the Management toolbox and pass it the 2 layers, relationship, and what to do with the result. In this case, I’m adding the result to an existing selection:

arcpy.SelectLayerByLocation_management(indexLayer, "INTERSECT", "%s\extentPoly" %(scratchWorkspace), "", "ADD_TO_SELECTION")

And the last step, export your selected data driven pages:

ddp.exportToPDF(r"C:\Users\codergrl\Exports\MapBook.pdf", "SELECTED", "")
Posted in Blog, Python Tagged with: , , , , , , , , ,