Created
August 26, 2019 18:54
-
-
Save rrudolph/0e789e2660d7de29eca2978af9e2917c to your computer and use it in GitHub Desktop.
Automate map production with custom inset extents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Purpose: to generate a mapbook for Channel Islands National Park. Originally this is for fire resource advisor maps, | |
| # but can be adapted to any mapbook purpose. The main thing this script does beyond a normal data driven pages output | |
| # is custom zooming to the island extent in the inset map. Without the custom zooming to each island, the inset map would | |
| # be too small and would not make for a useable inset to reference where the user was looking at each grid cell. | |
| # This script looks up the island three digit code within the page name of the data driven pages and zooms to that island | |
| # based off the three digit code. | |
| # Author: Rocky Rudolph, GISP, GIS Specialist, Channel Islands National Park, rocky_rudolph@nps.gov | |
| # Date: 12/15/2017 | |
| print("Importing modules") | |
| import arcpy, os | |
| # To turn it into facing pages, follow this | |
| # http://desktop.arcgis.com/en/arcmap/10.3/map/page-layouts/creating-a-map-book-with-facing-pages.htm | |
| # The exents can be found by zooming the inset frame to the desire extent and running this code | |
| # in the arcpy window in ArcMap. Then convert to a dictionary. | |
| # mxd = arcpy.mapping.MapDocument(r"CURRENT") | |
| # insetdf = arcpy.mapping.ListDataFrames(mxd)[1] | |
| # print(insetdf.extent) | |
| ext = {"SMI":(-46050.5853997891, -446865.821368743, -25059.9834185779, -435323.022283148), | |
| "SRI":(-28860.3701013181, -459688.596747635, 7190.44324573367, -439864.139320936), | |
| "SCI":(3044.63809297324, -455215.166872286, 46532.0276017449, -431301.31245753), | |
| "ANI":(50077.1997830257, -447636.65351233, 61773.8654679639, -441204.619686265 ), | |
| "SBI":(81643.5536542199, -507533.040362624, 96907.7023730642, -499139.236219609 )} | |
| def exportPDF(pageName): | |
| """Export the pdf to the specified folder and append it to the master pdf.""" | |
| print("Exporting page {0} of {1}").format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount)) | |
| outPDF = outFolder + "Fire_READ_" + str(pageName)+ ".pdf" | |
| arcpy.mapping.ExportToPDF(mxd, outPDF, resolution="100") | |
| finalPdf.appendPages(outPDF) | |
| def MakeNewExtent(island): | |
| """Get the inset data frame's extent and change it | |
| to whatever island the mabook is currently on.""" | |
| insetdf = arcpy.mapping.ListDataFrames(mxd)[1] | |
| newExtent = insetdf.extent | |
| # Reference the dictionary based on the desired island to zoom to. | |
| newExtent.XMin, newExtent.YMin = ext[island][0], ext[island][1] | |
| newExtent.XMax, newExtent.YMax = ext[island][2], ext[island][3] | |
| insetdf.extent = newExtent | |
| #Specify the map document | |
| mxd = arcpy.mapping.MapDocument(r"C:\GIS\Projects\Fire READ Maps\CHIS Fire READ Mapbook 11x17 Field.mxd") | |
| # mxd = arcpy.mapping.MapDocument(r"CURRENT") | |
| outFolder = r"C:\GIS\Projects\Fire READ Maps\Export Maps\\" | |
| finalpdf_filename = outFolder + "CHIS Fire READ Mapbook.pdf" | |
| if os.path.exists(finalpdf_filename): | |
| os.remove(finalpdf_filename) | |
| finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename) | |
| # Append existing pre-made legend title page and overview maps | |
| finalPdf.appendPages(outFolder + "Fire_READ_Legend.pdf") | |
| finalPdf.appendPages(outFolder + "CHIS Fire READ Mapbook 11x17 Index.pdf") | |
| pageNameField = "Grid_ID" | |
| # Export each of the data driven pages | |
| for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): | |
| mxd.dataDrivenPages.currentPageID = pageNum | |
| pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField) | |
| print(pageName) | |
| # Check the current page name and adjust the inset map extent according | |
| # to which island it is on. Then export the map and append to the master pdf. | |
| if "SMI" in pageName: | |
| MakeNewExtent("SMI") | |
| exportPDF(pageName) | |
| elif "SRI" in pageName: | |
| MakeNewExtent("SRI") | |
| exportPDF(pageName) | |
| elif "SCI" in pageName: | |
| MakeNewExtent("SCI") | |
| exportPDF(pageName) | |
| elif "ANI" in pageName: | |
| MakeNewExtent("ANI") | |
| exportPDF(pageName) | |
| elif "SBI" in pageName: | |
| MakeNewExtent("SBI") | |
| exportPDF(pageName) | |
| else: | |
| print("Skipping " + pageName) | |
| finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS", | |
| pdf_layout="SINGLE_PAGE") | |
| finalPdf.saveAndClose() | |
| del mxd, pageName, finalPdf | |
| print("Done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment