GEOG 485:
GIS Programming and Software Development

Lesson 3 Practice Exercise C Solution


Below is one approach to Lesson 3 Practice Exercise C. The number of spaces to query is stored in a variable at the top of the script, allowing for easy testing with other values.

# Selects park and ride facilities with over a certain number of parking spots
#  and exports them to a new feature class using CopyFeatures

import arcpy
parkingSpaces = 500
arcpy.env.workspace = "C:\\data\\Geog485\\Lesson3PracticeExercises\\Lesson3PracticeExerciseC\\Washington.gdb"

# Set up the SQL expression to query the parking capacity
parkingQuery = '"Approx_Par" > ' + str(parkingSpaces)

# Make a feature layer of park and rides that applies the SQL expression
arcpy.MakeFeatureLayer_management("ParkAndRide", "ParkAndRideLayer", parkingQuery)

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

The video below offers some line-by-line commentary on the structure of the above solution:

Click for a transcript of "3C" video.

PRESENTER: This video describes a solution to lesson 3 of practice exercise C, wherein we want to select park and ride facilities that meet a certain threshold of a minimum number of parking spaces, and then, copy them to their own new feature class.

After importing the arcpy site package in line 4, we set up the variable representing that threshold. So in this case, we set that equal to 500 parking spaces.

Putting the variable at the top of the script is helpful so that number is not buried down later in our code. And so if we want to adjust this value and test with other values, it's easy to find.

In line 6, I'm setting up the arcpy workspace to be equal to my file geodatabase location. This is a little different from some of the things shown in the other practice exercise videos. But it's a nice way to work with file geodatabases, in the sense that, from now on, if you refer to a feature class, you only have to use it's name. You don't have to put the entire path. And you'll see that occur later in this script.

Line 9 is probably the most critical line of this script. It's setting up the SQL query expression to get park and ride facilities that have greater than that number of parking spaces. So if you were to look at this in ArcMap, where the park and ride facilities are the black dots, we would do a Select By Attributes. And we query on that attribute Approx_Par is greater than 500. And that would give us those large park and rides that are primarily found in the Seattle and Tacoma areas.

Notice that you need to convert that integer of 500 into a string, just for the purpose of putting it into this query expression. However, you don't need to enclose it in any type of special quotes. The field name, you do have to put into double quotes, and that's why I've surrounded this part of the expression with single quotes, as you learned in your lesson materials.

Once you have that query string all set up, you can run MakeFeatureLayer to get a Park and Rides Feature Layer with just those selected park and rides that meet the criteria.

So there's three parameters to pass in here. The first one is the name of the feature class that were operating on. And again, because we set up the workspace, we can just put that we're working with the Park and Ride Feature Class.

The second parameter is the name of the feature layer. And we could name this anything here. It's just going to reside in the computer's memory, as long as we're consistent.

And then the third parameter is that SQL query expression. This is optional. If we omitted this, we would just get everything. But we want to just have those park and ride facilities that meet the criteria.

Once we have that feature layer, we can run the copy features tool to make a brand new feature class with just these selected elements. So the two parameters here-- the first one is the name of the feature layer that we're working with. And, in line 12, remember that we called this Park and Ride Layer.

The second parameter, Big Park and Ride Facilities, is the name of the new feature class that we want to create. And so once we've done that, we have a new feature class and we can run the Delete tool to clean up our feature layer. And that's all we need to do in this script.