GEOG 485:
GIS Programming and Automation

Lesson 2 Practice Exercises


Before trying to tackle Project 2, you may want to try some simple practice exercises, particularly if the concepts in this lesson were new to you. Remember to choose File > New in PythonWin to create a new script (or click the empty page icon). You can name the scripts something like Practice1, Practice2, etc. To execute a script in PythonWin, click the "running man" icon.

Find the spaces in a list of names

Python String objects have an index method that enables you to find a substring within the larger string. For example, if I had a variable defined as name = "Joe Paterno" and followed that up with the expression name.index("Pa"), it would return the value 4 because the substring "Pa" begins at character 4 in the string held in name. (The first character in a string is at position 0.)

For this practice exercise, start by creating a list of names like the following:

beatles = ["John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison"]

Then write code that will loop through all the items in the list, printing a message like the following:

"There is a space in ________'s name at character ____." where the first blank is filled in with the name currently being processed by the loop and the second blank is filled in with the position of the first space in the name as returned by the index method. (You should obtain values of 4, 4, 5 and 6, respectively, for the items in the list above.)

This is a good example in which it is smart to write and test versions of the script that incrementally build toward the desired result rather than trying to write the final version in one fell swoop. For example, you might start by setting up a loop and simply printing each name. If you get that to work, give yourself a little pat on the back and then see if you can simply print the positions of the space. Once you get that working, then try plugging the name and space positions into the larger message.

Practice 1 Solution

Convert the names to a "Last, First" format

Build on Exercise 1 by printing each name in the list in the following format:

Last, First

To do this, you'll need to find the position of the space just as before. To extract part of a string, you can specify the start character and the end character in brackets after the string's name, as in the following:

name = "Joe Paterno"
print name[4:11]  # prints Paterno

One quirky thing about this syntax is that you need to specify the end character as 1 beyond the one you really want. The "o" in "Paterno" is really at position 10, but I needed to specify a value of 11.

One handy feature of the syntax is that you may omit the end character index if you want everything after the start character. Thus, name[4:] will return the same string as name[4:11] in this example. Likewise, the start character may be omitted to obtain everything from the beginning of the string to the specified end character (-1).

Practice 2 Solution

Convert scores to letter grades

Write a script that accepts a score from 1-100 as an input parameter, then reports the letter grade for that score. Assign letter grades as follows:

A: 90-100
B: 80-89
C: 70-79
D: 60-69
F: <60

Practice 3 Solution

Create copies of a template shapefile

Imagine that you're again working with the Nebraska precipitation data from Lesson 1 and that you want to create copies of the Precip2008Readings shapefile for the next 4 years after 2008 (e.g., Precip2009Readings, Precip2010Readings, etc.). Essentially, you want to copy the attribute schema of the 2008 shapefile, but not the data points themselves. Those will be added later. The tool for automating this kind of operation is the Create Feature Class tool in the Data Management toolbox. Look up this tool in the Help system and examine its syntax and the example script. Note the optional template parameter, which allows you to specify a feature class whose attribute schema you want to copy. Also note that Esri uses some inconsistent casing with this tool and you will have to call arcpy.CreateFeatureclass_management() using a lower-case "c" on "class." If you follow the examples in the Geoprocessing Tool Reference help you will be fine.

To complete this exercise, you should invoke the Create Feature Class tool inside a loop that will cause the tool to be run once for each desired year. The range(...) function can be used to produce the list of years for your loop.

Practice 4 Solution

Clip all feature classes in a geodatabase

The data for this practice exercise consists of two file geodatabases: one for the USA and one for just the state of Iowa. The USA dataset contains miscellaneous feature classes. The Iowa file geodatabase is empty except for an Iowa state boundary feature class.

Download the data

Your task is to write a script that programmatically clips all the feature classes in the USA geodatabase to the Iowa state boundary. The clipped feature classes should be written to the Iowa geodatabase. Append "Iowa" to the beginning of all the clipped feature class names.

Your script should be flexible enough that it could handle any number of feature classes in the USA geodatabase. For example, if there were 15 feature classes in the USA geodatabase instead of three, your final code should not need to change in any way.

Practice 5 Solution