8/6/2019 Python for Arc Gis
1/26
Python Programming forArcGIS
David Quinn Daniel Sheehan
[email protected] [email protected]
9.00 - 12.00January 27, 2011
http://[email protected]/http://[email protected]/http://[email protected]/http://[email protected]/8/6/2019 Python for Arc Gis
2/26
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
http://tinyurl.ie/iaphttp://tinyurl.ie/iap8/6/2019 Python for Arc Gis
3/26
Python
Python is a programming language that lets you work more
quickly and integrate your systems more effectively 1
1http://www.python.org/
8/6/2019 Python for Arc Gis
4/26
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
8/6/2019 Python for Arc Gis
5/26
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)
8/6/2019 Python for Arc Gis
6/26
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 and
ArcGIS 10
2
Until we start using ArcGIS (Section ??), you can use Python onWindows/Linux/Mac for the following exercises.
8/6/2019 Python for Arc Gis
7/26
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)
8/6/2019 Python for Arc Gis
8/26
The Print function and Strings
# th is is a commentprint "hello world"
"" " AlternativeCommentingStyle """
8/6/2019 Python for Arc Gis
9/26
The Print function and Strings
# th is is a commentprint "hello world"
# this is a variable that contains a str ingname = "will iam"# Print out the variable that contains the stringprint name
8/6/2019 Python for Arc Gis
10/26
Integers and Floats
# declare variablesint_sample = 10float_sample = 10.0
# printing variables# cast nonstr ing variable as a str ing using str ()print "The value of this integer is : " + s t r ( int_sample )print "The value o f t h i s f l o a t i s : " + s t r ( float_sample )
8/6/2019 Python for Arc Gis
11/26
ifstatement
x = 2
# Condition checks i f statement is truei f x == 1:
print ' x i s 1! '
8/6/2019 Python for Arc Gis
12/26
if / elif / elsestatement
x = 2
# Condition checks i f statement is true
i f x == 1:print ' x i s 1! '
e l i f x ==2:print ' x i s 2! '
else :print 'x is not known. : ( '
8/6/2019 Python for Arc Gis
13/26
for loop
for i in range( 3 ) :
# convention is to use 4 spaces to indent# python reads whitespace at the begining of a li neprint i
8/6/2019 Python for Arc Gis
14/26
while loop
# define jj = 1
# ' while ' less than some conditionwhile j < 3:
print j#increment jj += 1
8/6/2019 Python for Arc Gis
15/26
Three ways to access a folder
# Accessing a folder
path = "C: \ \ folderName \ \ "
path = "C: / folderName/ "
path = r "C:\folderName\"
8/6/2019 Python for Arc Gis
16/26
Importing Modules
Use the importcommand:
# Count number of fi l e s 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
8/6/2019 Python for Arc Gis
17/26
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 al l f i lesprint i
Try replacing * with *.txt
8/6/2019 Python for Arc Gis
18/26
Importing the ArcGIS module
ArcGIS 9.3:import arcgisscripting
ArcGIS 10:import arcpy
8/6/2019 Python for Arc Gis
19/26
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 files
a list of shapefiles
http://tinyurl.ie/iaphttp://tinyurl.ie/iap8/6/2019 Python for Arc Gis
20/26
Model Builder
ModelBuilder3
3
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_ModelBuilder
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_ModelBuilderhttp://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_ModelBuilderhttp://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_ModelBuilderhttp://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_ModelBuilder8/6/2019 Python for Arc Gis
21/26
Exercise 2: ModelBuilder
Using ModelBuilder:
1 Buffer center.shp 1km
2 Clip road_network.shp file to buffer
3 Export model as Python
Exercise 3: Convert ModelBuilder
8/6/2019 Python for Arc Gis
22/26
Exercise 3: Convert ModelBuilder
Code into a loop
Using the code from ModelBuilder
1 Identify relative filepaths 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
shapefiles
W iti t T t Fil
8/6/2019 Python for Arc Gis
23/26
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 e xi st i t w i l l be created
# W r i t e r e s u l t s t o 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()
E i 4 Fil M i l ti
8/6/2019 Python for Arc Gis
24/26
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
T
8/6/2019 Python for Arc Gis
25/26
Tomorrow
Functions
Attribute tables - Reading/Writing
Packaging scripts into toolboxes
Q ti | ? | C t
8/6/2019 Python for Arc Gis
26/26
Questions | ? | Comments