# This script determines the percentage of cities in the
#  state with park and ride facilities
 
import arcpy
arcpy.env.overwriteOutput = True
 
cityBoundaries = "D:\\Data\\Geog485\\Lesson3PracticeExerciseA\\Washington.gdb\\CityBoundaries"
parkAndRide = "D:\\Data\\Geog485\\Lesson3PracticeExerciseA\\Washington.gdb\\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(parkAndRide, "ParkAndRideLayer")
 
    # Make a feature layer of all the cities polygons   
    arcpy.MakeFeatureLayer_management(cityBoundaries, "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
    
# Delete the feature layers even if there is an exception (error) raised
finally: 
    arcpy.Delete_management("ParkAndRideLayer")
    arcpy.Delete_management("CitiesLayer")
     
# Count the total number of cities (this tool saves you a loop)
numCitiesCount = arcpy.GetCount_management(cityBoundaries)
numCities = int(numCitiesCount.getOutput(0))
 
# Calculate the percentage and print it for the user
percentCitiesWithParkAndRide = ((1.0 * citiesWithParkAndRide) / numCities) * 100
 
print (str(percentCitiesWithParkAndRide) + " percent of cities have a park and ride.")
# This script determines the percentage of cities in the
#  state with park and ride facilities
 
import arcpy
arcpy.env.overwriteOutput = True
 
cityBoundaries = "D:\\Data\\Geog485\\Lesson3PracticeExerciseA\\Washington.gdb\\CityBoundaries"
parkAndRide = "D:\\Data\\Geog485\\Lesson3PracticeExerciseA\\Washington.gdb\\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(parkAndRide, "ParkAndRideLayer")
 
    # Make a feature layer of all the cities polygons   
    arcpy.MakeFeatureLayer_management(cityBoundaries, "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
    
# Delete the feature layers even if there is an exception (error) raised
finally: 
    arcpy.Delete_management("ParkAndRideLayer")
    arcpy.Delete_management("CitiesLayer")
     
# Count the total number of cities (this tool saves you a loop)
numCitiesCount = arcpy.GetCount_management(cityBoundaries)
numCities = int(numCitiesCount.getOutput(0))
 
# Calculate the percentage and print it for the user
percentCitiesWithParkAndRide = ((1.0 * citiesWithParkAndRide) / numCities) * 100
 
print (str(percentCitiesWithParkAndRide) + " percent of cities have a park and ride.")
# This script determines the percentage of cities in the #  state with park and ride facilities # Name of column with Park & Ride information # Used for counting cities with Park & Ride # Make a feature layer of all the park and ride facilities # Make a feature layer of all the cities polygons # Narrow down the cities layer to only the cities that contain a park and ride # Create an update cursor and loop through the selected records # Set the park and ride field to TRUE and keep a tally # Delete the feature layers even if there is an exception (error) raised # Count the total number of cities (this tool saves you a loop) # Calculate the percentage and print it for the user