1 of 35

USING PYTHON IN ARCGIS: A REPORT FROM THE REAL WORLD

2 of 35

I <3 Python!

STEVE SHAFFER

Also ArcGIS

You can find me at:

sshaffer@eecenvironmental.com

steve.shaffer.ninja

3 of 35

NOW YOUR TURN

  1. Who uses ArcGIS?

Desktop / Pro / Server / Online

  • Who uses Geoprocessing?

GP Tools / ModelBuilder / arcpy / ArcObjects

  • Who uses Python?

Desktop / Web / ArcGIS

  • Who uses/used VBA?

4 of 35

SOME BACKGROUND

on ArcGIS, Python, and arcpy

5 of 35

1

GETTING SETUP

First things first

6 of 35

INSTALL PYTHON

  • Install from Esri
    • Installs to C:\Pythonxx\ArcGISxx.x

OR

  • Install from Somewhere Else
    • Installs to C:\Pythonxx\
    • Need to separately install the arcpy site package

7 of 35

GET A PYTHON CODE EDITOR

  • If you’ve never done this before, you will be tempted to skip this step and just use Notepad.

  • Don’t.

8 of 35

THE CODE EDITOR SPECTRUM

Text Editor

“IDE”

Integrated Development Environment

9 of 35

WHICH ONE TO CHOOSE?

Here, try a list on Python’s site!

https://wiki.python.org/moin/PythonEditors

10 of 35

hahahahahaha...

THAT DIDN’T HELP

11 of 35

MY RECOMMENDATION: PYCHARM

  • Free community edition
  • Cross-platform

  • Code completion
  • Jump to definition
  • Inspections
  • Auto save
  • VCS integration
  • So much more!

  • Can be overwhelming at first

12 of 35

CONFIGURE IDE INTEGRATION

  • Set IDE as editor

  • Configure IDE to recognize *.pyt files as Python
    • If using .pyt
    • See next slide

  • Configure IDE to use the correct Python interpreter
    • If applicable
    • See next++ slide

13 of 35

  • Configure IDE to recognize *.pyt files as Python

14 of 35

  • Configure IDE to use the correct Python interpreter

(per-project setting)

15 of 35

2

WRITING SCRIPTS

Live by the code

16 of 35

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

17 of 35

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!

18 of 35

3

DEPLOYING SCRIPTS

> gis push

19 of 35

ADD SCRIPT TO TOOLBOX

Given a script and a toolbox…

  1. Toolbox > Right-click > Add > Script

  • Use wizard / properties to define…
    1. Name, alias, etc.
    2. Parameters: make sure they match up with your code!
    3. Validation: separate Python code
    4. Help: go ahead and link to your .chm...

  • Use your script wherever you use GP tools!

20 of 35

WORKFLOW: STANDARD TOOLBOX

add to UI

write script

.py

GP Tool

Button

add to model

add to AGS

GP Service

add to toolbox

21 of 35

WORKFLOW: PYTHON TOOLBOX

add to UI

write toolbox

.pyt

Button

add to model

add to AGS

GP Service

GP Tools

22 of 35

STANDARD TOOLBOX VS. PYTHON TOOLBOX

Standard Toolbox

  • Scripts live next to other tools�
  • Configuration is defined through GUI�
  • Edit script and configuration in different places�
  • Can embed source code�
  • OK for version control

Python Toolbox

  • Only Python tools in toolbox�
  • Everything is code��
  • Edit everything in one place��
  • Supports licensing�
  • Version control friendly

23 of 35

DEMOS!

24 of 35

Add buttons to ArcMap

Replace your old VBA scripts!

25 of 35

ADD BUTTONS TO ARCMAP

Given a GP tool...

  1. Enter “customize mode” in ArcMap

  • “Commands” tab > [Geoprocessing Tools] < {your script}

26 of 35

Automate Map Creation and Publication

Replace DS Map Book!

27 of 35

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(), ...

28 of 35

Create Geographic Data from Tables

Replace humans!

29 of 35

CREATE GEOGRAPHIC DATA FROM TABLES

Reading data: arcpy.SearchCursor

Editing data: arcpy.UpdateCursor

Writing data: arcpy.InsertCursor

30 of 35

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 cursorfor row in cur:� print "Name: %s, CFCC code: %s" % (row.NAME, row.CFCC)�

# Deallocate the cursor objectsdel cur, row

31 of 35

Build Complex Geoprocessing Models

Replace ModelBuilder!

(the annoying parts)

32 of 35

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

33 of 35

Build ArcGIS Server Geoprocessing Services

Replace third-party software???

34 of 35

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)

35 of 35

PYTHON IN ARCGIS PRO?

<Insert wisdom here>