Gerhard Trichtl Python: Mapautomation in ArcGIS Pro
Gerhard Trichtl
Python:Mapautomation in ArcGIS Pro
Session Contents
• Python in ArcGIS Pro• Language changes between 2x and 3x• Converting your scripts from ArcGIS for Desktop to
ArcGIS Pro• API changes
- arcpy.mapping > arcpy.mp
Python within ArcGIS ProPython is “embedded” within ArcGIS Pro
- Python Window
- GP Tools (Script tools)- GP Tools with Python requirement
(Field Calculator)- Advanced Labeling
Tour of arcpy.mapping / arcpy.mp
• Overview
sada ga mapping/mp
arcpy
Language Changes between 2x and 3x: Highlights- Printing
- print >>> print()- Division
- Float is know returned- Dictionaries
- {}.iteritems >>> {}.items- Miscellaneous
- raw_input() >>> input()- range() >>> range object
- Related modules are now packages:- urllib, urllib2, urlparse >>> urllib- pep-3108
- https://docs.python.org/3/whatsnew/3.0.html
Python 2 vs. Python 3
• Python 2.x:- ArcGIS Desktop, ArcGIS Server Map Runtime- End of Support: 01.2020
• Python 3.x:- ArcGIS Pro, ArcGIS Server Pro Runtime, Hosted Notebooks, ArcGIS API for Python
• https://www.esri.com/arcgis-blog/products/arcgis/announcements/how-sunsetting-python-2-affects-arcgis/
• Windows 7/Windows 2008 R2- Support ends form Microsoft with January 2020 – so also ArcGIS would not longer support
those OS‘s – see also Deprection notes- https://support.esri.com/en/technical-article/000017062 - Last version:
http://downloads.esri.com/Support/downloads/other_/DEPRECATED%20FEATURES%20PLAN%20FOR%20ARCGIS%2010.7.x%20and%20ArcGIS%20Runtime%20SDK%20100.x%20Mid-Year%202019%20v1b%20Final%20%2009042019.pdf 8
Function to Import 8.x/9.x/10.x documents into ArcGISProjects
• ArcGISProject.importDocument (document_path, {include_layout})
.MXDsArcGIS Pro
Projects
Looping through MXDs in a folder.
Reference a template APRX.Import MXD into the APRX.Save the project.
…
DEMO
Demo
ImportingDocuments
Demo Source Code
You can get there from here
• The starting points are:- ArcGISProject- LayerFile
• The end result might be:- A modified, saved project- An exported PDF
Referencing projects
ArcGISProject classArcGISProject function
ArcGISProject(aprx_path)MethodsimportDocumentlistBrokenDataSourcessaveupdateConnectionProperties
...
Properties:defaultGeodatabasefilePathhomeFolder
...
Updating Data Sources – improvements at Pro
Project/Map/Layer/Table/LayerFile.updateConnectionProperties (current_connection_info, new_connection_info, {auto_update_joins_and_relates}, {validate})
Find this path:
Replace it with this path:
.APRXs
Updating Data Sources – Improvements
1. Changing a folder
2. Changing FGDB to SDE
3. Changing PGDB to FGDB
Updating Data Sources advanced concepts – Layer.connectionProperties
• New at Pro • The entire layer data source object model is exposed as a Python dictionary.• Use if you need more fine grained control than what’s available in
Project/Map/Layer/Table/LayerFile.updateConnectionproperties()• Useful when dealing with joins and relates or Enterprise Geodatabase
Access a layer in a map.
File Geodatabase layer connection properties dictionary
Get layer’s connection properties.
Updating Data Sources advanced concepts – Layer.connectionProperties• File Geodatabase layer connection Properties (with joins and relates)• It gets complicated!
Layer’s database
List of relates on the layer
Relate properties
Updating Data Sources advanced concepts – Layer.connectionProperties• Two ways to use the connection properties dictionary
1. Write directly to the dictionary
2. UpdateConnectionProperties will also do find and replace for full and partial dictionaries
Access a layer in a map.Get it’s connection properties.
Update connection properties dictionary.
Find this dictionary key/value pair.
Replace it with this key/value pair
Updating Data Sources advanced concepts – Layer.connectionProperties• Enterprise Geodatabase examples – change instance or server
Enterprise Geodatabase connection properties dictionary
Old database info
New database info
Update connection properties for project
Get layer’s connection properties.
Demo
UpdatingDatasources
Updating Data Sources – demo source code
DEMO
Maps and Layers
• Use “List” functions on appropriate objectsmp = aprx.listMaps(“parc*”)[0]lyr = mp.listLayers()[0]
- Warning - it is easy to forget to use [0]
• Common properties are available- Layers:
- definitionQuery, saveACopy, visible- Map:
- AddLayer, ClearSelection, defaultCamera
Layer
Symbology
Simple
UniqueValue
GraduatedColor
GraduatedSymbol
SymbolClassBreak
Group Item
ColorRamp
.symbology
.renderer / .updateRenderer
Renderer
.symbol
.classBreaks
.backgroundSymbol
.symbolTemplate
.groups .items.colorRamp
.symbol
.symbol
.applySymbolFromGallery()
Symbology Model
DemoUpdate Symbology
Sample to Update Symbology – demo source code
Layouts and layout elements
• Support zero to many- lyt = aprx.listLayouts(“Landscape”)[0]
• Find elements on a layout- txtElm = lyt.listElements(“text_element”, “title”)[0]- txtElm.text = “Some new title”
• Each element has a unique name
Map Frame and Camera
• Reference map frame on a layout- mf = lyt.listElements(“mapframe_element”)[0]
• Zoom to Bookmark / Layer- bkmk = mf.map.listBookmarks(“bookmark”)[0]- mf.zoomToBookmark(bkmk)- lyrExt = mf.getLayerExtent(lyr, True)- mf.camera.setExtent(lyrExt)
• Modify camera- cam = mf.camera- cam.X = x; cam.Y = y; cam.scale = sc- mf.camera = cam
Managing PDF documents
• Can create or open existing PDFs- myPDF = arcpy.mp.PDFDocumentCreate(r”C:\Temp\new.pdf”)- myPDF.appendPages(r”C:\Temp\someOther.pdf”)- myPDF.saveAndClose()
ModifyLayout & Export
SampleCode
Questions ?
Complete answersand select “Submit”
Scroll down to find the feedback section
Select the session you attended
Download the Esri Events app and find your event
Please Take Our Survey on the App