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.
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).
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 you may have to monkey around with the output a bit to get it in a format you can use. See the example in the Esri documentation that converts the GetCount result to an integer.
- 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.