referred to as arcpy.mapping, is Essential Python intended for use by anyone who needs Scripting for GIS to automate an ArcMap workflow. Map Automation While arcpy.mapping is driven by the powerful Python programming language, you do not need to be a GIS software developer to create arcpy.mapping scripts. The intent of arcpy.mapping is to automate tedious tasks, allowing you to focus on your important creative and analytic work.
Slide 2 The arcpy.mapping module has been
arcpy.mapping • Create a list of maps that have layers referencing a certain data source. created to automate map • Update or repair the data source links for all layers in a map document. • Create a report on information contained in documents such as management and output (printing and layers, data sources, and layer types. • Save a group of map documents to a prior ArcGIS version for distribution to others. exporting) workflows. The provided • Open and analyze the contents of map documents, and updating the document metadata (for example, keywords, author, summary) based on their content. functions focus on modifying existing • Create geographic data in batches using map export commands, such as a series of GeoTIFF images driven by a list of features in the map. • Create a map book with title page, multiple map pages, and any map layers and layout elements and number of additional pages of supporting content like tabular reports and contact lists. are not designed as a complete map compilation system. ArcMap is the recommended system for creating new map documents and authoring map layers and layouts. However, the layer and layout element modification capabilities of arcpy.mapping allow for creation of map products by modifying the contents of map documents. Being successful in these workflows requires careful map authoring of layers and page layout elements so they are easily used with your scripts. It is easiest to understand the capabilities of arcpy.mapping by describing some common scripting workflows. Here are a few of the many scenarios that an arcpy.mapping script can do:
UWF GIS Online Python Scripting
Copyright, 2016 Page 1 Map Automation Create a list of maps that have layers referencing a certain data source. Update or repair the data source links for all layers in a map document. Create a report on information contained in documents such as layers, data sources, and layer types. Save a group of map documents to a prior ArcGIS version for distribution to others. Open and analyze the contents of map documents, and updating the document metadata (for example, keywords, author, summary) based on their content. Create geographic data in batches using map export commands, such as a series of GeoTIFF images driven by a list of features in the map. Create a map book with title page, multiple map pages, and any number of additional pages of supporting content like tabular reports and contact lists.
Slide 3 There are numerous reasons why data
Change data sources import arcpy sources need to be repaired or mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") mxd.findAndReplaceWorkspacePaths(r"C:\Project\Data", r"C:\Project\Data2") mxd.saveACopy(r"C:\Project\Project2.mxd") del mxd redirected to different locations. The idea of making these changes manually in every affected map document can be overwhelming. One benefit of usingarcpy.mapping is to redirect, or repair, a layer's workspace to another data source by changing the workspace path. Making these changes manually in every affected map document can be an overwhelming and time- consuming task. Through the arcpy.mapping scripting environment, you can automate these tasks. An added benefit of using a script is that these tasks can be
UWF GIS Online Python Scripting
Copyright, 2016 Page 2 Map Automation completed without even having to open a map document in ArcMap.
In this example the data was located
directly under the C:\Project\Data folder but was moved into a subfolder called Data2. This script updates a single map document. However, you may have many map documents which need to have their layer source paths changed. In this case, you could use a Python loop to process a folder of map documents and change the layer paths from one workspace to another.
Slide 4 With arcpy.mapping, you can build a
Map Automation variety of map books. You can also use data-driven pages within your scripts to further automate the production of your map books. Data-driven pages allow you to quickly and easily create a series of layout pages from a single map document. With arcpy.mapping, you can also create a new PDF file and then append the individual component PDF files together, such as your title page, contact page, and map pages.
UWF GIS Online Python Scripting
Copyright, 2016 Page 3 Map Automation Slide 5 Python scripts using ArcPy can be run Decide where to run your script in different environments. You can import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") design your scripts to run inside the import arcpy Python window in ArcGIS for Desktop mxd = arcpy.mapping.MapDocument(r”C:\Maps\City_streets.mxd) or outside of ArcGIS in an IDE (Integrated Development Environment), such as IDLE or PythonWin. When running your scripts inside of the Python window, the ArcPy site package is already imported for you. When running your script from an IDE, you will need to include the following Python statement in your script. import arcpy You can also reference your map document in two different ways from within the Python window. You can use the keyword CURRENT to specify that you will work with the currently opened map document. If you have the C:\Maps\City_Streets.mxd opened in ArcMap, you can reference this opened map document by using the MapDocument class and setting the resulting map document object to a variable, called mxd. mxd = arcpy.mapping.MapDocument("CURR ENT") Note: This applies only to running your script from ArcMap. ArcCatalog will not recognize the CURRENT keyword. Inside an IDE, you would need to replace the keyword CURRENT with the actual path to the map document. mxd = arcpy.mapping.MapDocument(r"C:\M aps\City_Streets.mxd") If you create a script tool to run from ArcToolbox, the CURRENT keyword will
UWF GIS Online Python Scripting
Copyright, 2016 Page 4 Map Automation only be recognized when running your tool from ArcMap.
Slide 6 Working with objects in the map The objective of arcpy.mapping is to
document simplify the scripting experience. import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Maps\Schools.mxd") Within your map document, you have mxd.author = "GIS Department" many components, or objects, which mxd.save() you can access and change. The map document and the objects you work with inside the map, such as a north arrow or data frame, need to be created prior to running your script. Once you have created these objects, you can access and work with them through arcpy.mapping. In the following example, a map document named Schools.mxd located in the C:\Maps folder is accessed through the arcpy.mapping.MapDocument clas s. All of the properties and methods associated with the MapDocument object are assigned to the variable mxd. mxd = arcpy.mapping.MapDocument(r"C:\M aps\Schools.mxd") While the map document cannot be created with arcpy.mapping, you can use your script to access the map document and the objects it contains.
UWF GIS Online Python Scripting
Copyright, 2016 Page 5 Map Automation Now you can update the Author property and save your map document using the following code: mxd.author = "GIS Department" mxd.save()
Slide 7 Updating layer symbology can also be
Layer Symbology automated with arcpy.mapping. Imagine that you have many map documents that use the same layer, such as Parks or Rivers. You want to update all of the map documents that use this layer with updated symbology found in a layer file. In this section, you will use a workflow which will update a layer's symbology based on another layer which contains the symbology you wish to use.
Slide 8 Writing efficient arcpy.mapping scripts
Automating Map Production is dependent on effectively authoring much of your map content ahead of time in ArcMap. Your scripts will be used to manage the properties of these map document objects. The arcpy.mapping functions focus on modifying existing map layers and layout elements. This scripting environment is not designed as a complete map compilation system. ArcMap is the recommended system for creating new map documents and authoring map layers and layouts. The capabilities of arcpy.mapping allow for changing a large volume of maps or
UWF GIS Online Python Scripting
Copyright, 2016 Page 6 Map Automation layers quickly by accessing existing objects that you have already created. Being successful in these workflows requires careful map authoring to best take advantage of the capabilities of arcpy.mapping.
Slide 9 To make your scripts work well with
Tips for map documents your authored maps and layers, • Make sure each layer and data frame in your map has a unique name. consider the following best practices: • Provide unique names for all of your page layout elements. • Consider all of the page layout elements that will be Action: Make sure each layer and data needed for the maps that you will create. frame in your map has a unique name. Why? Naming your layers uniquely allows you to locate a layer easily with your script. Action: Provide unique names for all of your page layout elements. Why? Each page layout element has a name property which is used by arcpy.mapping to find the page layout element you wish to access. Action: Consider all of the page layout elements that will be needed for the maps that you will create. Why? Because arcpy.mapping cannot create page layout elements, you need to pre-author these for the maps you wish to create. Once you have them created, you can position them as desired on your layout page, or even move them off of the page so they will not be included in your layout. For example, if you have two data frames in your map document, each with a Schools layer, you will have to write additional code to first identify the
UWF GIS Online Python Scripting
Copyright, 2016 Page 7 Map Automation data frame and then the schools layer within the data frame.
Slide 10 ArcGIS provides all the tools you need
Tips for map documents to create the mapping content of your map books in either printed or Adobe PDF format. With arcpy.mapping, you can write scripts that send your ArcMap documents directly to a printer, or you can export to a variety of formats, including PDF. Data-driven pages provide the tools for creating the map pages of your map book. Many of the pages of your book will contain maps, but other pages may be dedicated to text, such as tabular information, a table of contents, or title page. These pages can be created outside of ArcGIS and then appended to your map pages. Through arcpy.mapping you can automate the process of creating your map pages. You can also append all of your PDF files together, including your title page and other pages, into your final map book.