Python Programming for ArcGIS

Preview:

Citation preview

Python Programming forArcGIS

David Quinn Daniel Sheehandjq@mit.edu dsheehan@mit.edu

9.00 - 12.00January 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 morequickly and integrate your systems more effectively 1

1http://www.python.org/

Python + ArcGIS

Python can interact with ArcGIS and be used to repeat manytypes 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 yetcompatible)

• Many functions in ArcGIS require strings to be passedback 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 → IDLE2

The class will assume people are using both ArcGIS 9.3 andArcGIS 10

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

Programming Concepts

Three Concepts

1 Variables2 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

# this is a commentprint " hello world"

"" " AlternativeCommentingStyle """

The ’Print’ function and Strings

# this is a commentprint " hello world"

# this is a variable that contains a stringname = "william"# Print out the variable that contains the stringprint name

Integers and Floats

# declare variablesint_sample = 10float_sample = 10.0

# printing variables# cast non−string variable as a string using str ( )pr int "The value of this integer is : " + s t r ( int_sample )pr int "The value of this f loat is : " + s t r ( float_sample )

if statement

x = 2

# Condition checks i f statement is truei f x == 1:

print 'x is 1! '

if / elif / else statement

x = 2

# Condition checks i f statement is truei 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 l ineprint i

while loop

# define jj = 1

# ' while ' less than some conditionwhile j < 3:

print j#increment jj += 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 les in a directory

import osf 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

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 esprint 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 file from course site: http://tinyurl.ie/iap

2 Using the glob module print out:a list of all the filesa list of shapefiles

Model Builder

ModelBuilder3

3http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_ModelBuilder

Exercise 2: ModelBuilder

Using ModelBuilder:1 Buffer ‘center.shp’ 1km2 Clip ‘road_network.shp’ file to buffer3 Export model as ‘Python’

Exercise 3: Convert ModelBuilderCode into a loop

Using the code from ModelBuilder1 Identify relative filepaths and restructure code2 Iterate through this loop 5 times, buffering 1km each time3 Clip ‘road_network.shp’ to buffer and make 5 new

shapefiles

Writing to a Text File

# Create a f i l e ( 'a ' means append)f = open( "C: \example. tx 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 ef . write ( " I am the content of your f i l e \n" )

# Use these commands when finishedf . flush ( )f . close ( )

Try reading the contents of your file using ’f.read()’

Exercise 4: File Manipulation

Create a folder called ‘tmp-folder’:

• Make 20 text files called File1.txt, File2.txt .... File20.txt• Write a text string into each file

Tomorrow

• Functions• Attribute tables - Reading/Writing• Packaging scripts into toolboxes

Questions | ? | Comments

Recommended