# This script determines the percentage of cities with two park # and ride facilities import arcpy arcpy.env.overwriteOutput = True cityBoundaries = "D:\\WCGIS\\Geog485\\Lesson3PracticeExerciseB\\Washington.gdb\\CityBoundaries" parkAndRide = "D:\\WCGIS\\Geog485\\Lesson3PracticeExerciseB\\Washington.gdb\\ParkAndRide" parkAndRideField = "HasTwoParkAndRides" cityIDStringField = "CI_FIPS" citiesWithTwoParkAndRides = 0 numCities = 0 # Make a feature layer of all the park and ride facilities arcpy.MakeFeatureLayer_management( parkAndRide, "ParkAndRide_Layer") # Make an update cursor and loop through each city with arcpy.da.UpdateCursor(cityBoundaries, (cityIDStringField, parkAndRideField)) as cursor: for ciy in cursor: # Create a query string for the current city cityID = city[0] queryString = '"' + cityIDStringField+ '" =' + "'" cityIDString + "'" # Make a feature layer of just the current city polygon arcpy.MakeFeatureLayer_management( cityBoundaries, "CurrentCity_Layer", queryString ) try: # Narrow down the park and ride layer by selecting only the park and rides # in the current city arcpy.SelectLayerByLocation_management("ParkAndRide_Layer", "CONTAINED_BY", "CurrentCity_Layer") # Count the number of cities selected SelectedParkAndRideCount = int(arcpy.GetCount_management("ParkAndRide_Layer".getOutput(0)) # If more the one park and ride found, update the row to TRUE if SelectedParkAndRideCount > 1: city[1] = True cursor.updateRow(city) # Add 1 to your tally of cities with two park and rides citiesWithTwoParkAndRides += 1 finally: # Delete current cities layer to prepare for next run of loop # Clean up park and ride feature layer # Calculate and report the number of cities with two park and rides if else:
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question