Select to view content in your preferred language

Export Bookmarked Frame + Data Driven Frame Python

662
2
Jump to solution
12-04-2013 05:41 PM
ShaneHogg
Deactivated User
Hi, Been stuck on this for a few hours wondering if anything can point me in the right direction, I am pretty new to Python.

What I have is 2 data frames in an MXD.  1 data frame is controlled using data drive pages and the other frame is controlled using bookmarks (has no spatial reference).  There are 30+ pages and I have to export each one manually changing book mark to pg1,pg2, pg3..etc and my data driven pages  to pg1, pg2, pg3 etc.  I am trying to write a python script which will change the data drive page when it changes the bookmark and exports at each loop e.g bookmark pg1 and data drive page pg1 on same page.

This is as far as I have managed to get today:

Any help would be much appreciated.

code:
import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Cross Section")[0]   for bkmk in arcpy.mapping.ListBookmarks(mxd, data_frame=df):  df.extent = bkmk.extent   for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):   mxd.dataDrivenPages.currentPageID = pageNum  arcpy.mapping.ExportToPNG(mxd, r"D:\\" + str(pageNum) + bkmk.name + ".png")   del mxd 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
I think the following code will work better for you.

import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Cross Section")[0]  dict={} dict[1] = "Page 1" dict[2] = "Page 2" dict[3] = "Page 3"  for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):   mxd.dataDrivenPages.currentPageID = pageNum    bkmk = arcpy.mapping.ListBookmarks(mxd, dict[pageNum], df)[0]   df.extent = bkmk.extent    arcpy.mapping.ExportToPNG(mxd, r"D:\\" + str(pageNum) + bkmk.name + ".png") del mxd


Here are some comments:
- your code was iterating through all bookmarks for each DDP page.  I let DDP be the main driver, and then got the bookmark extent for that particular page.
- If your bookmark names were identical to the page numbers, it could have been even easier code. 
- Because your page numbers obviously don't match the page names I created a Python dictionary - it stores the translation (I'm assuming a 1:1 relationship between DDP page number [key] and bookmark [value]).   You will need to edit the code to increase the number of key/value-pairs in the dictionary.  If there is not a 1:1 relationship and there are more pages than bookmarks, then it will error when it advances to the next DDP page and can't find the corresponding bookmark.
- For listBookmarks, we are passing in the dictionary key as the wildcard and it returns the name as the value to do the search on.

I hope this helps and have FUN learning arcpy.mapping!!!
Jeff

View solution in original post

0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
I think the following code will work better for you.

import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Cross Section")[0]  dict={} dict[1] = "Page 1" dict[2] = "Page 2" dict[3] = "Page 3"  for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):   mxd.dataDrivenPages.currentPageID = pageNum    bkmk = arcpy.mapping.ListBookmarks(mxd, dict[pageNum], df)[0]   df.extent = bkmk.extent    arcpy.mapping.ExportToPNG(mxd, r"D:\\" + str(pageNum) + bkmk.name + ".png") del mxd


Here are some comments:
- your code was iterating through all bookmarks for each DDP page.  I let DDP be the main driver, and then got the bookmark extent for that particular page.
- If your bookmark names were identical to the page numbers, it could have been even easier code. 
- Because your page numbers obviously don't match the page names I created a Python dictionary - it stores the translation (I'm assuming a 1:1 relationship between DDP page number [key] and bookmark [value]).   You will need to edit the code to increase the number of key/value-pairs in the dictionary.  If there is not a 1:1 relationship and there are more pages than bookmarks, then it will error when it advances to the next DDP page and can't find the corresponding bookmark.
- For listBookmarks, we are passing in the dictionary key as the wildcard and it returns the name as the value to do the search on.

I hope this helps and have FUN learning arcpy.mapping!!!
Jeff
0 Kudos
ShaneHogg
Deactivated User
Hey Jeff,

Thanks for your help,  In the end I made a few changes just to export of PDF and combine all the pages into 1 PDF file.  I also changed the bookmark names to match the data driven pages names as its just easier/automatic for me, but good to know the alternative!

I think the next step from here is to create a toolbox and add it so that a user can enter in a output folder and name.  Do you know of any esri help files/pages which may be able to help with that?

code is here if its useful for anyone at this stage:

import arcpy, os, string
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Cross Section")[0] 
finalPdf = arcpy.mapping.PDFDocumentCreate(r"D:\final.pdf")
tmpPdf = r"D:\TEMP.pdf"

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum

  bkmk = arcpy.mapping.ListBookmarks(mxd, pageNum, df)[0]
  df.extent = bkmk.extent

  arcpy.mapping.ExportToPDF(mxd, tmpPdf)
  finalPdf.appendPages(tmpPdf)
  
del mxd, tmpPdf

tmpPdf = r"D:\TEMP.pdf"
arcpy.mapping.ExportToPDF(tmpPdf, "ALL")
finalPdf.appendPages(tmpPdf)
del tmpPdf
0 Kudos