GEOG 485:
GIS Programming and Automation

Lesson 3 Practice Exercise D Solution


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. The SQL expression takes a little bit of work to create due to the string query using two different types of quotes.

Note that the Python code colorizer below makes a mistake in line 11 and paints the variable targetCity green when it should be black.

# 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 = "C:\\data\\Geog485\\Lesson3PracticeExercises\\Lesson3PracticeExerciseD\\Washington.gdb"
parkAndRide = "ParkAndRide"    # Name of P & R feature class
cities = "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 cities and park and rides
arcpy.MakeFeatureLayer_management(cities, "CitiesLayer", cityQuery)
arcpy.MakeFeatureLayer_management(parkAndRide, "ParkAndRideLayer")

# Select all park and rides in the target city
arcpy.SelectLayerByLocation_management("ParkAndRideLayer", "CONTAINED_BY", "CitiesLayer")

# Copy the features to a new feature class and clean up
arcpy.CopyFeatures_management("ParkAndRideLayer", "TargetParkAndRideFacilities")


See the video below for some line-by-line commentary on the above solution:

Click for a transcript of "3D" video.

PRESENTER: 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.

And 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 5 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 5 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 6 I set up the workspace, which is going to be my file geodatabase. This was a technique that I used also in Exercise C's solution, which is convenient, because I'll be working with several feature classes here. In lines 7 and 8 I decided to go ahead here and set up some variables that represent the names of those feature classes. This is not necessary. I could just plug in these strings into the functions in lines 14 and 15.

But sometimes, when I'm working with a lot of data sets, I like to set up everything at the beginning just so I can see exactly what I'm working with. And so this is optional.

The rest of the code, some of it could go within try, accept, 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'll do first here is go ahead in line 11 and 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 field name needs to go in double quotes in the string. So I'm enclosing the first part of the string in single quotes.

The name of the city has to go within single quotes in the query. So I have to do a little bit of string concatenation here and enclose each of those single quotes in double quotes, and stick everything together so I have that full query string. And once I have it, in line 14, I can make it a feature layer of just that target city. This will get just the city of Federal Way.

And by now this pattern of making the feature layer and passing in the parameters should be familiar to you. The first parameter is the name of the feature class that I'm using. And remember in line 8 I set up a variable for that. The second parameter is the name I want to give this feature layer throughout this script. I'm going to call it cities layer.

And then the third parameter is the optional SQL query expression, and it's what will narrow down the cities to just be one city, in this case. In line 15 I'm going to make a feature layer of all the park and ride facilities. And in line 15 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.

So the two parameters are the variable I set up in line 7 that gets the park and ride feature class, and then the name I want to give to this feature layer in the script, which I just called it park and ride layer.

With all of those elements, 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 I pass in the park and ride layer and the type of spatial relationship which I'm using, which is contained by, which you've seen in previous practice exercises.

And then I'm working with the cities layer to do the selection. So that's the third parameter. Once I have that 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 give it my park and ride feature layer.

And then the second parameter here is the name of the new feature class that will be created. It's going to go into that workspace, which is the Washington file geodatabase. And I'm going to call that feature request Target Park and Ride Facilities.

Now at the end of your code, probably 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.