You are on page 1of 22

Basics for Geoprocessing with Python in ArcGIS 10.

Melissa Brenneman

www.topoworks.com

Basics for Geoprocessing with Python in ArcGIS 10.1

Part 1: Introduction to Python with ArcGIS 10.1


Geoprocessing and Python ArcPy Writing scripts Variables Strings Indentation

Welcome
Instructor: Melissa Brenneman Restrooms Schedule

Part 1: Introduction to Python with ArcGIS 10.1


Exercise 1: Get to know Python

Part 2: Getting the most out of ArcPy


Exercise 2: Working with existence, cursors and lists

Part 3: Enhancing your scripts


Exercise 3: Create a Python script tool

Introduction

Copyright 2013 TopoWorks

Lecture 1 - 1

Basics for Geoprocessing with Python in ArcGIS 10.1

Geoprocessing and Python


Geoprocessing GIS tasks (manipulating GIS data)
Change Projection Data Management Spatial Analysis Proximity?

Combine to automate workflows and perform analysis

Python

free , powerful, easy to learn, installed with ArcGIS create programs (scripts) for geoprocessing

Ways to use Python with ArcGIS


Python Add-in (10.1) Python Script Tool Python Window Python Toolbox (10.1) Field Calculator Python Script 4

Introduction

Copyright 2013 TopoWorks

Lecture 1 - 2

Basics for Geoprocessing with Python in ArcGIS 10.1

Python window

Use arcpy functionality


Geoprocessing tools Set environments

Use additional functionality of Python Includes help/usage Test ideas outside of large script Starting point for creating scripts

Right-click and save statements to a script

Creating Python scripts


IDE (Integrated Development Environment)

IDLE (Free) - Default editor with standard Python install PythonWin (Free)
Windows development environment Must install separately

Others
PyScripter (Free) Wing

Introduction

Copyright 2013 TopoWorks

Lecture 1 - 3

Basics for Geoprocessing with Python in ArcGIS 10.1

PythonWin

Script

Interactive Window

ArcPy (Python site-package)


Run tools to execute geoprocessing operations

Buffer, Clip, AddField Workspace, Coordinate System, Cell Size ListFields, ListFeatureClasses, Describe, SearchCursor SpatialReference, FieldMap arcpy.mapping manipulating map docs, map automation arcpy.sa Spatial Analyst functions and operators arcpy.ga Geostatistical Analyst functions arcpy.da Data Access functions (10.1) arcpy.na Network Analyst functions (10.1)

Set environments

Functions for helping workflows

Classes for creating complex objects

Modules that provide additional functionality


Introduction

Copyright 2013 TopoWorks

Lecture 1 - 4

Basics for Geoprocessing with Python in ArcGIS 10.1

Python and ArcGIS


Import ArcPy
import arcpy

Set environments
arcpy.env.workspace = "C:\\Temp\\ToolData\\Indiana.gdb"

arcpy.env.overwriteOutput = True

Run tools
arcpy.Buffer_analysis (inData, outData, 50)

arcpy.analysis.Buffer (inData, outData, 50)

Use ArcPy functions to help workflows


if not arcpy.Exists(outData): arcpy.Select_analysis(inData, outData, whereClause)

Writing scripts
Comments preface with #
# Buffer roads

Variable - name that stores a value

Do not need to be declared (or specified) before setting


inData = "rivers" outData = "river_buf"

Variables, functions, etc. are case sensitive


inData

is not the same as indata

NameError: name 'indata' is not defined

Can cause locks to be placed (ex: map doc)


Good idea to delete the variable when done with the file

10

Introduction

Copyright 2013 TopoWorks

Lecture 1 - 5

Basics for Geoprocessing with Python in ArcGIS 10.1

Working with strings


Single or double quotes
x = 'my python string' y = "roads.shp"

Concatenating strings
print "This teacher is " + "GREAT!" inData = "Indiana.gdb" + "\\" + "cnty"

For paths, use \\, /, or preface with r


env.workspace = "C:\\ToolData\\Indiana.gdb" env.workspace = "C:/ToolData/Indiana.gdb" env.workspace = r"C:\ToolData\Indiana.gdb"

11

Working with strings (cont)


String indexes are zero-based ([<num>])
>>> y = "roads.shp" >>> print y[3] (4th character) >>> d

r o a ds . s hp
0 1 2 3 4 5 6 7 8

Slicing strings ([<st>:<end>])


>>> print x[1:4] >>> oad Can index in reverse order >>> print y[:-4] >>> roads

r o a ds . s hp
0 1 2 3 4 5 6 7 8 9 -9 -8 -7 -6 -5 -4 -3 -2 -1

12

Introduction

Copyright 2013 TopoWorks

Lecture 1 - 6

Basics for Geoprocessing with Python in ArcGIS 10.1

Indentation
No begin/end statements to identify blocks of code Everything at an indentation is a block of code
if x > 5: print "x is greater than 5" print "but I don't care" if y == 0: print "y is 0" == is used to test conditions = is used to assign values : colon at the end of if 1st block of code 2nd block of code

13

Python in the Field Calculator

Function name Input parameter

Field names are enclosed in exclamation points

14

Introduction

Copyright 2013 TopoWorks

Lecture 1 - 7

Basics for Geoprocessing with Python in ArcGIS 10.1

Computer Exercises
Accessing the exercises Training data Starting ArcMap Starting PythonWin

Arranging windows in PythonWin

15

Introduction

Copyright 2013 TopoWorks

Lecture 1 - 8

Basics for Geoprocessing with Python in ArcGIS 10.1

Part 2: Getting the most out of ArcPy


ArcPy organization Testing for existence Cursors Listing data Common errors

ArcPy
Tools

Are actually functions, but


Have a tool reference page Return a result object Produce messages (accessed with GetMessages(), etc.) Licensed by product and extension

Basic, Standard, Advanced (ArcView, ArcEditor, ArcInfo) Spatial Analyst, Geostatistical Analyst,

Functions

Defined bit of functionality to include in larger program


List datasets, get dataset properties, check for existence, more Ex: ListFields, ListFeatureClasses, Describe, SearchCursor

Getting the most out of ArcPy

Copyright 2013 TopoWorks

Lexture 2 - 1

Basics for Geoprocessing with Python in ArcGIS 10.1

ArcPy
Classes

Use to create objects (provides a blueprint)


Ex: SpatialReference, FieldMap

Have properties
Ex: properties of env are used for environment settings arcpy.env.workspace = "c:/data/hydrology.gdb"

Modules provide addtl functionality


arcpy.mapping manipulating map docs, map automation arcpy.sa Spatial Analyst functions and operators arcpy.ga Geostatistical Analyst functions arcpy.da Data Access functions (10.1) arcpy.na Network Analyst functions (10.1)

Test for Existence


arcpy.Exists (dataset)

feature classes, tables, datasets, shapefiles, workspaces, files Returns Boolean (True or False) Uses the current workspace

arcpy.env.workspace = "c:/data/HamiltonCnty.gdb" if not arcpy.Exists(outFC): # create outFC arcpy.Select_analysis (inFC, outFC, wClause)

Getting the most out of ArcPy

Copyright 2013 TopoWorks

Lexture 2 - 2

Basics for Geoprocessing with Python in ArcGIS 10.1

Cursors
Iterate through rows in a table

read or write geometries make mass updates to a field summarize and report field values
Read rows Insert rows Update or delete rows

Search Cursor Insert Cursor Update Cursor

Forward direction only

No backing up
Use reset method to step through again

arcpy.da Cursors (new at 10.1)


More efficient than 10.0 ArcPy cursors Can use only the fields you need Can use tokens to return specific geometry

full geometry is more costly (takes more time)

Field values are accessed by Index


cur = arcpy.da.InsertCursor(tbl,["fld1","fld2"]) cur.insertRow([1, 10])

Getting the most out of ArcPy

Copyright 2013 TopoWorks

Lexture 2 - 3

Basics for Geoprocessing with Python in ArcGIS 10.1

arcpy.da Cursors (continued)


Support with statements

Guarantees close and release of database locks Regardless of success or failure

with arcpy.da.SearchCursor(tbl, fld) as cursor: for row in cursor: print row[0]

Slicing strings (review)


Slicing strings ([<st>:<end>]) >>> print x[:4] >>> Clin C l i n t on
0 1 2 3 4 5 6 7 -7 -6 -5 -4 -3 -2 -1

With cursor field values >>> print row[0] >>> Clinton >>> print row[0][:4].upper() >>> CLIN

Getting the most out of ArcPy

Copyright 2013 TopoWorks

Lexture 2 - 4

Basics for Geoprocessing with Python in ArcGIS 10.1

Listing data
Get a list of feature classes, tables, rasters,

Return Python lists


Strings (ex: List FeatureClasses > list of strings) ArcPy objects (ex: ListFields > list of field objects)

Batch process data by looping through a list Arguments such as:


{wild_card}, {feature_ type}, {field_type}

Ex: copy shapefiles to a GDB


# get a list of FCs (shapefiles) in current workspace fcs = arcpy.ListFeatureClasses () # loop through the list and copy each to a geodatabase for fc in fcs: arcpy.Copy_management (fc, "C:/myGDB.gdb/" + fc[:-4])

Common script errors


Spelling mistakes Incorrect case in variable or function names

Print vs. print, If vs. if, For vs. for arcpy.env.Workspace vs. arcpy.env.workspace

Incorrect indentation Missing colons after if, else, for, etc. Missing quotation marks on ArcPy function parameters that require strings (most tool parameters) Missing double equal sign for testing conditions

10

Getting the most out of ArcPy

Copyright 2013 TopoWorks

Lexture 2 - 5

Basics for Geoprocessing with Python in ArcGIS 10.1

Getting Help

Tool Help Show Help - limited


more examples usage, code samples

11

Getting the most out of ArcPy

Copyright 2013 TopoWorks

Lexture 2 - 6

Basics for Geoprocessing with Python in ArcGIS 10.1

Part 3: Enhancing your scripts


Executing scripts Capturing arguments Adding a script as a tool Messaging and error handling ArcPy modules

Executing Scripts
In IDE (eg. PythonWin) At the DOS prompt
Python C:\PythonScripts\RoadBuffer.py

Can also be scheduled to run at a certain time Can be added to a batch file (.BAT) Add a script to a toolbox Double-click to run from a dialog box Edit to open script source in IDE editor (eg. PythonWin) Script tools behave similarly to ArcGIS system tools
Can be added to models, other scripts Can be executed from ArcGIS Python window Can be documented

As a script tool in ArcGIS


From a Python toolbox in ArcGIS (10.1)


2

Enhancing your scripts

Copyright 2013 TopoWorks

Lexture 3 - 1

Basics for Geoprocessing with Python in ArcGIS 10.1

Capturing arguments
Arguments are inputs to scripts Make scripts more flexible, values arent hard-coded

arcpy.GetParameterAsText()

(0-based)

import arcpy # set variables to capture user arguments inWksp = arcpy.GetParameterAsText(0) cntyBnd = arcpy.GetParameterAsText(1) outPath = arcpy.GetParameterAsText(2)

Adding a script as a tool


1 Add script to ArcToolbox

2 Specify Name, Label, Description 3 Assign Parameters

Enhancing your scripts

Copyright 2013 TopoWorks

Lexture 3 - 2

Basics for Geoprocessing with Python in ArcGIS 10.1

Assigning parameters
Display name Data type (Field)

Required/ Optional Filter

Input/ Output Obtained from

Messaging and error handling


Try-except statement

Can be used to wrap entire programs or portions Error > exception raised > code under except is executed arcpy.GetMessages() to retrieve geoproc messages
(0) all messages, (1) warnings, (2) errors

try: arcpy.Buffer_analysis("roads", "roads_buf") #if error running tool, print tool messages except arcpy.ExecuteError: print arcpy.GetMessages(2) #any other error except Exception as err: print err.message 6

Enhancing your scripts

Copyright 2013 TopoWorks

Lexture 3 - 3

Basics for Geoprocessing with Python in ArcGIS 10.1

ArcPy Modules
Mapping module (arcpy.mapping)

Manipulate contents of .mxd and .lyr files Functions to automate exporting and printing Perform map algebra Set up complex neighborhood searches Automate network analysis workflows Module for working with data: edit sessions, improved cursors (see Part 2), NumPy arrays

Spatial Analyst module (arcpy.sa)

Geostatistical Analysis module (arcpy.ga)

Network Analyst module (arcpy.na)

Data access module (arcpy.da)

Help for writing scripts


ArcGIS Desktop Help

scripting topic help pages Tool help > scripting section

Export models to scripts ArcGIS script tools

Right-click > Edit to view script (read-only, but can copy)

ArcGIS Resource Center > Communities > Python (Technical community)

Python recipes (code snippets), tutorials, gallery

Enhancing your scripts

Copyright 2013 TopoWorks

Lexture 3 - 4

TopoWorks 110 Dry Run Drive Noblesville, IN 46060 Tel: (317) 674-3396 training@topoworks.com

www.topoworks.com

You might also like