Python Programming for ArcGIS

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

9.00 - 12.00 January 27, 2011

Outline
1

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

2

3

4

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

org/ .python.Python Python is a programming language that lets you work more quickly and integrate your systems more effectively 1 1 http://www.

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. upgrade) .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.

Logistics We will be using the IDLE programming environment Windows: START → P rograms → P ython2. you can use Python on Windows/Linux/Mac for the following exercises. 2 .X → IDLE MAC: Applications → P ython2.3 and ArcGIS 10 Until we start using ArcGIS (Section ??).X → IDLE 2 The class will assume people are using both ArcGIS 9.

not tab key) .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.

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 .

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 ) .Integers and Floats # declare variables int_sample = 10 float_sample = 10.

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 \ " .

sys.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 . Commonly used modules are: 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. 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 e s print i Try replacing ‘*’ with ‘*.txt’ .

3: import arcgisscripting ArcGIS 10: import arcpy .Importing the ArcGIS module ArcGIS 9.

Exercise 1: Reading folder contents 1 2 Download zip file from course site: http://tinyurl.ie/iap Using the glob module print out: a list of all the files a list of shapefiles .

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

shp’ 1km Clip ‘road_network.shp’ file to buffer Export model as ‘Python’ .Exercise 2: ModelBuilder Using ModelBuilder: 1 2 3 Buffer ‘center.

shp’ to buffer and make 5 new shapefiles . buffering 1km each time Clip ‘road_network.Exercise 3: Convert ModelBuilder Code into a loop Using the code from ModelBuilder 1 2 3 Identify relative filepaths and restructure code Iterate through this loop 5 times.

read()’ .Writing to a Text File # Create a f i l e ( 'a ' means append) f = open( "C: \ example. 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 file using ’f. '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 . t x t " .

..Exercise 4: File Manipulation Create a folder called ‘tmp-folder’: • Make 20 text files called File1..txt .txt • Write a text string into each file . File2.txt. File20.

Reading/Writing • Packaging scripts into toolboxes .Tomorrow • Functions • Attribute tables .

Questions | ? | Comments .

Sign up to vote on this title
UsefulNot useful