In a previous article you learned how to create a map series using ArcGIS Pro. In this article we’ll expand upon that article and show you how to use Python with the arcpy mapping module to automate the export of maps in a series to a PDF file.
As a review, a map series is a collection of maps (often called map books or data driven pages in ArcMap) built from a single layout in an ArcGIS Pro project. Each page shows a particular map extent that contains detailed layer information for the extent.
There are two types of map series that can be created in ArcGIS Pro: spatial map series and bookmark map series. A spatial map series defines the extent for each page as an individual feature. The series is built by iterating each feature in the layer into an index layer so that you have one map per feature. A bookmark map series creates one page per bookmark and iterates a list of bookmarks associated with a map to create the series.
The MapSeries class in the arcpy.mp package supports the ability to automate the export of a spatial map series to a PDF file. At this time it doesn’t support the export of a bookmark map series. The arcpy package doesn’t provide functionality for creating the map series, but rather it facilitates the export of an existing map series. Therefore, you will need to create the map series using ArcGIS Pro, and then automate the export of the PDF file.
Automating the Export of a Map Series using arcpy.mp
The MapSeries class in the arcpy.mp module facilities access to a spatial map series on a Layout in an ArcGIS Pro project and automates export to PDF files.
Open the ArcGIS Pro project you created in the last article.
This project contains a single operational layer called Parcels_Tax_Delinquent. These are the individual parcels in Bell County, Texas that have delinquent taxes. This layer will be used as the index layer for the map series that will be created. Since each parcel is a different size it is known as an irregular index. Other index layer types that can be used to create a spatial map series in ArcGIS Pro include grid index and strip index. ArcGIS Pro includes two tools in the Geoprocessing pane that can be used to create grid and strip indexes. However, that isn’t the focus of this particular tutorial. We are focused on the use of an irregular index which can be a feature class of any type (point, line, or polygon)
Click the Analysis tab on the ArcGIS Pro ribbon.
Select Python | Python Window (as of Pro 2.7). If you’re using a version of Pro prior to version 2.7 there should be a Python Window button directly on the ribbon.
This will display the Python Window below the map.
Enter the code you see below in the Python Window where it says “Enter Python code here”. Note: spacing is very important in Python! Also note that the location where you place the file will be specific to your computer and where you’d like to write the PDF file. If you’re prefer to simply import the existing script that I have already written you can download it here and then right click where it says “Enter Python code here”, select Load Code, and reference the script file you downloaded (MapSeries.py).
After entering the code by typing or loading the existing script, click the Enter key on your keyboard TWICE. You will click the first time to exit the code block, and a second time to actually run the script. You should see an indicator that the script is running (small dots moving from left to right in the Python Window).
If there are no problems or errors the script should produce a new PDF containing each of the maps in the map series as a separate page. If you got error messages you’ll need to go back and fix those and re-run the script. If you’re new to Python it will be a lot easier to simply load the script that you can download and execute it without having to type everything in.
Exporting Selected Features of a Map Series
The previous script exported each of the maps in the series to a PDF file, but there may be times when you’d like to create a selection set and export only the selected features from the series. This next script will do just that. We’re going to use the Select by Attributes tool to select land parcels with a value of greater than $50,000.
Enter the code you see below or download and load the pre-written script. Once again, remember to update the folder and file name for the output PDF file based on your folder structure.
Notice the call to arcpy.SelectLayerByAttribute_management() which creates a new selection set for the index layer (Parcels_Tax_Delinquent) defined as land_value greater than or equal to 50000. Also notice that we’ve added a parameter to the exportToPDF() function that will export the selected features.
When you run this script by clicking the Enter key TWICE, it should produce a PDF file with only 40 maps in the series, one for each selected feature.
The arcpy.mp MapSeries class facilitates the automation of exporting a series of maps from a spatial map series to a PDF file. If you’d like to learn more about creating map series in ArcGIS Pro or automating geoprocessing tasks in ArcGIS Pro with Python please visit the links below for more information about our upcoming classes.
Creating Maps Books with Map Series