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. import arcpy from arcpy import env env.workspace = "path" featureclass = CityBoundaries cursor = arcpy.da.SearchCursor PandR = 0 cityBoundaries = "C:\\Data\\Lesson3PracticeExerciseA\\Washington.gdb\\CityBoundaries" parkAndRide = "C:\\Data\\Lesson3PracticeExerciseA\\Washington.gdb\\ParkAndRide" parkAndRideField = "HasParkAndRide" citiesWithParkAndRide = 0 # Make a feature layer of all the cities polygons arcpy.MakeFeatureLayer_management(parkAndRide, "ParkAndRideLayer") # Make a feature layer of all the cities polygons arcpy.MakeFeatureLayer_management(cityBoundaries, "CitiesLayer") #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. arcpy.SelectLayerByLocation_management ("CitiesLayer", "CONTAINS", "ParkAndRideLayer") #Create an update cursor for your now narrowed-down "CitiesLayer" and loop through each record, setting the HasParkAndRide field #to "True." selectedcities = arcpy.da.UpdateCursor("CitiesLayer") with arcpy.da.UpdateCursor("CitiesLayer", (parkAndRideField,)) as cursor: row[0] = "True" cursor.updateRow(row) citiesWithParkAndRide +=1 numCities = int(arcpy.GetCount_management(cityBoundaries).getOutput(0)) percentCitiesWithPandR = (citiesWithParkAndRide/numCities) * 100
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