Python Scripting for Map Automation in Arcgis 10

23
Python Scripting for Map Automation in ArcGIS 10 Michael Grossman Michael Grossman Jeff Barrette

Transcript of Python Scripting for Map Automation in Arcgis 10

Page 1: Python Scripting for Map Automation in Arcgis 10

Python Scripting for Map Automation in y gArcGIS 10

Michael GrossmanMichael GrossmanJeff Barrette

Page 2: Python Scripting for Map Automation in Arcgis 10

What is map scripting (akaWhat is map scripting (aka arcpy mappingarcpy mapping)?)?What is map scripting (aka What is map scripting (aka arcpy.mappingarcpy.mapping)?)?

•• A new mapping module that is part of the geoprocessing A new mapping module that is part of the geoprocessing ArcPyArcPy sitesite--packagepackage

A th i ti API th t ll tA th i ti API th t ll t•• A python scripting API that allows our users to:A python scripting API that allows our users to:

–– Manage map documents, layer files, and the data within themManage map documents, layer files, and the data within them•• Find a layer with data source X and replace with YFind a layer with data source X and replace with Y•• Find a layer with data source X and replace with YFind a layer with data source X and replace with Y•• Update a layer’s symbology in many MXDsUpdate a layer’s symbology in many MXDs•• Generate reports that lists document informationGenerate reports that lists document information

Data sources broken layers spatial reference info etcData sources broken layers spatial reference info etc–– Data sources, broken layers, spatial reference info, etc.Data sources, broken layers, spatial reference info, etc.

–– Automate the exporting and printing of map documentsAutomate the exporting and printing of map documents

A t t d ti d t PDF b kA t t d ti d t PDF b k–– Automate map production and create PDF map booksAutomate map production and create PDF map books

Page 3: Python Scripting for Map Automation in Arcgis 10

Who is arcpy mapping for? Why was it built?Who is arcpy mapping for? Why was it built?Who is arcpy.mapping for? Why was it built?Who is arcpy.mapping for? Why was it built?

•• An easy to use, productive scripting environment for the An easy to use, productive scripting environment for the GIS Analyst.GIS Analyst.

–– Courser grained object modelCourser grained object modelg jg j–– Not a complete replacement for ArcObjectsNot a complete replacement for ArcObjects

•• An environment to use for basic map/layer management An environment to use for basic map/layer management p y gp y gand map automation tasksand map automation tasks

•• A simple way to publish mapping tasks to the server A simple way to publish mapping tasks to the server environment.environment.

–– arcpy.mapping scripts can be easily published as arcpy.mapping scripts can be easily published as geoprocessing toolsgeoprocessing toolsg p gg p g

Page 4: Python Scripting for Map Automation in Arcgis 10

Demonstration #1Demonstration #1Demonstration #1Demonstration #1Brief introduction to an arcpy.mapping scriptBrief introduction to an arcpy.mapping script

Find and replace text in an ArcMap layoutFind and replace text in an ArcMap layout

Page 5: Python Scripting for Map Automation in Arcgis 10

Running arcpy mapping scriptsRunning arcpy mapping scriptsRunning arcpy.mapping scriptsRunning arcpy.mapping scripts

•• arcpy.mapping is part of arcpy.mapping is part of the Geoprocessing (GP) the Geoprocessing (GP) frameworkframework

•• Run in wide variety of Run in wide variety of placesplaces

Script tool in ArcMap orScript tool in ArcMap or–– Script tool in ArcMap or Script tool in ArcMap or ArcCatalogArcCatalog

–– Python Window in ArcMapPython Window in ArcMap–– Standalone python Standalone python -- from from

IDE, from the command IDE, from the command line, or as a scheduled line, or as a scheduled t kt ktasktask

–– Run as a GP Service in Run as a GP Service in ArcGIS ServerArcGIS Server

Page 6: Python Scripting for Map Automation in Arcgis 10

Tour of arcpy mappingTour of arcpy mappingTour of arcpy.mappingTour of arcpy.mapping

OverviewOverview

arcpy

icore sa ga mapping

Page 7: Python Scripting for Map Automation in Arcgis 10

Tour of arcpy mappingTour of arcpy mappingTour of arcpy.mappingTour of arcpy.mapping

Managing Documents and LayersManaging Documents and LayersManaging Documents and LayersManaging Documents and Layers

CLASSESCLASSES FUNCTIONSFUNCTIONS

MapDocumentMapDocumentLayerLayerTableViewTableView

MapDocument MapDocument LayerLayerListBrokenDataSourcesListBrokenDataSources

LabelClassLabelClassDataFrameDataFrameDataFrameTime DataFrameTime GraphicElementGraphicElement

ListDataFrames ListDataFrames ListLayersListLayersListLayoutElementsListLayoutElementsListPrinterNamesListPrinterNames

LegendElementLegendElementPictureElementPictureElementTextElementTextElementMapSurroundElementMapSurroundElement

ListTableViewsListTableViewsAddLayerAddLayerAddLayerToGroupAddLayerToGroupInsertLayerInsertLayerM LM LPictureElementPictureElement MoveLayerMoveLayerRemoveLayerRemoveLayerUpdateLayerUpdateLayer

Page 8: Python Scripting for Map Automation in Arcgis 10

Tour of arcpy mappingTour of arcpy mappingTour of arcpy.mappingTour of arcpy.mapping

Printing Exporting Server Publishing Map BooksPrinting Exporting Server Publishing Map BooksPrinting, Exporting, Server Publishing, Map BooksPrinting, Exporting, Server Publishing, Map Books

CLASSESCLASSES FUNCTIONSFUNCTIONS

DataDrivenPagesDataDrivenPagesPDFDocumentPDFDocument

ExportToAIExportToAIExportToBMPExportToBMPExportToEMFExportToEMFExportToEPSExportToEPSExportToGIFExportToGIFExportToJPEGExportToJPEGExportToPDFExportToPDF

GGExportToPNGExportToPNGExportToSVGExportToSVGExportToTIFFExportToTIFFPDFDocumentCreatePDFDocumentCreatePDFD OPDFD OPDFDocumentOpenPDFDocumentOpenPrintMapPrintMapPublishMSDToServerPublishMSDToServerAnalyzeForMSDAnalyzeForMSDC tT MSDC tT MSDConvertToMSDConvertToMSD

Page 9: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for Map Documents (MXDs)arcpy mapping for Map Documents (MXDs)arcpy.mapping for Map Documents (MXDs)arcpy.mapping for Map Documents (MXDs)

•• Opening Map Documents (MXD) with arcpy.mappingOpening Map Documents (MXD) with arcpy.mapping•• Use the arcpy.mapping.MapDocument functionUse the arcpy.mapping.MapDocument function•• Takes a path to MXD file or special keyword "CURRENT"Takes a path to MXD file or special keyword "CURRENT"•• Takes a path to MXD file or special keyword CURRENTTakes a path to MXD file or special keyword CURRENT

Page 10: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for Map Documents (MXDs)arcpy mapping for Map Documents (MXDs)arcpy.mapping for Map Documents (MXDs)arcpy.mapping for Map Documents (MXDs)

•• The CURRENT keyword versus MXDs on diskThe CURRENT keyword versus MXDs on disk

•• Get map from diskGet map from diskGet map from diskGet map from diskmapDoc = arcpy.mapping.MapDocument(mapDoc = arcpy.mapping.MapDocument(r"Cr"C::\\MapsMaps\\LocalGovernmentInfrastructure.mxd")LocalGovernmentInfrastructure.mxd")

•• Get map from current ArcMap sessionGet map from current ArcMap sessionGet ap o cu e t c ap sess oGet ap o cu e t c ap sess omapDoc = arcpy.mapping.MapDocument("CURRENT")mapDoc = arcpy.mapping.MapDocument("CURRENT")

•• When using CURRENTWhen using CURRENT–– Always run in foreground (checkbox in script tool props)Always run in foreground (checkbox in script tool props)–– Be wary of open conflicts, file contentionBe wary of open conflicts, file contention

•• Limitations and preLimitations and pre--authoringauthoring–– No "New Map" function, so keep an empty MXD availableNo "New Map" function, so keep an empty MXD available

Page 11: Python Scripting for Map Automation in Arcgis 10

Demonstration #2Demonstration #2Demonstration #2Demonstration #2Working with Map Documents (MXDs)Working with Map Documents (MXDs)

•• A standalone script that creates a new MXD for each data frameA standalone script that creates a new MXD for each data frame•• Use the Python Window to update a data frame nameUse the Python Window to update a data frame name

Page 12: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for Map Layers and Data Framesarcpy mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Frames

•• The “List” functionsThe “List” functions–– ListLayersListLayers–– ListDataFramesListDataFrames–– Watch the list indexes (you may often forget to use [0])Watch the list indexes (you may often forget to use [0])

••dfdf = = arcpy.mapping.ListDataFramesarcpy.mapping.ListDataFrames(MXD)[0](MXD)[0]

DataFrame properties and methodsDataFrame properties and methods•• DataFrame properties and methodsDataFrame properties and methods–– Map NavigationMap Navigation–– DataFrameTimeDataFrameTime

Page 13: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for Map Layers and Data Framesarcpy mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Frames

•• Layer functionsLayer functionsFUNCTIONSFUNCTIONS

LayerLayerListLayersListLayersListTableViewsListTableViews

AddLayerAddLayerAddLayerToGroupAddLayerToGroupInsertLayerInsertLayerMoveLayerMoveLayerRemoveLayerRemoveLayerUpdateLayerUpdateLayer

Page 14: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for Map Layers and Data Framesarcpy mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Frames

•• When and what to preWhen and what to pre--author for Map Document scenariosauthor for Map Document scenarios–– Layer Symbology (aka Renderer) properties are not accessibleLayer Symbology (aka Renderer) properties are not accessible–– PrePre--Author Layer files and use Author Layer files and use UpdateLayerUpdateLayer or data source or data source yy Up yUp y

methods to connect it to datamethods to connect it to data

•• Scenarios for updating layersScenarios for updating layers•• Scenarios for updating layersScenarios for updating layers–– changing layer symbologychanging layer symbology–– changing layer typechanging layer type

•• egeg. batch replace a traditional . batch replace a traditional featureclassfeatureclass ""basemapbasemap" layer with a " layer with a basemapbasemap layer from a serverlayer from a server

Page 15: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for Map Layers and Data Framesarcpy mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Framesarcpy.mapping for Map Layers and Data Frames

•• Updating Data SourcesUpdating Data Sources–– use arcpy.mapping for migrating Map Documents and Layer use arcpy.mapping for migrating Map Documents and Layer

files to new data sources. Batch migration.files to new data sources. Batch migration.–– Fancier scripts can help mitigate migration pain: SQL syntax Fancier scripts can help mitigate migration pain: SQL syntax

changes, etc.changes, etc.

Page 16: Python Scripting for Map Automation in Arcgis 10

Demonstration #3Demonstration #3Demonstration #3Demonstration #3Working with Map Layers and Data FramesWorking with Map Layers and Data Frames

•• A script A script that finds a layer and turns it onthat finds a layer and turns it on•• A script that updates layer data sourcesA script that updates layer data sources•• A script that replaces a layerA script that replaces a layerA script that replaces a layerA script that replaces a layer

Page 17: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for the Page Layoutarcpy mapping for the Page Layoutarcpy.mapping for the Page Layoutarcpy.mapping for the Page Layout

•• When and what to preWhen and what to pre--author author for layout manipulation for layout manipulation scenariosscenarios

–– Name your layout elementsName your layout elements–– Cannot add new elements, so Cannot add new elements, so

prepre--author and hide off page ifauthor and hide off page ifprepre author and hide off page if author and hide off page if necessarynecessary

Page 18: Python Scripting for Map Automation in Arcgis 10

Demonstration #4Demonstration #4Demonstration #4Demonstration #4Working with layout elementsWorking with layout elements

•• A script tool to find a picture element and change its data sourceA script tool to find a picture element and change its data source

Page 19: Python Scripting for Map Automation in Arcgis 10

arcpy mapping for Printing and Exportingarcpy mapping for Printing and Exportingarcpy.mapping for Printing and Exportingarcpy.mapping for Printing and Exporting

•• Export functionsExport functions•• Print FunctionsPrint Functions FUNCTIONSFUNCTIONS•• Print FunctionsPrint Functions•• Map Server PublishingMap Server Publishing•• Map BooksMap Books

ExportToAIExportToAIExportToBMPExportToBMPExportToEMFExportToEMF

•• The The PDFDocumentPDFDocumentclassclass

ExportToEPSExportToEPSExportToGIFExportToGIFExportToJPEGExportToJPEGExportToPDFExportToPDF

GG

CLASSESCLASSES

ExportToPNGExportToPNGExportToSVGExportToSVGExportToTIFFExportToTIFFPDFDocumentCreatePDFDocumentCreatePDFD OPDFD OCLASSESCLASSES

DataDrivenPagesDataDrivenPagesPDFDocumentPDFDocument

PDFDocumentOpenPDFDocumentOpenPrintMapPrintMapPublishMSDToServerPublishMSDToServerAnalyzeForMSDAnalyzeForMSDC tT MSDC tT MSDConvertToMSDConvertToMSD

Page 20: Python Scripting for Map Automation in Arcgis 10

Demonstration #5Demonstration #5Demonstration #5Demonstration #5Map output and map booksMap output and map books

A i t t l t t d t d i t lti PDFA i t t l t t d t d i t lti PDF•• A script tool to export data driven pages to multipage PDFA script tool to export data driven pages to multipage PDF•• A script tool to create a temporal map bookA script tool to create a temporal map book•• A full custom application ported from VBAA full custom application ported from VBApp ppp p

Page 21: Python Scripting for Map Automation in Arcgis 10

Resources availableResources availableResources availableResources available

•• Desktop helpDesktop help–– Geoprocessing Geoprocessing The The ArcPyArcPy site package site package Mapping moduleMapping module

•• Alphabetical lists of classes and functionsAlphabetical lists of classes and functionspp•• Detailed discussionsDetailed discussions•• Multiple sample scripts for each class and function topicMultiple sample scripts for each class and function topic

•• ArcGIS Resource CenterArcGIS Resource Center–– Download sample script toolsDownload sample script toolsp pp p

•• see the Geoprocessing Resource Center Gallerysee the Geoprocessing Resource Center Gallery–– Watch video demonstrationsWatch video demonstrations–– Monitor the user forums/discussionsMonitor the user forums/discussions–– Monitor the user forums/discussionsMonitor the user forums/discussions

Page 22: Python Scripting for Map Automation in Arcgis 10

We want your feedbackWe want your feedbackWe want your feedbackWe want your feedback

•• Get access to ArcGIS 10 PreGet access to ArcGIS 10 Pre--releaserelease•• Try using it for you day to day tasksTry using it for you day to day tasks•• Help us indentify future requirementsHelp us indentify future requirements

Page 23: Python Scripting for Map Automation in Arcgis 10

Thank you for your attentionThank you for your attentionThank you for your attention.Thank you for your attention.

Questions?Questions?Questions?Questions?