Title: An Introduction to Python for GIS Specialists and Data Managers
1An Introduction to Pythonfor GIS Specialists and
Data Managers
Jacob Morgan Brent FrakesNational Park
ServiceFort Collins, COApril, 2008
2Session Goals
- Overview of Python
- See power of the language
- Understand basic syntax
- References to other sources
- Too little time to really cover everything
3Sessions
- One Basic Python
- Two Using the ESRI Geoprocessor
- Three Formatting Data and Python Scripting
- Each one hour
- Examples and practice
4Session 1 Overview
- Scripts and the Debugger
- Commenting
- Data Types
- Operators
- Decisions and Loops
- File Handling
- Modules, Functions and Classes
5Scripts and the Debugger
- General Setup and Notes
- All code is text-based (.py extension)
- Once compiled, a new file will have a .pyc
extension. - Python is VERY picky about indentation
- Python is case sensitive
- IDLE
- Free
- GUI interface for development
- Compiler included with ArcGIS
- Provides debugging but doesn't allow for
breakpoints - Pythonwin
- Free
- GUI interface for development
- Good debugger with breakpoints
- http//sourceforge.net/projects/pywin32/
6Scripts and the Debugger
- Exercise
- type at the prompt gtgt'hello world'
- type at the prompt gtgtprint 'hello world'
- Run a script with the following code
- print 'hello world'
7Commenting
- Why?
- Make your code understandable to you and others
- How to use code
- Why code was written
- Debugging (comment out parts of script)
- Ways to Comment
- Single Line use the pound () sign before the
comment text. - single line commented text
- Multiple Lines use triple quotes before and
after a text block. -
- These two lines of text
- Are being commented out
-
8Exercise
- Add both types of comments to your script
9Data Types
- Strings
- Numeric
- Lists
- Tuples
10Strings
- An ordered collection of characters used to
represent text-based information - Common String Literals and Operations
- strNull ' ' empty string
- str1 "metadata" double-quotes
- str2 '.xml' single quotes
- str3 str1 str2 concatenate
- strNull100 repeat
- str2.upper() string method call
- str102 indexing
- str1 str1str2 strings can't be modified
-
but can be reassigned
11Strings
- Special Strings
- \n newline
- \t horizontal tab
- \\ backslash
12Strings
- Type the following
- gtgtgta'Hello'
- gtgtgtb 'World'
- gtgtgt ab
- gtgtgt (ab)10
- gtgtgt a01
- gtgtgt a-4-1
- gtgtgta.upper()
- gtgtgta.lower()
- Add two strings
- Try using the two other string methods
13Numeric Types
- Four types
- Integers
- Numeric type providing a container for whole
numbers. Plain integers are precise up to 32
bits (dependant on platform and compiler). - Floating Point Numbers
- Numbers those with a decimal component. These
are called floating point since the decimal point
can be positioned anywhere in the digit string by
a signed integer exponent- - Long Integers
- Complex Numbers
14Numeric Operators
Operation Result
x y sum of x and y
x - y difference of x and y
x y product of x and y
x / y quotient of x and y
x // y (floored) quotient of x and y
x y remainder of x / y
-x x negated
x x unchanged
abs(x) absolute value or magnitude of x
int(x) x converted to integer
long(x) x converted to long integer
float(x) x converted to floating point
complex(re,im) a complex number with real part re, imaginary part im. im defaults to zero.
c.conjugate() conjugate of the complex number c
divmod(x, y) the pair (x // y, x y)
pow(x, y) x to the power y
x y x to the power y
15Numeric Considerations
- Integer calculations yield integers (including
division!) - Floating point calculations yield floating points
- The constructors int(), long(), float(), and
complex() can be used to produce numbers of a
specific type. However, converting numbers
between types may result in rounding or
truncating digits.
16Numeric Types - Exercises
- gtgt 1 1
- gtgta 2 set value to 2
- gtgtb 3
- gtgtba
- gtgtb/a still an integer
- gtgtvalue "10" not an integer
- gtgtc 1.27 floating point
- gtgtcb
- gtgtb/c
- gtgtd int(c) rounding
17Lists
- An ordered collections of arbitrary objects
- Fully indexed
- Variable in length, heterogenous, and arbitrarily
nestable - Can be altered
- Always in brackets
- Aka Arrays!
18List - Examples
- d empty list
- d 1,2,3 list of three integers
- d 1,2,"3" two integers and a string
- d0 first element
- d 1,2, 3,3,4, 5 3 by 3 array
- len(d) 2
- len(d0) 3
19Lists - Exercises
- gtgtL1 1,2,3,4,5,6
- gtgtlen(L1)
- gtgt L1 0
- gtgt L1.pop() remove last element from stack
- gtgt L1 6 is removed
- gtgt L1 02 index 0-2
- gtgt L1 -1 index from end of list
- gtgtL2 5,6,7
- gtgt L1.append(L2) insert a new list
- gtgt L1
- gtgt L1.extend(L2) extend the current list
- gtgt L1
20Operators
- Operators compute a value when presented with a
string or number - Common Operators include
- xy assign
- xy true or false
- x lt y True or false
- xlty True or false
- xgty True or false
- xy, x/y, xy, x-y Basic math
- x1 index
- x110 slice
- x or y y evaluated only if x is false
- x and y y is evaluated only if x is true
21Operators - Exercises
- gtgtgtx 5
- gtgtgty 6
- gtgtgt x lt y
- gtgtgt x gt y
- gtgtgt x lt y or y gt 7
- gtgtgt x lt y and y gt 7
22Controlling Flow
- Decisions/Conditionals are statements that
determine if a series of conditions is true. - Most common
- if
- for
- while
23Decisions/Conditionals
- if condition
- action(s)
- elif condition
- action(s)
- else condition
- action(s)
24if Condition Example
- x 15
- if x gt 5 and x lt 10
- print The value is between 5 and 10
- else
- print The value is not between 5 and 10
25if Condition - Exercise
- gtgtgtx 7
- gtgtgtif x gt 5 and x lt 10
- print The value is between 5 and 10
26for loop
- Supports repeated execution of a statement, or
block of statements, controlled by an iterable
express - for target in iterable
- statement(s)
- else optional on normal termination
- statement(s)
27for Examples
- for x in range (1,30,2)
- print x
- for letter in hello world
- print letter
- infile open (dataset.txt,r)
- for line in infile
- print line
28For Exercises
- gtgtgtfor x in range(1,20)
- if x2 0
- print x, is even
- gtgtgtimport glob
- gtgtgtfiles glob.glob(.)
- gtgtgtfor file in files
- print file
-
29while loop
- allows looping to occur only while a specified
condition is true. WHILE evaluates the condition
each time the loop completes and terminates the
loop when the condition evaluates to false - while expression
- statement(s)
- else optional for normal termination
- statement(s)
30While Examples
- x 100
- while xgt0
- x x/2
- print x
31While Exercise
- gtgtgtx 20
- gtgtgty 0
- gtgtgtwhile xgty
- print y
- y 2
32File Handling
- Ways to open, read, write, and close files
- File2read open (filename, mode 'r')
- filename - string containing filename and
extension (e.g., 'metadata.xml') - file modes - indicates how to read or write the
file - 'r' - file must already exist and is read only
- 'w' - creates a new file for writing.
Overwrites any existing filename - 'a' - Write only mode. If file already exists,
information is appended. - 'r' - File must already exist and is opened for
both reading and writing. - 'w' - New file is created. Open for reading
and writing. - 'a' - File open for reading and writing. If
file already exists, data is appended.
33Useful File Methods
- file.read() - reads up to the end of file and
returns it as a string. - file.readline() - reads one line from file (up to
the '\n') and returns a string - file.seek(pos, how 0) - Move to a position in
the file. 'How' is the reference point for moving
(0 start of file, 1 current position 2 end
of file). - file.write() - Writes a string to a file
- file.writelines(line) - writes a line to a file
- file.close() - must be closed for it to be
unlocked by Windows
34File Examples
- infile open(d.txt, 'r')
- data infile.read() the string data is
assigned the file contents - outfile open(e.txt,'w') assign outfile to
the file object for writing - outfile.write(data) write the contents to the
file - infile.close()
- outfile.close()
35File Exercises
- gtgtgtfilename C\\temp\\test.txt
- gtgtgtoutfile open (filename ,w)
- gtgtgtoutfile.write(hello world)
- gtgtgtoutfile.close()
- gtgtgtinfile open(filename ,r)
- gtgtgtdata infile.read()
- gtgtgtprint filename, -, data
- gtgtgtinfile.close()
36Modules, Functions, and Classes
- Module
- Single Python file
- Provides definitions of functions, variables or
classes - Corresponds to a specific thema (e.g., read,
edit, and write zipfiles) - Composed of functions and/or classes. Functions
can be stand-alone or are part of a class.
Whether a function or class, both accomplish
tasks.
37Standard Modules
- Supported by Python - integral to many
applications - Index can be found at http//docs.python.org/lib/
modindex.html - Importing is simple
- gtgtimport module
- (e.g., import os)
38Accessing Functions
- import module
- module.function(arguments)
- Examples
- import os
- os.getcwd() get current directory
- import glob
- glob.glob(.py) get a list of all files with
the .py extension
39Instantiating Classes
- import module
- instance module.class(arguments)
- instance.method()
- Examples
- Import gzip import gzip module
- Zipfile gzip.GzipFile(datafile.zip) instantia
te GzipFile class - files Zipfile.read() Read zipfile and assign
it to the files string
40Getting Documentation About Modules
- dir() List of available functions and methods
- module.__doc__ - module documentation
- module.function.__doc__ - function/method
documentation - Example
- import os
- dir(os)
- os.rename.__doc__
41Third Party Modules
- Many others have developed modules and posted
them to the Python Package Index
(http//pypi.python.org/pypi/)
42Exercises
- gtgtgtimport os
- gtgtgtdir(os)
- gtgtgtos.rename.__doc__
- gtgtgtimport shutil
- gtgtgtshutil.__doc__
- gtgtgtdir(shutil)