Five Useful Python Code Snippets to Automate Mapping Tasks

4269
2
11-18-2016 09:15 AM
Labels (1)
Suzanne-Boden
Esri Regular Contributor
9 2 4,269

Have you been meaning to try out the ArcMap Python window, but just aren’t sure how to start? Well, the five code snippets below are a great tool to familiarize yourself with the Python window and the arcpy.mapping module. Make a copy of an MXD file and move it somewhere to test out these snippets. You’ll soon see how useful a few Python tricks can be for map management tasks.

  • Each snippet below works with ArcMap 10.x, an existing map document (MXD file), arcpy.mapping module, and Python 2.x.

You don’t have to know any Python syntax to use these snippets. Simply enter the code into the Python window and check out the results.

Snippet 1: Check Layer Definition Queries

Suppose you need to check if any of the layers in your map document have a definition query applied, and if so, view the definition queries. Opening the Layer Properties dialog box for each layer is a pain.

Solution: Open the Python window instead and run the code below. The name and definition query of all the map layers will be “printed” (i.e., displayed in the Python window).

Python code snippet to get ArcMap layer definition queries

Code Breakdown

  • The first line of code creates a variable called mxd that references the open map document using the CURRENT keyword.
  • The ListLayers function is used to create a list of all the layers in the map document and the for loop is used to iterate through each layer in the map.
  • The Layer class has many properties that can be accessed, including the definition query for a layer. Because not all layer types support all properties, the supports method is used to test which properties a layer supports. 
  • The last line of code prints (displays) the name of each layer and its definition query.

Snippet 2: Fix Broken Links

At some point, you have probably moved map data from one workspace to another. Have you ever seen the dreaded red exclamation marks? 

ArcMap table of contents with broken layer data sources

If the path to map layers has changed, with just a few lines of code you can quickly find the old workspace path and replace it with the new workspace path for all layers and tables in your map document at once. Run the following lines of code in the Python window (be sure to update the paths to reflect your old and new workspaces).

Python code snippet to update ArcMap workspace

Code Breakdown

  • The first line of code is the same as the previous example, creating the variable mxd that references the open map document.
  • The findAndReplaceWorkspacePaths method is used to search for layers that have the old workspace (first parameter) and update the workspace to the new workspace (second parameter) for all the layers and tables in the map document.
  • The RefreshActiveView function is used to see the changes in the table of contents and on the map.

Snippet 3: Identify Layers with a Broken Data Source

Speaking of those red exclamation marks, you can also use arcpy.mapping to print out the layers that have broken links using the code below.

Python code snippet to find broken data sources in ArcMap

Code Breakdown

  • The first line of code creates a variable called mxd that references the open map document, just like the examples above.
  • Like the first example, the ListLayers function creates a list of all  the layers in the map document.
  • Next, the for loop is used to iterate through each layer in the map, printing out the name and isBroken layer properties for each.

Snippet 4: Inventory Layer Data Sources

If you need to know the data source for all  the layers in your map, but don’t want to open the Layer Properties dialog box for each, simply run the  lines of code below in the Python window to print out the names and the workspace path of the layers.

Python code snippet to inventory layer data sources

Code Breakdown

  • Like all the examples above, the first line of code creates the mxd variable to reference the open map document (are you getting the hang of it?).
  • Again, the ListLayers function creates a list of all the layers in the map document that you can iterate through with the for loop.

In this example, you print out the layer properties name and workspacePath for each layer in your map. Because not all layers have a workspace path (for example, web services), the supports method is used.

Snippet 5: Combine PDF Documents

Maybe you’re creating a map book or maybe you have a few PDF documents you need to combine (like your expense report and receipts). You can use arcpy.mapping and the PDFDocument class to manipulate PDF documents. Run the code below to combine two PDF documents.

Python code snippet to combine PDF documents

Code Breakdown

  • The first line of code creates a variable called pdf that references an existing PDF document.
  • The appendPages method is used to add pages from a PDF to the end of the referenced PDF.
  • To save the changes, the saveAndClose method is used.

Want to learn more?

If you’d like to learn more about working with the ArcMap Python window or creating stand-alone Python scripts, these resources are recommended:

Brittney White This post was contributed by Esri instructor @Brittney White.

2 Comments
BlakeTerhune
MVP Regular Contributor

I never knew about the PDFDocument class. That's pretty handy!

DavidWard7
New Contributor

Also, Brittany teaches a great 3-day course.

About the Author
Suzanne is a Maryland native who enjoys writing about Esri technology and other topics. She is a Marketing Program Manager with Esri Training Services in Redlands, California.