Python Scripting for Map
Automation
Jeffrey Barrette
Michael Grossman
What is map scripting (arcpy.mapping)?
• A mapping module that is part of the ArcPy site-
package
• A python scripting API that allows you to:
- Manage map documents, layer files, and the data within
them
- Find a layer with data source X and replace with Y
- Update a layer’s symbology in many MXDs
- Generate reports that lists document information
- Data sources, broken layers, spatial reference info, etc.
- Automate the exporting and printing of map documents
- Automate map production and create PDF map books
- Extend Data Driven Pages
Why was it built? Who is arcpy.mapping for?
• An environment to use for basic map/layer
management and map automation tasks
• An easy to use, productive scripting environment for
the GIS Analyst
- Courser grained object model
- Not a complete replacement for ArcObjects
• A simple way to publish mapping tasks to the server
environment
- arcpy.mapping scripts can be easily published as
geoprocessing tools
Basic rules
• Reference an MXD using a path or “current” keyword
- When using CURRENT
- Always run in foreground
- May need to refresh (e.g., RefreshActiveView\TOC)
• Uniquely name all the objects you want to reference
• Pre-author MXDs with all possible elements
- Can’t create new objects (e.g., north arrow, data frames)
- Author the extra elements off the page
- No "New Map" function, so keep an empty MXD available
• This is not a replacement for ArcObjects – we are
trying to draw a line in the sand
Us You
“The Line”
Existing Applications
Demonstration
What’s new at 10.1 http://esriurl.com/3880
• What’s new à Mapping à What’s new for automating
map workflows
What’s new in 10.1: cloning elements
• You can now clone text and graphic elements
• This allows you to automate things like dynamic tables
• Example:
vertl = arcpy.mapping.ListLayoutElements(
mxd, “GRAPHIC_ELEMENT”, “VerticalLine”)[0]
vertl.elementPositionX = xPos; vertl.elementPositionY = 4
vert1.elementHeight = 3
for line in range(1, numColumns+1):
vert_clone = vertLine.clone("_clone")
xPos = xPos + colWidth
vert_clone.elementPositionX = xPos
What’s new in 10.1: symbology classes
• Layer.symbologyType r/o : string
– Returns:
• GRADUATED_COLORS, GRADUATED_SYMBOLS, UNIQUE_VALUES
• RASTER_CLASSIFIED, OTHER
• Layer.symbology r/o : Symbology Class
• Example:
if lyr.symbologyType == “GRADUATED_COLORS”:
lyr.symbology.numClasses = 10
lyr.symbology.valueField = “POP2007”
• General notes, can change:
- Symbology class
- Individual symbols use arcpy.mapping.UpdateLayer
- Classification methods
What’s new in 10.1: legend items
• A referenced legend item in a .style file can then be used
to update already existing legend items in a layout.
• Example:
mxd = arcpy.mapping.MapDocument("current")
legend = arcpy.mapping.ListLayoutElements(mxd,
"LEGEND_ELEMENT")[0]
styleItem = arcpy.mapping.ListStyleItems(
"USER_STYLE", "Legend Items", "MyNewStyle")[0]
for lyr in legend.listLegendItemLayers():
legend.updateItem(lyr, styleItem)
Publishing Map Services with arcpy.mapping
• Workflow from map document to map service
• Use Python for:
- Publishing automated analysis results
- Scheduled service upgrades
- Batch migration from 10.0 to 10.1
Prepare MXDs Create SDDraft and Stage and Publish
(arcpy.mapping) edit XML Map Service
(arcpy.mapping) (arcpy server GP tools)
arcpy.mapping.MapDocument() arcpy.mapping.CreateMapSDDraft() arcpy.StageService_server()
arcpy.UploadServiceDefinition_server()
Publishing Map Services with arcpy.mapping
Sample: CreateMapSDDraft (arcpy.mapping)
http://esriurl.com/3934
Open and
modify MXD
Create and
analyze
SDDraft,
optionally
modify XML
Stage and
publish Map
Service
High Quality Server Printing with arcpy.mapping
• Build customized versions of the new high quality print
services built into 10.1 server
• New arcpy.mapping method for converting Web Maps to
MapDocuments: ConvertWebMapToMapDocument ()
• Online help and examples
- http://esriurl.com/3941
• ConvertWebMapToMapDocument (webmap_json,
{template_mxd}, {notes_gdb},
{extra_conversion_options})
High Quality Server Printing with arcpy.mapping
• Convert the web map to a map document
• Full capabilities of arcpy.mapping on the document
- Modify content
- Add content
- Export using custom options
- Export data driven pages
- Export to PDF and insert additional pages
• Return the PDF file or map book
Related Session:
Supporting High-Quality Printing in Web Applications
with ArcGIS 10.1 for Server
High Quality Server Printing with arcpy.mapping
Sample: ConvertWebMapToMapDocument
http://esriurl.com/3941
Get webmap
JSON
Template MXD
name
Create new MXD
based on
webmap JSON
Export PDF and
append Title and
Contact Info
pages
Deployment - arcpy.mapping and Python Add-Ins
• ArcGIS 10.1 – Python Add-Ins
- Add-in deployment of mapping
tools
- Mechanism for deploying
ArcMap customizations
- Online help:
http://esriurl.com/3943
• Take mouse input, respond to Python Add-in Wizard
app events Download from:
http://esriurl.com/3942
- FinishDrawing, DPP
PageChange, etc.
Related Session:
Developing ArcGIS for Desktop Add-ins with Python
Demonstration:
Modify class breaks of a layer – Elevation python add-in