GEOG 485:
GIS Programming and Software Development

Lesson 3 Practice Exercise D Solution

PrintPrint

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)

Click here for transcript of Solution to Lesson 3, Practice Exercise D.

In this video, I'll be explaining one solution to Lesson 3 Practice Exercise D, in which we need to select park and ride facilities in a given target city and export them to a new feature class.

This solution will have a lot of elements that were used in Exercises B and C. These patterns should start to look familiar to you.

In line 4, I import the arcpy site package, and then in line 6, I set up a variable to represent the city on which I want to query for park and rides. I'm going to be doing an attribute query for this city, and then I'm going to follow it up with a spatial query to just get the park and rides that fall within that selected city. And putting line 6 at the top like this allows me to change the city, so I can test with different values without hunting through my code and finding where that is.

In line 7, I set up the workspace, which is going to be my file geodatabase. I'll be working with 3 different feature classes, all in the same geodatabase, so setting the workspace like this makes it easier for me to refer to those feature classes.

In lines 8 and 9, I set up variables to store the names of the two input feature classes.

The rest of the code, some of it could go within try, except, and finally blocks. For simplicity, I've left those out here, although for code quality, you're asked to include those in appropriate places in your project submissions.

What I do next on line 13 is set up the SQL query string to get just the city of Federal Way on its own. So, I'm doing this attribute query here, similar to what was done in Exercise B. I'm querying on the NAME field for the city. The expression as a whole being a string needs to be in quotes. And I use string concatenation to plug the name of the target city inside single quotes.

Once I have my query string, on line 16 I can perform a selection in which I end up with a feature layer of just that target city.

And by now, the use of SelectLayerByAttribute should be familiar to you. The first parameter is the name of the feature class that I'm querying. And remember, in line 10, I set up a variable for that. The second parameter is the selection method I want to use. And then the third parameter is the SQL query expression, and it's what will narrow down the cities to, in this case, just the city of Federal Way.

Now that I have a feature layer representing Federal Way, I can now do a Select Layer by Location so that I can get just the park and rides that fall within that city. So, I pass in the ParkAndRide feature class, the type of spatial relationship which I'm using, which is Contained By, which you've seen in previous practice exercises.

And then it’s the city layer that I want to use to do the selection. So, that's the third parameter. Once I have the selection made, then I can copy those selected features into a new feature class. And just like you saw in Practice Exercise C, I'm using the Copy Features tool, and I specify my ParkAndRide feature layer as the source of the features that I want to copy.

And then the second parameter is the name of the new feature class that will be created. Because I only specified a name and not a path, the new feature class is going to be saved in the workspace, which is the Washington file geodatabase. And I'm going to call that feature class TargetParkAndRideFacilities.

Now at the end of your code, preferably within a Finally block or somewhere at the end, you're going to delete the feature layers to clean them up. And in this case, we have two feature layers to clean up.

And that's all it takes to complete this exercise.

Credit: S. Quinn, J. Detwiler © Penn State is licensed under CC BY-NC-SA 4.0.

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:

Alternate Solution to Lesson 3, Practice Exercise D (1:36)

Click here for transcript of Alternate Solution to Lesson 3, Practice Exercise D.

This video shows an alternate solution to Lesson 3 Practice Exercise D.

Everything is the same in this version down until line 16. On that line, I use MakeFeatureLayer to create a feature layer containing just the target city. I give that feature layer object a name of “CityLayer”.

Then on line 17, I create another FeatureLayer, this one of all the park and ride facilities. In this line, I only pass in two parameters because I want all of the park and rides. So, I'm not going to pass in any type of query string here.

With those two layers created, I can now do a Select Layer by Location so that I can get just the park and rides that fall within the selected city. So in this use of the tool, the inputs I’m supplying are strings, that are the names that I assigned to the Feature Layer objects. That’s instead of a feature layer held in a variable and the name of a feature class, as I did it in the first solution.

So coming into line 20, “ParkAndRideLayer” refers to all 365 ParkAndRide features. After line 20, it refers to just the 8 features that fall within the boundaries of Federal Way.

I then insert the name of that layer into a call to the CopyFeatures tool and then finish up my cleaning up the two feature layers.

Credit: S. Quinn, J. Detwiler © Penn State is licensed under CC BY-NC-SA 4.0.