Python Programming for ArcGIS

26
Python Programming for ArcGIS David Quinn Daniel Sheehan [email protected] [email protected] 9.00 - 12.00 January 27, 2011

Transcript of Python Programming for ArcGIS

Page 1: Python Programming for ArcGIS

Python Programming forArcGIS

David Quinn Daniel [email protected] [email protected]

9.00 - 12.00January 27, 2011

Page 2: Python Programming for ArcGIS

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

Page 3: Python Programming for ArcGIS

Python

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

1http://www.python.org/

Page 4: Python Programming for ArcGIS

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

Page 5: Python Programming for ArcGIS

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)

Page 6: Python Programming for ArcGIS

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.

Page 7: Python Programming for ArcGIS

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)

Page 8: Python Programming for ArcGIS

The ’Print’ function and Strings

# this is a commentprint " hello world"

"" " AlternativeCommentingStyle """

Page 9: Python Programming for ArcGIS

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

Page 10: Python Programming for ArcGIS

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 )

Page 11: Python Programming for ArcGIS

if statement

x = 2

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

print 'x is 1! '

Page 12: Python Programming for ArcGIS

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. : ( '

Page 13: Python Programming for ArcGIS

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

Page 14: Python Programming for ArcGIS

while loop

# define jj = 1

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

print j#increment jj += 1

Page 15: Python Programming for ArcGIS

Three ways to access a folder

# Accessing a folder

path = "C: \ \ folderName\ \ "

path = "C: / folderName/ "

path = r "C: \ folderName\ "

Page 16: Python Programming for ArcGIS

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

Page 17: Python Programming for ArcGIS

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’

Page 18: Python Programming for ArcGIS

Importing the ArcGIS module

ArcGIS 9.3:import arcgisscripting

ArcGIS 10:import arcpy

Page 19: Python Programming for ArcGIS

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

Page 20: Python Programming for ArcGIS

Model Builder

ModelBuilder3

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

Page 21: Python Programming for ArcGIS

Exercise 2: ModelBuilder

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

Page 22: Python Programming for ArcGIS

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

Page 23: Python Programming for ArcGIS

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()’

Page 24: Python Programming for ArcGIS

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

Page 25: Python Programming for ArcGIS

Tomorrow

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

Page 26: Python Programming for ArcGIS

Questions | ? | Comments