Pan and zoom all data frames to match the data driven data frame in ArcMap

A colleague had a map document containing 9 data frames showing various climate scenarios. He asked me for a script to pan and zoom each data frame to the extent and scale of the data driven data frame. The data driven pages were set up beforehand and must be enabled manually.

#
# @date 25/08/2015
# @author Cindy Williams
#
# Pans and zooms each data frame in an ArcMap document
# to the extent and scale of the data frame containing
# the data driven pages index layer before exporting.
#
# For use as a standalone script.
#
import arcpy
import os
map_doc = r"" # Your mxd here
mxd = arcpy.mapping.MapDocument(map_doc)
out_folder = r"" # Your folder here
if mxd.isDDPenabled: # Check if data driven pages are enabled in the map
ddp = mxd.dataDrivenPages
ddp_df = ddp.dataFrame # Get the data frame containing the data driven pages index layer
# Get all data frames besides the one containing the index layer
dfs = [df for df in arcpy.mapping.ListDataFrames(mxd) if not df.name == ddp_df.name]
# Loop over the data driven pages
for i in range(1, ddp.pageCount + 1):
ddp.currentPageID = i
cur_ddp = ddp.pageRow.getValue(ddp_name)
ddp_jpeg = os.path.join(out_folder, str(cur_ddp) + ".jpg")
for df in dfs:
df.extent = ddp_df.extent
df.scale = ddp_df.scale
arcpy.mapping.ExportToJPEG(mxd, ddp_jpeg, resolution=200)
print("Exported " + ddp_jpeg)
else:
print("Please enable Data Driven Pages before continuing.")

In Line 21, the data frame containing the data driven pages index layer is stored, so that it can be excluded from the list of all the data frames in the document in Line 24. When looping over the data driven pages for export, the scale and extent of each data frame are set to match those of the main data frame before writing to jpeg.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.