You are on page 1of 71

Lecture 12

Programming for automation of


common data management tasks

Daniel P. Ames
Hydroinformatics
Fall 2012

This work was funded by National


Science Foundation Grant EPS
Goals this Week…
• To learn the basics of Python Scripting for
automation of basic geoprocessing and
database management tasks.
• Tuesday focus on intro to Python and ArcPy
for mapping and geoprocessing tasks
• Thursday focus on PyODBC and using Python
to automate database tasks
Useful Software to have on your
laptop computer in class…
• ArcGIS. http://www.esri.com/software/arcgis/arcgis-
for-desktop/free-trial
• Python. www.Python.org.
• PyODBC. http://code.google.com/p/pyodbc/.
• ArcMap 10.x comes with Python 2.6. It installs it in
your C: folder under C:\Python\ArcGIS10\.
• PyODBC. http://code.google.com/p/pyodbc/downlo
ads/detail?name=pyodbc-3.0.6.win32-
py2.6.exe&can=2&q=
Slide Credits
• 2010 Mitch Marcus and Varun Aggarwala,
University of Pennsylvania
• 2010 ESRI Conference Workshop: Python
Essentials in ArcGIS-I
• 2002 LinuxWorld Tutorial: Introduction to
Python by Guido van Rossum
Python
 Python is an open source scripting language.
 Developed by Guido van Rossum in the early 1990s
 Named after Monty Python
 Available on eniac
 Available for download from http://www.python.org

CIS 530 - Intro to NLP 5


Why Python?

 Powerful but unobtrusive object system


 Every value is an object
 Classes guide but do not dominate object
construction
 Powerful collection and iteration
abstractions
 Dynamic typing makes generics easy
Python

 Interpreted language: works with an


evaluator for language expressions
 Dynamically typed: variables do not have a
predefined type
 Rich, built-in collection types:
 Lists
 Tuples
 Dictionaries (maps)
 Sets
 Concise
Language features

 Indentation instead of braces


 Newline separates statements
 Several sequence types
 Strings ’…’: made of characters, immutable
 Lists […]: made of anything, mutable
 Tuples (…) : made of anything, immutable
 Powerful subscripting (slicing)
 Functions are independent entities (not all
functions are methods)
 Exceptions
Basics
Dynamic typing

 Variables come into existence


when first assigned a value

 A variable can refer to an object


of any type
Playing with Python (1)
>>> 2+3
5
>>> 2/3
0
>>> 2.0/3
0.66666666666666663
>>> x=4.5
>>> int(x)
4
Playing with Python (2)
>>> x='abc'
>>> x[0]
'a'
>>> x[1:3]
'bc'
>>> x[:2]
'ab’
>>> x[1]='d'
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
x[1]='d'
TypeError: 'str' object does not support item
assignment
Playing with Python (3)
>>> x=['a','b','c']
>>> x[1]
'b'
>>> x[1:]
['b', 'c']
>>> x[1]='d'
>>> x
['a', 'd', 'c']
Playing with Python (4)
>>> def p(x):
if len(x) < 2:
return True
else:
return x[0] == x[-1] and p(x[1:-1])
>>> p('abc')
False
>>> p('aba')
True
>>> p([1,2,3])
False
>>> p([1,’a’,’a’,1])
True
>>> p((False,2,2,False))
True
>>> p((’a’,1,1))
False
The Python Command Line
Interpreter

 Interactive interface to Python


% python
Python 2.6 (r26:66714, Feb 3 2009, 20:49:49)
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

 Python interpreter evaluates inputs:

>>> 3*(7+2)
27
The IDLE GUI Environment
(Windows)
 Shell for interactive evaluation.
 Text editor with color-coding and smart indenting for
creating Python files.
 Menu commands for changing system settings and
running files.

Installed with ArcMap


A Code Sample (in IDLE)
x = 34 - 23 # A comment.
y = “Hello” # Another one.
z = 3.45
if z == 3.45 or y == “Hello”:
x = x + 1
y = y + “ World” # String concat.
print x
print y
Enough to Understand the Code
 Indentation matters to the meaning of the code:
 Block structure indicated by indentation
 The first assignment to a variable creates it.
 Variable types don’t need to be declared.
 Python figures out the variable types on its own.
 Assignment uses = and comparison uses ==.
 For numbers + - * / % are as expected.
 Special use of + for string concatenation.
 Special use of % for string formatting (as with printf in C)
 Logical operators are words (and, or, not)
not symbols
 Simple printing can be done with print.
Basic Datatypes
 Integers (default for numbers)
z = 5 / 2 # Answer is 2, integer division.
 Floats
x = 3.456
 Strings
 Can use “” or ‘’ to specify.
“abc” ‘abc’ (Same thing.)
 Unmatched can occur within the string.
“matt’s”
 Use triple double-quotes for multi-line strings or strings than
contain both ‘ and “ inside of them:
“““a‘b“c”””
Whitespace
Whitespace is meaningful in Python: especially
indentation and placement of newlines.
 Use a newline to end a line of code.
 Use \ when must go to next line prematurely.
 No braces { to mark blocks of code in Python…
}
Use consistent indentation instead.
 The first line with less indentation is outside of the
block.
 The first line with more indentation starts a nested block
 Often a colon appears at the start of a new block.
(E.g. for function and class definitions.)
Comments
 Start comments with # – the rest of line is ignored.
 Can include a “documentation string” as the first line of any
new function or class that you define.
 The development environment, debugger, and other tools
use it: it’s good style to include one.
def my_function(x, y):
“““This is the docstring. This
function does blah blah blah.”””
# The code would go here...
Naming Rules
 Names are case sensitive and cannot start with a number.
They can contain letters, numbers, and underscores.
bob Bob _bob _2_bob_ bob_2 BoB
 There are some reserved words:
and, assert, break, class, continue, def, del, elif,
else, except, exec, finally, for, from, global, if,
import, in, is, lambda, not, or, pass, print, raise,
return, try, while
The + Operator
 The + operator produces a new tuple, list, or string whose
value is the concatenation of its arguments.

>>> (1, 2, 3) + (4, 5, 6)


(1, 2, 3, 4, 5, 6)

>>> [1, 2, 3] + [4, 5, 6]


[1, 2, 3, 4, 5, 6]

>>> “Hello” + “ ” + “World”


‘Hello World’
Lists

>>> li = [‘abc’, 23, 4.34, 23]


>>> li[1] = 45
>>> li
[‘abc’, 45, 4.34, 23]

 We can change lists in place.


 Name li still points to the same memory reference when
we’re done.
Operations on Lists Only 1

>>> li = [1, 11, 3, 4, 5]

>>> li.append(‘a’) # Note the method syntax


>>> li
[1, 11, 3, 4, 5, ‘a’]

>>> li.insert(2, ‘i’)


>>>li
[1, 11, ‘i’, 3, 4, 5, ‘a’]
Functions
Defining Functions
Function definition begins with def Function name and its arguments.

def get_final_answer(filename):
“Documentation String”
line1
line2 Colon.
return total_counter

The indentation matters…


First line with less
indentation is considered to be The keyword ‘return’ indicates the
outside of the function definition.value to be sent back to the caller.

No header file or declaration of types of function or arguments.

CIS 530 - Intro to NLP


Python and Types

Python determines the data types of variable


bindings in a program automatically. “Dynamic Typing”

But Python’s not casual about types, it


enforces the types of objects. “Strong Typing”

So, for example, you can’t just append an integer to a string. You
must first convert the integer to a string itself.
x = “the answer is ” # Decides x is bound to a string.
y = 23 # Decides y is bound to an integer.
print x + y # Python will complain about this.
Calling a Function

 The syntax for a function call is:


>>> def myfun(x, y):
return x * y
>>> myfun(3, 4)
12
Functions without returns

 All functions in Python have a return value


 even if no return line inside the code.
 Functions without a return return the special value
None.
 None is a special constant in the language.
 None is used like NULL, void, or nil in other languages.
 None is also logically equivalent to False.
 The interpreter doesn’t print None
Logical Expressions
True and False
 True and False are constants in Python.

 Other values equivalent to True and False:


 False: zero, None, empty container or object
 True: non-zero numbers, non-empty objects

 Comparison operators: ==, !=, <, <=, etc.


 X and Y have same value: X == Y
 Compare with X is Y :
X and Y are two variables that refer to the identical same
object.
Boolean Logic Expressions
 You can also combine Boolean expressions.
 True if a is True and b is True: a and b
 True if a is True or b is True: a or b
 True if a is False: not a
 Use parentheses as needed to disambiguate
complex Boolean expressions.
Special Properties of and and or
 Actually and and or don’t return True or False.
 They return the value of one of their sub-expressions
(which may be a non-Boolean value).
 X and Y and Z
 If all are true, returns value of Z.
 Otherwise, returns value of first false sub-expression.
 X or Y or Z
 If all are false, returns value of Z.
 Otherwise, returns value of first true sub-expression.
 And and or use lazy evaluation, so no further expressions
are evaluated
Control of Flow
if Statements
if x == 3:
print “X equals 3.”
elif x == 2:
print “X equals 2.”
else:
print “X equals something else.”
print “This is outside the ‘if’.”

Be careful! The keyword if is also used in the syntax


of filtered list comprehensions.
Note:
 Use of indentation for blocks
 Colon (:) after boolean expression
while Loops
>>> x = 3
>>> while x < 5:
print x, "still in the loop"
x = x + 1
3 still in the loop
4 still in the loop
>>> x = 6
>>> while x < 5:
print x, "still in the loop"

>>>
For Loops
For Loops 1
 For-each is Python’s only for construction
 A for loop steps through each of the items in a collection
type, or any other type of object which is “iterable”

for <item> in <collection>:


<statements>

 If <collection> is a list or a tuple, then the loop steps


through each element of the sequence.

 If <collection> is a string, then the loop steps through each


character of the string.

for someChar in “Hello World”:


print someChar
For Loops 2
for <item> in <collection>:
<statements>

 <item> can be more complex than a single


variable name.
 When the elements of <collection> are themselves
sequences, then <item> can match the structure of the
elements.
 This multiple assignment can make it easier to access
the individual parts of each element.

for (x, y) in [(a,1), (b,2), (c,3), (d,4)]:


print x
String Operations
 A number of methods for the string class perform
useful formatting operations:

>>> “hello”.upper()
‘HELLO’

 Check the Python documentation for many other


handy string operations.

 Helpful hint: use <string>.strip() to strip off


final newlines from lines read from files
Importing and Modules
Importing and Modules
 Use classes & functions defined in another file.
 A Python module is a single file with the same name (plus
the .py extension)

Where does Python look for module files?


 The list of directories where Python looks: sys.path

 To add a directory of your own to this list, append it to this


list.
sys.path.append(‘/my/new/path’)
Commonly Used Modules

 Some useful modules to import, included with


Python:

 Module: sys - Lots of handy stuff.


Maxint
 Module: os - OS specific code.
 Module: os.path - Directory processing.
More Commonly Used Modules
 Module: math - Mathematical code.
Exponents
sqrt
 Module: Random - Random number code.
Randrange
Uniform
Choice
Shuffle
 To see what’s in the standard library of modules, check out
the Python Library Reference:
http://docs.python.org/lib/lib.html
 Or O’Reilly’s Python in a Nutshell:
http://proquest.safaribooksonline.com/0596100469
(URL works inside of UPenn, afaik, otherwise see the course
web page)
Finally…
 pass
It does absolutely nothing.

 Just holds the place of where something should go


syntactically. Programmers like to use it to waste time in
some code, or to hold the place where they would like put
some real code at a later time.
for i in range(1000):
pass
ArcPy Python Scripting Basics
Why use Python scripting in ArcMap?

• Python: a free,
cross-platform, and
easy to learn
language
• Used to execute
single tools
or strings of tools
• Develop, execute,
and share
geoprocessing
workflows
• Automation
ArcPy

• The access point to every geoprocessing tool

• A package of functions, classes and modules, all


related to scripting in ArcGIS
- Functions that enhance geoprocessing workflows
(ListFeatureClasses, Describe, SearchCursor )
- Classes that can be used to create complex
objects (SpatialReference and FieldMap objects)
- Modules that provide additional functionality
(Mapping and SpatialAnalyst modules)

• Builds on arcgisscripting module (pre-10.0)


ArcGIS Python window

• Embedded, interactive Python window within ArcGIS


– Access to ArcPy, any Python functionality

• Great for experimenting with and learning Python


Executing a tool in Python

• ArcPy must be imported


• Follow syntax: arcpy.toolname_toolboxalias()
• Enter input and output parameters

# Import ArcPy
import arcpy

# Set workspace environment


arcpy.env.workspace = ”C:/Data”

# Execute Geoprocessing tool


arcpy.Buffer_analysis(“roads.shp", “roads_buffer.shp", “50 Meters”)
Getting tool syntax

• Results window, ‘Copy as Python Snippet’

• Export Model to Python script

• Drag tool into Python window

• Tool documentation

• arcpy.Usage(“Buffer_analysis”)
Setting environments in Python

• Accessed from arcpy.env

• Common environments:
– Workspace, coordinate system, extent

• Examples:
arcpy.env.workspace
arcpy.env.outputCoordinateSystem
arcpy.env.extent
Live Demo

• Live demonstration of running


geoprocessing functions in ArcMap
Geoprocessing messages

• Tools return three types of messages:


– Informative messages
– Warning messages
– Error messages
• Displayed in the ArcGIS Python window
• arcpy.GetMessages()
– GetMessages(): All messages
– GetMessages(0): Only informative messages
– GetMessages(1): Only warning messages
– GetMessages(2): Only error messages
ArcPy functions: GetMessages

# Execute Geoprocessing tool


arcpy.Buffer_analysis(“roads.shp", “roads_buffer.shp", “50 Meters”)

# Print the execution messages


print arcpy.GetMessages()

Executing: Buffer roads.shp roads_buffer.shp ’50 Meters’


Start Time: Tue July 13 13:52:40 2010
Executing (Buffer) successfully.
End Time: Tue July 13 13:52:45 2010(Elapsed Time: 5.00…
Error handling basics

• Why do errors occur?


– Incorrect tool use
– Typos
– Syntax errors
• Python error handling
– Try…Except…
try:
# Do Something
except:
# A failure occurred, do something else
Try, Except Statement

# Start Try block


try:
arcpy.Buffer_analysis(“roads.shp", “roads_buffer.shp", “50 Meters”)

# If an error occurs
except:
# Print that Buffer failed and why
print "Buffer failed“
print arcpy.GetMessages(2)
Live Demo

• Live demonstration of error handling


in Python/ArcPy
Geoprocessing Tasks
ArcPy functions

• Perform useful scripting tasks


– Print geoprocessing messages
(GetMessages)
– List data to aid batch
processing
(ListFeatureClasses, ListFields;
12 total List functions)
– Getting data properties
(Describe)

• Allow automation of manual


tasks
Batch processing

• Run a geoprocessing operation


multiple times with some automation
– Example: Using the Clip tool to clip every
feature class in a workspace to a
boundary

• List functions used in Python


to perform batch processing
ArcPy functions: ListFeatureClasses

# Set the workspace


arcpy.env.workspace = “C:/Data/FileGDB.gdb/FDs"

# Get a list of all polygon feature classes


fcList = arcpy.ListFeatureClasses("*", "polygon")

# Print the list of feature classes one at a time


for fc in fcList:
print fc
ArcPy functions: ListFields

# List all fields in a feature class


fields = arcpy.ListFields(“C:/Data/roads.shp")

# loop through all the fields


for field in fields:
# print the field’s name and field type
print field.name, field.type

• Field objects have properties


– name, aliasName, domain,
editable, hasIndex, isNullable,
isUnique, length, precision, scale,
type
ArcPy functions: Describe

• Returns an object with properties

• Allows script to determine properties


of data
– Data type (shapefile, coverage, network
dataset, etc.)
– Shape type (point, polygon, line, etc.)
– Spatial reference
– Extent of features
– Path
ArcPy functions: Describe

# Describe a feature class


desc = arcpy.Describe(“C:/Data/roads.shp")

# Branch script logic based on ShapeType property


if desc.shapeType == "Polyline" :
print "The fc is a line feature class“
elif desc.shapeType == "Polygon" :
print "The fc is a polygon feature class“
else:
print “The fc is not a line or polygon."
Receiving arguments

• Arguments are inputs to a script


- Makes script more flexible and portable
- Values passed to script from user, instead of
hard-coded

• Use GetParameterAsText to read


arguments
- Zero-based index

• Connect script to an ArcGIS script tool


Receiving arguments

# Create variables from input arguments


inputFC = arcpy.GetParameterAsText(0)
outputFC = arcpy.GetParameterAsText(1)

# First and third parameters come from arguments


arcpy.Clip_analysis(inputFC, “C:/Data/boundary.shp", outputFC)
Python scripting resources

• ArcGIS Resource Centers


– http://resources.arcgis.com
– Online documentation
– Geoprocessing Resource Center: script gallery, blog,
presentations
• Python Reference Books
– Learning Python by Mark Lutz & David Ascher
– Core Python Programming by Wesley J. Chun
• Python Organization
– http://www.python.org
• DiveIntoPython.org
– Free tutorial: http://diveintopython.org/#download

You might also like