Print
Below is one possible approach to Lesson 3 Practice Exercise D. Notice that the city name is stored near the top of the script in a variable so that it can be tested with other values.
# Selects park and ride facilities in a given target city and # exports them to a new feature class import arcpy targetCity = "Federal Way" # Name of target city arcpy.env.workspace = r"C:\PSU\geog485\L3\PracticeExerciseD\Washington.gdb" arcpy.env.overwriteOutput = True parkAndRideFC = "ParkAndRide" # Name of P & R feature class citiesFC = "CityBoundaries" # Name of city feature class # Set up the SQL expression of the query for the target city cityQuery = "NAME = '" + targetCity + "'" # Select just the target city cityLayer = arcpy.SelectLayerByAttribute_management(citiesFC, "NEW_SELECTION", cityQuery) # Select all park and rides in the target city parkAndRideLayer = arcpy.SelectLayerByLocation_management(parkAndRideFC, "CONTAINED_BY", cityLayer) # Copy the features to a new feature class and clean up arcpy.CopyFeatures_management(parkAndRideLayer, "TargetParkAndRideFacilities") arcpy.Delete_management(parkAndRideLayer) arcpy.Delete_management(cityLayer)
See the video below for some line-by-line commentary on the above solution:
Solution to Lesson 3, Practice Exercise D (4:06)
Here is an alternate solution for this exercise:
# Selects park and ride facilities in a given target city and # exports them to a new feature class import arcpy targetCity = "Federal Way" # Name of target city arcpy.env.workspace = r"C:\PSU\geog485\L3\PracticeExerciseD\Washington.gdb" arcpy.env.overwriteOutput = True parkAndRideFC = "ParkAndRide" # Name of P & R feature class citiesFC = "CityBoundaries" # Name of city feature class # Set up the SQL expression of the query for the target city cityQuery = "NAME = '" + targetCity + "'" # Make feature layers for the target city and park and rides arcpy.MakeFeatureLayer_management(citiesFC, "CityLayer", cityQuery) arcpy.MakeFeatureLayer_management(parkAndRideFC, "ParkAndRideLayer") # Select all park and rides in the target city arcpy.SelectLayerByLocation_management("ParkAndRideLayer", "CONTAINED_BY", "CityLayer") # Copy the features to a new feature class and clean up arcpy.CopyFeatures_management("ParkAndRideLayer", "TargetParkAndRideFacilities") arcpy.Delete_management("ParkAndRideLayer") arcpy.Delete_management("CityLayer")
See the video below for some line-by-line commentary on the above solution: