Building a map book with Data Driven Pages and ArcPy mapping

Many organizations have a need to create map books containing a series of individual maps that cover a larger geographical area. These map books contain a series of maps and some optional and additional pages, including title pages, an overview map, and some other ancillary information, such as reports and tables. For example, a utility company might want to generate a map book detailing their assets across a service area. A map book for this utility company could include a series of maps, each at a large scale, along with a title page and an overview map. These resources would then be joined together into a single document that could be printed or distributed as a PDF file.

Getting ready

ArcGIS for Desktop provides the ability to efficiently create a map book through a combination of Data Driven Pages along with an arcpy.mapping script. With a single map document file, you can use the Data Driven Pages toolbar to create a basic series of maps using the layout view along with your operational data and an index layer. The index layer contains features that will be used to define the extent of each map in the series. However, if you need to include additional pages in the map book, including a title page, an overview map, and other ancillary pages, you'll need to combine the output from the Data Driven Pages toolbar with the functionality provided by the arcpy.mapping module. With the arcpy.mapping module, you can automate the export of the map series and append the ancillary files to a single map book document. While it is certainly possible to programmatically generate the entire map book using only Python and the arcpy.mapping module, it is more efficient to use a combination of programming and the Data Driven Pages toolbar. In this recipe, you'll learn how to create a map book that includes a series of maps along with a title page and an overview map page.

How to do it…

To save some time on this recipe, I have precreated a map document file for you that contains the data and Data Driven Pages functionality to create a series of topographic maps for King County, Washington. This map document file, called Topographic.mxd, can be found in the c:ArcpyBookCh4 folder. You may want to take a few moments to open this file in ArcGIS for Desktop and examine the data. The Data Driven Pages functionality has already been enabled for you. Additionally, a map title page (TitlePage.pdf) and an overview map page (MapIndex.pdf) have also been created for you. These files are also located in your c:ArcpyBookCh4 folder.

The steps to generate a map series can be somewhat lengthy, and are beyond the scope of this book. However, if you'd like an overview of the process, go to the ArcGIS Desktop Help system, navigate to Desktop | Mapping | Page layouts | Creating a Map Book, and follow the first seven items under this folder. This includes building map books with ArcGIS through adding dynamic text to your map book.

Follow these steps to learn how to use the Data Driven Pages functionality and the arcpy.mapping module to create a map book:

  1. Create a new IDLE script and save it as c:ArcpyBookCh4DataDrivenPages_MapBook.py.
  2. Import the arcpy and os modules:
    import arcpy
    import os
  3. Create an output directory variable:
    # Create an output directory variable
    outDir = r"C:ArcpyBookCh4"
  4. Create a new, empty PDF document in the specified output directory:
    # Create a new, empty pdf document in the specified output directory
    finalpdf_filename = outDir + r"MapBook.pdf"
    if os.path.exists(finalpdf_filename):
      os.remove(finalpdf_filename)
    finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)
  5. Add the title page to the PDF:
    # Add the title page to the pdf
    print("Adding the title page  
    ")
    finalPdf.appendPages(outDir + r"TitlePage.pdf")
  6. Add the index map to the PDF:
    # Add the index map to the pdf
    print("Adding the index page  
    ")
    finalPdf.appendPages(outDir + r"MapIndex.pdf")
  7. Export the Data Driven Pages to a temporary PDF and then add it to the final PDF:
    # Export the Data Driven Pages to a temporary pdf and then add it to the
    # final pdf. Alternately, if your Data Driven Pages have already been
    # exported, simply append that document to the final pdf.
    mxdPath = outDir + r"Topographic.mxd"
    mxd = arcpy.mapping.MapDocument(mxdPath)
    print("Creating the data driven pages 
    ")
    ddp = mxd.dataDrivenPages
    temp_filename = outDir + r"	empDDP.pdf"
    
    if os.path.exists(temp_filename):
      os.remove(temp_filename)
    ddp.exportToPDF(temp_filename, "ALL")
    print("Appending the map series  
    ")
    finalPdf.appendPages(temp_filename)
  8. Update the properties of the final PDF:
    # Update the properties of the final pdf.
    finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS", pdf_layout="SINGLE_PAGE")
  9. Save the PDF:
    # Save your result
    finalPdf.saveAndClose()
  10. Remove the temporary Data Driven Pages file:
    # remove the temporary data driven pages file
    if os.path.exists(temp_filename):
        os.remove(temp_filename)
  11. The entire script should appear as follows:
    How to do it…
  12. You can check your work by examining the c:ArcpyBookcodeCh4DataDrivenPages_MapBook.py solution file.
  13. Save and execute your script. If the script successfully executes, you should find a new file called MapBook.pdf in the c:ArcpyBookCh4 folder. When you open this file, you should see this screenshot:
    How to do it…

How it works…

The PDFDocument class in the arcpy.mapping module is frequently used to create map books. In this recipe, we used the PDFDocumentCreate() function to create an instance of PDFDocument. A path to the output PDF file was passed into the PDFDocumentCreate() function. With this instance of PDFDocument, we then called the PDFDocument.appendPages() method twice, inserting the title page and map index files that already existed as PDF files. Next, we retrieved a dataDrivenPages object from the map document file and exported each of the pages to a single PDF document. This document was then appended to our final output PDF file that already contained the title page and map index page. Finally, we updated the PDFDocument properties to use thumbs and a single page view, saved the entire file, and removed the temporary data drive page document.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset