Thursday, January 3, 2013

ArcPy 10.1 - Multiple Data Frames and Data Driven Pages

Data driven pages is a great way to automate map production over a given area, and it works right out of the box with a single data frame.  If you have 2 or more data frames though, you will need to control the movement of each data frame object by performing some sort of coding (ArcObjects or ArcPy).

A simple way to move multiple data frames is to:

  1. Get a list of all data frames
  2. Identify the parent data frame
  3. Move the extents of the other data frame
Sounds simple?  Well it is.

To get started, get a reference to the map document

mxd = arcpy.mapping.MapDocument("CURRENT")

Change "CURRENT"  to a path if not run in ArcMap.  Then list all the data frames

dfs = arcpy.mapping.ListDataFrames(mxd)

Now how do you know what data frame is the parent data frame or what data frame is a child data frame? Luckily at 10.1, the data driven page object provides a property called dataFrame, which returns the DataFrame object.  At 10.0, you would either use names or indexing values to determine which is the parent or not, but this example is for 10.1.

ddp = mxd.dataDrivePages
parentDF = ddp.dataFrame

Next step is to pan the other data frames to the extent of the parent data frame's extent.


for pageNum in range(1, ddp.pageCount + 1):

   for df in dfs:
      ddp.currentPageID = pageNum
      row = ddp.pageRow
      extent = row.Shape.extent
      if (df.name == parentDF.name) and (df.scale == parentDF.scale):
         print 'I found the parent frame'
      else:
         df.panToExtent(extent)
   arcpy.mapping.ExportToPNG(mxd, r"C:\Project\OutPut\ParcelAtlas_" + str(pageNum) + ".png")

Here we have a bunch of code that loops through each data driven page area and then modifies each underlying data frame that isn't the parent.  After the data frames are changes, the code then exports the maps to PNG to the '...\output' folder with reference to the page number.

Enjoy