Tag: ArcPy

Combining Data Driven Pages with Python and arcpy.mapping

You can use arcpy.mapping, the Python module that allows you to interact with your map documents and layers, in conjunction with your data driven pages to do much more than make simple map books. For example, most complete map book products require title pages, overview maps, and sometimes pages of text or report information. In the past, putting these types of map books together could be time consuming, and usually required extra steps to assemble pages into a single document. At ArcGIS 10 putting together this type of map book product can be automated with a simple Python script.  

The following example exports a title page, an overview map, and a series of data driven pages and combines them, along with a PDF file containing a list of contact information, into a single multi-page PDF file:

import arcpy, os, string
#Create final output PDF file
finalPdf = arcpy.mapping.PDFDocumentCreate(r”C:MyProjectMyAtlas.pdf”)

#Export Title page and append to final PDF file
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyTitlePage.mxd”)
tmpPdf = r”C:MyProjectMyTitlePage.pdf”
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf
    
#Export Overview Map and append to final PDF file    
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyOverviewMap.mxd”)
tmpPdf = r”C:MyProjectMyOverviewMap.pdf”
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf

#Export Data Driven Pages and append to final PDF file    
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlasPages.mxd”)
tmpPdf = r”C:MyProjectMyAtlasPages.pdf”
ddp = mxd.dataDrivenPages
ddp.exportToPDF(tmpPdf, “ALL”)
finalPdf.appendPages(tmpPdf)
del mxd, tmpPdf

#Append Contact Information to final PDF file
finalPdf.appendPages(r”C:MyProjectContactInfo.pdf”)

#Update the properties of the final pdf to show thumbnail view
finalPdf.updateDocProperties(pdf_open_view=”USE_THUMBS”,
                             pdf_layout=”SINGLE_PAGE”)

del finalPdf

You can also use python in conjunction with data driven pages to cycle through each page, and perform one of the many operations supported by arcpy.mapping. For example, you can update a layer’s symbology, update some layout text, and export or print each map. This example demonstrates how to cycle through all your data driven pages and export them as PNG files:
import arcpy

#Specify the map document
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlasPages.mxd”)

#Export each of the data driven pages
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    print “Exporting page {0} of {1}”.format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPNG(mxd, r”C:MyProjectOutputMyAtlas_Page” + str(pageNum) + “.png”)
del mxd

See the following blog entry for more info on exporting and printing using arcpy.mapping: http://blogs.esri.com/Dev/blogs/arcgisdesktop/archive/2010/02/01/ArcPy-and-Geoprocessing_2620_-it_1920_s-for-Developers-too.aspx

arcpy.mapping opens up many possibilities for the types of map books you can create. For example, you can create a thematic atlas with multiple pages specifying a different theme on each page. The following example zooms to a selected parcel, toggles on different layer visibility and exports the layout for multiple themes in order to create a parcel report with a soil map, a flood map and a zoning map:

import arcpy, os

#Specify output path and final output PDF
outPath = r”C:MyProjectoutput\”
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + “ParcelReport.pdf”)

#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r”C:MyProjectMyParcelMap.mxd”)
df = arcpy.mapping.ListDataFrames(mxd, “Layers”)[0]

#Select a parcel using the LocAddress attribute and zoom to selected
parcelLayer = arcpy.mapping.ListLayers(mxd, “Parcels”, df)[0]
arcpy.SelectLayerByAttribute_management(parcelLayer, “NEW_SELECTION”, “”LocAddress” = ’519 Main St’”)
df.zoomToSelectedFeatures()

#Turn on visibility for each theme and export the page
lyrList = ["Soils", "Floodplains", "Zones"]
for lyrName in lyrList:
     lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
     lyr.visible = True

     #Export each theme to a temporary PDF and append to the final PDF
     tmpPdf = outPath + lyrName + “_temp.pdf”
     if os.path.exists(tmpPdf):
          os.remove(tmpPdf)
     arcpy.mapping.ExportToPDF(mxd, tmpPdf)
     finalPdf.appendPages(tmpPdf)
     
     #Turn off layer visibility and clean up for next pass through the loop
     lyr.visible = False
     del lyr, tmpPdf
del mxd, df, finalPdf

Frequently map books require a separate layout for a left and right page in order to create a wider margin in the middle when the pages are bound together. Or some map books require page layouts using different orientations, where some of the maps are portrait and some are landscape.   In this example we use two layouts, one for the left and one for the right. Both layouts have multiple pages created using data driven pages. The following script exports all the left pages and all the right pages and assembles them together into a single PDF file:

import arcpy, os

# Specify left and right map documents and create final output PDF file
mxdLeft = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlas_left.mxd”)
mxdRight = arcpy.mapping.MapDocument(r”C:MyProjectMyAtlas_right.mxd”)
finalPdf = arcpy.mapping.PDFDocumentCreate(r”C:MyProjectMyAtlas.pdf”)

#Export left (odd) pages to temporary PDF files
for pgNumLeft in range(1, mxdLeft.dataDrivenPages.pageCount + 1, 2):
    mxdLeft.dataDrivenPages.currentPageID = pgNumLeft
    arcpy.mapping.ExportToPDF(mxdLeft, r”C:MyProjectTemp_Page” + str(pgNumLeft) + “.pdf”)

#Export right (even) pages to temporary PDF files
for pgNumRight in range(2, mxdRight.dataDrivenPages.pageCount + 1, 2):
    mxdRight.dataDrivenPages.currentPageID = pgNumRight
    arcpy.mapping.ExportToPDF(mxdRight, r”C:DataDemosHamiltonCoDemooutputAtlas_Page” + str(pgNumRight) + “.pdf”)

#Append all pages into final PDF file and clean up temporary PDF files
for pgNum in range(1, mxdLeft.dataDrivenPages.pageCount + 1,):
    tmpPdf = r”C:DataDemosHamiltonCoDemooutputAtlas_Page” + str(pgNum) + “.pdf”
    if os.path.exists(tmpPdf):
         finalPdf.appendPages (tmpPdf)
         os.remove(tmpPdf)
del mxdLeft, mxdRight, finalPdf, tmpPdf

When you combine the multiple pages, and dynamic layout elements from data driven pages along with the things you can do with arcpy.mapping there are many possibilities. Arcpy.mapping makes it easy to write powerful Python scripts that automate many of the redundant map book and atlas compilation and production tasks.

These are just a small sample.  There is a lot more you can do with arcpy.mapping.  See an overview at:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000032000000.htm

You can find additional sample scripts at Code Gallery

 

Posted in Uncategorized | Tagged , , , , , , , | 5 Comments

Creating Place Name Indexes for Map Books

ArcGIS 10 includes the ability to make multiple-page map books using data driven pages. However, map books are often a collection of information in addition to the maps, for example, title pages, tables of contents, and index pages.  So, in addition to data driven pages, ArcGIS 10 includes the arcpy.mapping Python module that provides the ability to create and combine a series of pages into a final map book product.  

Building an index or gazetteer of place names is a common requirement for map books, and data driven pages by themselves do not provide a solution for building the index pages. However, Python scripting can be used to create these. We have placed a sample on the Resource Center to help. The sample combines the use of Data Driven Pages, arcpy.mapping, and a 3rd party PDF design toolkit called ReportLab to create a final map book product with index pages.

 

Indexes can vary greatly from organization to organization. The rules for what is indexed, how the index table is compiled, and the index formats can differ widely.  This sample provides one example. However, both the geoprocessing workflow and script can be customized for your particular index needs (Note:  In the geoprocessing steps in this example the Frequency tool is used that requires an ArcInfo license.).  Also, one of the nice things about using ReportLab is it gives you all sorts of formatting options for the results.  The arcpy.mapping Map Book with Index Pages sample can be downloaded here.

Content provided by David W.

Posted in Uncategorized | Tagged , , , , , , , , | 1 Comment

Getting started with Python map automation

ArcGIS 10 provides a new Python mapping module (arcpy.mapping) that
allows you to interact with the contents of map documents and layer
files without necessarily needing to interactively open an ArcMap
session.  The methods, properties and functions available in this new
map scripting API enable you, for example, to automate changing data
sources, modify layer properties, export and print maps, as well as
automate the creation of thematic maps and map series.  Because the new
mapping module is part of the ArcPy geoprocessing framework, scripts can
be used within ArcGIS Desktop but also published to ArcGIS Server as
geoprocessing services making it much easier to make mapping and
printing capabilities available on the server.

The following links are resources that will help you learn more about
arcpy.mapping as well as sample scripts available for download:

  •  A “Python Scripting for Map Automation in ArcGIS 10
    video presented at the 2010 Developer’s Summit is an excellent starting
    point.  This presentation not only introduces arcpy.mapping but also
    demonstrates many of its use cases. 
  • ArcGIS Desktop help has a complete section dedicated to the ArcPy mapping module.  Embedded within the help are over 100 different, practical help samples that can be copied/pasted into your applications.
  • Approximately 20 script tools that perform routine map and layer management tasks, printing and exporting, as well as basic cartographic operations
  • A script tool
    that combines Data Driven Pages, arcpy.mapping, and the ReportLab site
    package to generate a map series that includes index pages
  • A script that automatically repositions a locator map on a page layout avoiding overlap with underlying features.
  • A script that calculates adjacent page numbers to be used for a map series.
Posted in Uncategorized | Tagged , , , , , , | Leave a comment

Review of IDEs for Python

A common question we get from our Python user community is “which Python IDE is best?” or “which IDE do you guys use?” The answer we’ve given over the years has varied depending on whom you asked and the evolving Python IDE landscape.

We at Esri haven’t had the time to do a review of what’s out there but since we know a lot of you are interested in this topic, you may want to read the infoworld article: “Nine fine Python development tools” http://www.infoworld.com/d/developer-world/infoworld-review-nine-fine-python-development-tools-374

Kudos to Jason Scheirer for finding this article, Ghislain Prince for writing the post, David Wynne for pointing out that I had misspelled Ghislain’s name in the first post, and Ghislain for pointing out that he didn’t actually find the article, Jason did, and he (Ghislain) only forwarded me the email from Jason after adding the blog post details, but David didn’t know that when he caught the original misspelling.  My goal this week is to make this note longer than the actual post…

Posted in Analysis & Geoprocessing, Python | Tagged , | 8 Comments

Handling XML with Python in ArcGIS

The Python language offers a variety of ways to handle and parse XML. Lots of GIS data can be stored in an XML schema. For example, KML is made up of a well known XML structure. Another well known structure includes GPX, the native format that GPS devices typically save too. By using Python and ArcPy, you can create features from XML.

One of my teammates on the Geoprocessing team recently invested in a GPS sports watch. In addition to telling time, his watch could track movement and monitor heart rate. Since we love geography, programming, geoprocessing and fitness so much, another teammate, Ghislain, took to writing a tool to turn the GPX (XML) into feature classes for display on a map. A closer inspection of the GPS output and a look into the schema provided by topographix (http://www.topografix.com/gpx/1/1/) shows that the XML follows a standard format on how latitude, longitude, elevation and time are saved. This particular schema is enhanced to save the heart rate information. Since the information is saved in an expected way, one Python script could be written to handle many different GPS outputs.

Ghislain looked into the Python class ElementTree. ElementTree has many easy to use functions to parse through XML. Python also has other objects like Minidom or XPath which can be used to read XML; however, for this script ElementTree was used. The script looks for the values mentioned previously and hands this information over to an ArcPy cursor which writes the information into a point feature class. 

The complete script is available in the Model and Script Gallery: GPS to Layer. Feel free to take it and use it simply to convert your GPX points into a feature class, or break it open and look at how Python can be used in your ArcGIS workflows. Use the comments section here or on the page itself to leave any feedback on this tool.

Well, what good would this GPS to Points tool be if we didn’t use it to evaluate the fitness level of our co-worker? Based on the map show below there is a lot of orange and red, and I’m concerned with a heart rate of 200 beats per minute on the uphill, but according to Wikipedia, he just must be giving his “maximum effort”. At least his maximum effort is an honest effort!

Additional Resources:
• An excellent blog post by Doug Hellmann http://blog.doughellmann.com/2010/03/pymotw-parsing-xml-documents-with.html explains different ways to walk through XML.
• Python 2.6.x XML ElementTree documentation, http://docs.python.org/library/xml.etree.elementtree.html
• Element Tree Overview from effbot.org, http://effbot.org/zone/element-index.htm

Posted in Analysis & Geoprocessing | Tagged , , , | 1 Comment