Вы находитесь на странице: 1из 26

Python Programming for ArcGIS

David Quinn djq@mit.edu Daniel Sheehan dsheehan@mit.edu

9.00 - 12.00 January 27, 2011


Introduction to Python and ArcGIS Programming Principles and Modules ModelBuilder Reading and Writing Data

All slides, code and data available here: http://tinyurl.ie/iap


Python is a programming language that lets you work more quickly and integrate your systems more effectively 1


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

Many functions in ArcGIS require strings to be passed

back and forth

With ArcGIS 10:

Much better integration (if you can, upgrade)

We will be using the IDLE programming environment Windows: START P rograms P ython2.X IDLE MAC: Applications P ython2.X IDLE 2 The class will assume people are using both ArcGIS 9.3 and ArcGIS 10

Until we start using ArcGIS (Section ??), you can use Python on Windows/Linux/Mac for the following exercises.

Programming Concepts

Three Concepts
1 2 3

Variables Control Structures ( If statements and For Loops ) Functions

Python is case sensitive and reads whitespace (use space-bar, not tab key)

The Print function and Strings

# this is a comment print " hello world" " " " Alternative Commenting Style " " "

The Print function and Strings

# this is a comment print " hello world"

# this is a variable that contains a string name = " william " # Print out the variable that contains the string print name

Integers and Floats

# declare variables int_sample = 10 float_sample = 10.0 # printing variables # cast non string variable as a string using str ( ) print "The value of this integer is : " + s t r ( int_sample ) print "The value of this f l o a t is : " + s t r ( float_sample )

if statement

x = 2 # Condition checks i f statement is true i f x == 1: print ' x is 1! '

if / elif / else statement

x = 2 # Condition checks i f statement is true i f x == 1: print ' x is 1! ' e l i f x ==2: print ' x is 2! ' else : print ' x is not known. : ( '

for loop

for i in range (3) : # convention is to use 4 spaces to indent # python reads whitespace at the begining of a line print i

while loop

# define j j = 1 # ' while ' less than some condition while j < 3: print j #increment j j += 1

Three ways to access a folder

# Accessing a folder path = "C: \ \ folderName \ \ " path = "C: / folderName/ " path = r "C: \ folderName \ "

Importing Modules
Use the import command:

# Count number of f i l e s in a directory import os f i l e s = os . l i s t d i r ( path ) len ( f i l e s )

A module is a list of python programs that can be accessed. Commonly used modules are: os, sys, glob

Here the glob module is being imported:

import glob # use the glob module path = " /Users/ djq /Documents/ personal / " # set the folder path for i in glob . glob ( path + ' * ' ) : # loop through a l l f i l e s print i

Try replacing * with *.txt

Importing the ArcGIS module

ArcGIS 9.3:
import arcgisscripting

ArcGIS 10:
import arcpy

Exercise 1: Reading folder contents

1 2

Download zip le from course site: http://tinyurl.ie/iap Using the glob module print out: a list of all the les a list of shapeles

Model Builder


http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_ overview_of_ModelBuilder

Exercise 2: ModelBuilder

Using ModelBuilder:
1 2 3

Buffer center.shp 1km Clip road_network.shp le to buffer Export model as Python

Exercise 3: Convert ModelBuilder Code into a loop

Using the code from ModelBuilder

1 2 3

Identify relative lepaths and restructure code Iterate through this loop 5 times, buffering 1km each time 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 x t " , 'a ' ) # I f f i l e does not exist i t w i l l be created # Write results 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 . flush ( ) 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


Functions Attribute tables - Reading/Writing Packaging scripts into toolboxes

Questions | ? | Comments