Python Scripting for Map
Automation
Jeffrey Barrette
Michael Grossman
What is map scripting ([Link])?
• 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 [Link] 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
- [Link] 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 [Link]
• 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 = [Link](
mxd, “GRAPHIC_ELEMENT”, “VerticalLine”)[0]
[Link] = xPos; [Link] = 4
[Link] = 3
for line in range(1, numColumns+1):
vert_clone = [Link]("_clone")
xPos = xPos + colWidth
vert_clone.elementPositionX = xPos
What’s new in 10.1: symbology classes
• [Link] r/o : string
– Returns:
• GRADUATED_COLORS, GRADUATED_SYMBOLS, UNIQUE_VALUES
• RASTER_CLASSIFIED, OTHER
• [Link] r/o : Symbology Class
• Example:
if [Link] == “GRADUATED_COLORS”:
[Link] = 10
[Link] = “POP2007”
• General notes, can change:
- Symbology class
- Individual symbols use [Link]
- 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 = [Link]("current")
legend = [Link](mxd,
"LEGEND_ELEMENT")[0]
styleItem = [Link](
"USER_STYLE", "Legend Items", "MyNewStyle")[0]
for lyr in [Link]():
[Link](lyr, styleItem)
Publishing Map Services with [Link]
• 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
([Link]) edit XML Map Service
([Link]) (arcpy server GP tools)
[Link]() [Link]() arcpy.StageService_server()
arcpy.UploadServiceDefinition_server()
Publishing Map Services with [Link]
Sample: CreateMapSDDraft ([Link])
[Link]
Open and
modify MXD
Create and
analyze
SDDraft,
optionally
modify XML
Stage and
publish Map
Service
High Quality Server Printing with [Link]
• Build customized versions of the new high quality print
services built into 10.1 server
• New [Link] method for converting Web Maps to
MapDocuments: ConvertWebMapToMapDocument ()
• Online help and examples
- [Link]
• ConvertWebMapToMapDocument (webmap_json,
{template_mxd}, {notes_gdb},
{extra_conversion_options})
High Quality Server Printing with [Link]
• Convert the web map to a map document
• Full capabilities of [Link] 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 [Link]
Sample: ConvertWebMapToMapDocument
[Link]
Get webmap
JSON
Template MXD
name
Create new MXD
based on
webmap JSON
Export PDF and
append Title and
Contact Info
pages
Deployment - [Link] and Python Add-Ins
• ArcGIS 10.1 – Python Add-Ins
- Add-in deployment of mapping
tools
- Mechanism for deploying
ArcMap customizations
- Online help:
[Link]
• Take mouse input, respond to Python Add-in Wizard
app events Download from:
[Link]
- 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