USING PYTHON IN ARCGIS: A REPORT FROM THE REAL WORLD
I <3 Python!
STEVE SHAFFER
NOW YOUR TURN
Desktop / Pro / Server / Online
GP Tools / ModelBuilder / arcpy / ArcObjects
Desktop / Web / ArcGIS
SOME BACKGROUND
on ArcGIS, Python, and arcpy
1
GETTING SETUP
First things first
INSTALL PYTHON
OR
GET A PYTHON CODE EDITOR
THE CODE EDITOR SPECTRUM
Text Editor
“IDE”
Integrated Development Environment
WHICH ONE TO CHOOSE?
Here, try a list on Python’s site!
hahahahahaha...
THAT DIDN’T HELP
MY RECOMMENDATION: PYCHARM
CONFIGURE IDE INTEGRATION
(per-project setting)
2
WRITING SCRIPTS
Live by the code
BASIC SCRIPT STRUCTURE
import arcpy #Package imports
map_path = arcpy.GetParameterAsText(0) #Process parameters
pdf_path = arcpy.GetParameterAsText(1)
map = arcpy.mapping.MapDocument(map_path) #Do stuff
arcpy.mapping.ExportToPDF(map, pdf_path)
arcpy.AddMessage(‘Finished exporting’) #Log it!
del map #Clean up
BASIC SCRIPT STRUCTURE (STANDARD PYTHON WAY)
import arcpy #Package imports
def main():
map_path = arcpy.GetParameterAsText(0) #Process parameters
pdf_path = arcpy.GetParameterAsText(1)
map = arcpy.mapping.MapDocument(map_path) #Do stuff
arcpy.mapping.ExportToPDF(map, pdf_path)
arcpy.AddMessage(‘Finished exporting’) #Log it!
del map #Clean up
if __name__ == “__main__”:
main() #Kick it off!
3
DEPLOYING SCRIPTS
> gis push
ADD SCRIPT TO TOOLBOX
Given a script and a toolbox…
WORKFLOW: STANDARD TOOLBOX
add to UI
write script
.py
GP Tool
Button
add to model
add to AGS
GP Service
add to toolbox
WORKFLOW: PYTHON TOOLBOX
add to UI
write toolbox
.pyt
Button
add to model
add to AGS
GP Service
GP Tools
STANDARD TOOLBOX VS. PYTHON TOOLBOX
Standard Toolbox
Python Toolbox
DEMOS!
Add buttons to ArcMap
Replace your old VBA scripts!
ADD BUTTONS TO ARCMAP
Given a GP tool...
Automate Map Creation and Publication
Replace DS Map Book!
AUTOMATE MAP CREATION AND PUBLICATION
arcpy.mapping.____________
Some Classes:
MapDocument, LegendElement, DataFrame, TextElement, ImageElement, DataDrivenPages, ...
Some Methods:
AddLayer(), UpdateLayer(), RemoveLayer(), ...
ListDataFrames(), ListLayers(), ...
ListBrokenDataSources()
ExportToPDF(), ExportToAI(), PrintMap(), ...
Create Geographic Data from Tables
Replace humans!
CREATE GEOGRAPHIC DATA FROM TABLES
Reading data: arcpy.SearchCursor
Editing data: arcpy.UpdateCursor
Writing data: arcpy.InsertCursor
CURSORS
import arcpy�from arcpy import env��# Set the workspace�env.workspace = "D:/St_Johns/data.gdb"��# Create the search cursor�cur = arcpy.SearchCursor("roads", '"TYPE" <> 4')��# Iterate through the rows in the cursor�for row in cur:� print "Name: %s, CFCC code: %s" % (row.NAME, row.CFCC)�
# Deallocate the cursor objects�del cur, row
Build Complex Geoprocessing Models
Replace ModelBuilder!
(the annoying parts)
BUILD COMPLEX GEOPROCESSING MODELS
ModelBuilder is still great for composing tools.
A best practice: write small, generic tools in Python and compose them, along with other GP tools in ModelBuilder
Build ArcGIS Server Geoprocessing Services
Replace third-party software???
BUILD ARCGIS SERVER GEOPROCESSING SERVICES
A geoprocessing service is just a GP tool that you expose as a service in ArcGIS Server.
It doesn’t even really have to be geographic.
Could use Python to build a custom printing service!
(but I haven’t yet)
PYTHON IN ARCGIS PRO?
<Insert wisdom here>