Print
Below is one possible solution to Practice Exercise A with comments to explain what is going on. If you find a more efficient way to code a solution, please share it through the discussion forums. Please note that in order to make the changes to citiesLayer permanent, you have to write the layer back to disk using the arcpy.CopyFeatures_management(...) function. This is not shown in the solution here.
# This script determines the percentage of cities in the # state with park and ride facilities import arcpy arcpy.env.overwriteOutput = True arcpy.env.workspace = r"C:\PSU\geog485\L3\PracticeExerciseA\Washington.gdb" cityBoundariesFC = "CityBoundaries" parkAndRideFC = "ParkAndRide" parkAndRideField = "HasParkAndRide" # Name of column with Park & Ride information citiesWithParkAndRide = 0 # Used for counting cities with Park & Ride try: # Narrow down the cities layer to only the cities that contain a park and ride citiesLayer = arcpy.SelectLayerByLocation_management(cityBoundariesFC, "CONTAINS", parkAndRideFC) # Create an update cursor and loop through the selected records with arcpy.da.UpdateCursor(citiesLayer, (parkAndRideField)) as cursor: for row in cursor: # Set the park and ride field to TRUE and keep a tally row[0] = "True" cursor.updateRow(row) citiesWithParkAndRide += 1 except: print ("There was a problem performing the spatial selection or updating the cities feature class") # Delete the feature layers even if there is an exception (error) raised finally: arcpy.Delete_management(citiesLayer) del row, cursor # Count the total number of cities (this tool saves you a loop) numCitiesCount = arcpy.GetCount_management(cityBoundariesFC) numCities = int(numCitiesCount[0]) # Get the number of cities in the feature layer #citiesWithParkAndRide = int(citiesLayer[2]) # Calculate the percentage and print it for the user percentCitiesWithParkAndRide = (citiesWithParkAndRide / numCities) * 100 print (str(round(percentCitiesWithParkAndRide,1)) + " percent of cities have a park and ride.")
Below is a video offering some line-by-line commentary on the structure of this solution:
Video: One Solution to Lesson 3, Practice Exercise A (9:37)
Here is a different solution to Practice Exercise A, which uses the alternate syntax discussed in the lesson:
# This script determines the percentage of cities in the # state with park and ride facilities import arcpy arcpy.env.overwriteOutput = True arcpy.env.workspace = r"C:\PSU\geog485\L3\PracticeExerciseA\Washington.gdb" cityBoundariesFC = "CityBoundaries" parkAndRideFC = "ParkAndRide" parkAndRideField = "HasParkAndRide" # Name of column with Park & Ride information citiesWithParkAndRide = 0 # Used for counting cities with Park & Ride try: # Make a feature layer of all the park and ride facilities arcpy.MakeFeatureLayer_management(parkAndRideFC, "ParkAndRideLayer") # Make a feature layer of all the cities polygons arcpy.MakeFeatureLayer_management(cityBoundariesFC, "CitiesLayer") except: print ("Could not create feature layers") try: # Narrow down the cities layer to only the cities that contain a park and ride arcpy.SelectLayerByLocation_management("CitiesLayer", "CONTAINS", "ParkAndRideLayer") # Create an update cursor and loop through the selected records with arcpy.da.UpdateCursor("CitiesLayer", (parkAndRideField)) as cursor: for row in cursor: # Set the park and ride field to TRUE and keep a tally row[0] = "True" cursor.updateRow(row) citiesWithParkAndRide +=1 except: print ("There was a problem performing the spatial selection or updating the cities feature class") # Delete the feature layers even if there is an exception (error) raised finally: arcpy.Delete_management("ParkAndRideLayer") arcpy.Delete_management("CitiesLayer") del row, cursor # Count the total number of cities (this tool saves you a loop) numCitiesCount = arcpy.GetCount_management(cityBoundariesFC) numCities = int(numCitiesCount[0]) # Calculate the percentage and print it for the user percentCitiesWithParkAndRide = (citiesWithParkAndRide / numCities) * 100 print (str(round(percentCitiesWithParkAndRide,1)) + " percent of cities have a park and ride.")
Below is a video offering some line-by-line commentary on the structure of this solution: