Documente Academic
Documente Profesional
Documente Cultură
Daniel Sheehan
dsheehan@mit.edu, gishelp@mit.edu
9:30-12:30
February 1, 2013
Outline
More data types
Python functions
Accessing Attribute Tables
Lists = []
An ordered set of elements enclosed in square brackets.
Zero based (first element is accessed by typing list[0]).
# This is a list []
animals = [ 'dog ' , ' cat ' , 'horse ' , ' seal ' ]
Print animals[3]
Animals.append(owl)
Lists are mutable
Tuples = ()
Zero base (first element of a non-empty tuple is
always tuple[0]
# This is a tuple
Countries = (Costa Rica, India, Abu Dhabi)
Name[:4]
Example Function 1
# Python uses dynamic typing (type is set when
a variable is assigned or argument is passed)
def example(argument):
# Print the passed argument
print argument
# default return is none for a function
Example Function 2
# Function will give an error if argument is not a
number
# Save this file as example.py
def test_value(number):
# Add documentation here
number += 8
return number
Exercise 2
Call the function in the previous slide (called
accessAT.py) from a .py file. You should submit
a full path to the shapefile
(C:\\...\\interstateHighways.shp) as the
argument to the function. Once this works, add
a where clause to the searchCursor function call to
find only records for I95. You will need to see the
help page for the function, especially Example 4.
Be careful with \I95\.
Exercise 3
Adapt the script in Exercise 2 to:
Convert interstateHighways.shp to points
Write out the ROUTE_NUM, SHAPE@X,
SHAPE@Y for I90, then I93, then I95
You can add arguments to the accessAT function
to pass both the shapefile name and the route
you are working on.