TransWikia.com

Iterating through a Map Series Layer using two map frames with ArcPy for ArcGIS Pro

Geographic Information Systems Asked on April 23, 2021

I have one layout with 2 map frames on it. I want to create a map series where when I export to PDF, frame 1 iterates the odd numbered pages and frame 2 iterates the even numbered pages. The problem I have is at line 48 where I need to iterate on the second map frame. I can’t figure out how to do it. Here is my code:

# Purpose: This script is designed to show 2 pages from a Map Series on
#          one Layout.  The first map frame runs through the odd numbered
#          pages in the series, while the second frame runs through the even
#          numbered pages.It then combines all pages into a single, multipage PDF.

import arcpy, os

#Filepath variables are machine specific. May need to change to execute sample
outputFolder = 'C:'   ###CHANGE if needed
pdfFileName = 'InsertNameHere.pdf'              ###CHANGE if needed

#Reference the current project, MUST be run in the ArcGIS Pro application
p = arcpy.mp.ArcGISProject('current')

#Reference the appropriate Map frames, Maps, Layers, and Layout
m = p.listMaps('MapNameHere')[0]                   ###CHANGE if needed
lyt = p.listLayouts("MapLayoutNameHere")[0]       ###CHANGE if needed
df1 = lyt.listElements("MAPFRAME_ELEMENT","MapFrameNameHere 01")[0]  ###CHANGE if needed 
df2 = lyt.listElements("MAPFRAME_ELEMENT","MapFrameNameHere 02")[0]  ###CHANGE if needed
lyr = m.listLayers("IndexLayerNameHere")[0]   ###CHANGE if needed

####################

features = {row[0]:[row[1], row[2], row[3]] for row in arcpy.da.SearchCursor
       (lyr,['Id', 'SHAPE@','Scale','Name'])}

#####################

#References the active map series
ms = lyt.mapSeries

#Create a new PDF that pages will be added into
#First remove the file if it already exists
if os.path.exists(os.path.join(outputFolder, pdfFileName)):
  os.remove(os.path.join(outputFolder, pdfFileName))
pdf = arcpy.mp.PDFDocumentCreate(os.path.join(outputFolder, time.strftime("%Y%m%d") + pdfFileName))




#Iterate through the MapSeries
if ms.enabled:
    #This part itertates on the first map frame (Odd Numbers only)
    for pageNum in range(1,ms.pageCount,2):
        ms.currentPageNumber = pageNum

        #This part is supposed to iterate through the maps series on the second map frame (Even Numbers Only)
        arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",features[pageNum+1])
        df2.camera.setExtent(df2.getLayerExtent(lyr, False, True))
        
        #Export each page and append into the new PDF document
        print('Exporting and appending: ' + pageName)
        lyt.exportToPDF(os.path.join(outputFolder, pageName +  '.pdf'))
        pdf.appendPages(os.path.join(outputFolder, pageName +  '.pdf'))
        
        #Remove the single, exported page after appended into final PDF
        os.remove(os.path.join(outputFolder, pageName + '.pdf'))
        
#Complete the creation of the PDF and commit to file
pdf.saveAndClose()

Can someone help me figure out what I’m doing wrong?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP