GEOG 485:
GIS Programming and Software Development

Lesson 3 Practice Exercise A

PrintPrint

In this practice exercise, you will programmatically select features by location and update a field for the selected features. You'll also use your selection to perform a calculation.

In your Lesson3PracticeExerciseA folder, you have a Washington geodatabase with two feature classes:

  • CityBoundaries - Contains polygon boundaries of cities in Washington over 10 square kilometers in size.
  • ParkAndRide - Contains point features representing park and ride facilities where commuters can leave their cars.

The objective

You want to find out which cities contain park and ride facilities and what percentage of cities have at least one facility.

  • The CityBoundaries feature class has a field "HasParkAndRide," which is set to "False" by default. Your job is to mark this field "True" for every city containing at least one park and ride facility within its boundaries.
  • Your script should also calculate the percentage of cities that have a park and ride facility and print this figure for the user.

You do not have to make a script tool for this assignment. You can hard-code the variable values. Try to group the hard-coded string variables at the beginning of the script.

For the purposes of these practice exercises, assume that each point in the ParkAndRide dataset represents one valid park and ride (ignore the value in the TYPE field).

Tips

You can jump into the assignment at this point, or read the following tips to give you some guidance.

  • Make two feature layers: "CitiesLayer" and "ParkAndRideLayer."
  • Use SelectLayerByLocation with a relationship type of "CONTAINS" to narrow down your cities feature layer list to only the cities that contain park and rides.
  • Create an update cursor for your now narrowed-down "CitiesLayer" and loop through each record, setting the HasParkAndRide field to "True."
  • To calculate the percentage of cities with park and rides, you'll need to know the total number of cities. You can use the GetCount tool to get a total without writing a loop. Beware that this tool has an unintuitive return value that isn't well documented.  The return value is a Result object, and the count value itself can be retrieved through that object by appending [0] (see the second code example from the Help).  This will return the count as a string though, so you'll want to cast to an integer before trying to use it in an arithmetic expression.
  • Similarly, you may have to play around with your Python math a little to get a nice percentage figure. Don't get too hung up on this part.