Vous êtes sur la page 1sur 26

Python Programming for

ArcGIS
David Quinn Daniel Sheehan
djq@mit.edu dsheehan@mit.edu
9.00 - 12.00
January 27, 2011
Outline
1
Introduction to Python and ArcGIS
2
Programming Principles and Modules
3
ModelBuilder
4
Reading and Writing Data
All slides, code and data available here:
http://tinyurl.ie/iap
Python
Python is a programming language that lets you work more
quickly and integrate your systems more effectively
1
1
http://www.python.org/
Python + ArcGIS
Python can interact with ArcGIS and be used to repeat many
types of analyses.
Why Python?
It is now an integral part of ArcGIS
Easy to read syntax
Large user community
Useful for scripts to control other programs
How does Python work with ArcGIS?
With ArcGIS 9.3:
Python has some limitations (some commands are not yet
compatible)
Many functions in ArcGIS require strings to be passed
back and forth
With ArcGIS 10:
Much better integration (if you can, upgrade)
Logistics
We will be using the IDLE programming environment
Windows: START Programs Python2.X IDLE
MAC: Applications Python2.X IDLE
2
The class will assume people are using both ArcGIS 9.3 and
ArcGIS 10
2
Until we start using ArcGIS (Section ??), you can use Python on
Windows/Linux/Mac for the following exercises.
Programming Concepts
Three Concepts
1 Variables
2 Control Structures ( If statements and For Loops )
3 Functions
Python is case sensitive and reads whitespace (use space-bar,
not tab key)
The Print function and Strings
# t hi s i s a comment
pri nt " hello world"
" " " Alternative
Commenting
Style " " "
The Print function and Strings
# t hi s i s a comment
pri nt " hello world"
# t hi s i s a variable that contains a stri ng
name = " william"
# Pri nt out the variable that contains the stri ng
pri nt name
Integers and Floats
# declare variables
int_sample = 10
float_sample = 10.0
# pri nti ng variables
# cast nonstri ng variable as a stri ng using st r ( )
pri nt "The value of t hi s integer i s : " + s t r ( int_sample )
pri nt "The value of t hi s f l oat i s : " + s t r ( float_sample )
if statement
x = 2
# Condition checks i f statement i s true
i f x == 1:
pri nt ' x i s 1! '
if / elif / else statement
x = 2
# Condition checks i f statement i s true
i f x == 1:
pri nt ' x i s 1! '
el i f x ==2:
pri nt ' x i s 2! '
else:
pri nt ' x i s not known. : ( '
for loop
for i i n range(3) :
# convention i s to use 4 spaces to indent
# python reads whitespace at the begining of a l i ne
pri nt i
while loop
# define j
j = 1
# ' while ' less than some condition
while j < 3:
pri nt j
#increment j
j += 1
Three ways to access a folder
# Accessing a fol der
path = "C: \ \ folderName\ \ "
path = "C: / folderName/ "
path = r "C: \ folderName\ "
Importing Modules
Use the import command:
# Count number of f i l es i n a di rectory
import os
f i l es = os . l i s t di r ( path )
len ( f i l es )
A module is a list of python programs that can be accessed.
Commonly used modules are: os, sys, glob
glob
Here the glob module is being imported:
import glob # use the glob module
path = " / Users/ djq/Documents/ personal / " # set the fol der path
for i i n glob . glob( path + '
*
' ) : # loop through al l f i l es
pri nt i
Try replacing * with *.txt
Importing the ArcGIS module
ArcGIS 9.3:
import arcgisscripting
ArcGIS 10:
import arcpy
Exercise 1: Reading folder contents
1 Download zip le from course site: http://tinyurl.ie/iap
2 Using the glob module print out:
a list of all the les
a list of shapeles
Model Builder
ModelBuilder
3
3
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_
overview_of_ModelBuilder
Exercise 2: ModelBuilder
Using ModelBuilder:
1 Buffer center.shp 1km
2 Clip road_network.shp le to buffer
3 Export model as Python
Exercise 3: Convert ModelBuilder
Code into a loop
Using the code from ModelBuilder
1 Identify relative lepaths and restructure code
2 Iterate through this loop 5 times, buffering 1km each time
3 Clip road_network.shp to buffer and make 5 new
shapeles
Writing to a Text File
# Create a f i l e ( ' a ' means append)
f = open( "C: \ example. t xt " , ' a ' )
# I f f i l e does not exi st i t wi l l be created
# Write resul ts to a f i l e
f . write ( " I am the content of your f i l e \n" )
# Use these commands when finished
f . fl ush ( )
f . close ( )
Try reading the contents of your le using f.read()
Exercise 4: File Manipulation
Create a folder called tmp-folder:
Make 20 text les called File1.txt, File2.txt .... File20.txt
Write a text string into each le
Tomorrow
Functions
Attribute tables - Reading/Writing
Packaging scripts into toolboxes
Questions | ? | Comments

Vous aimerez peut-être aussi