GEOG 485:
GIS Programming and Software Development

Lesson 3 Practice Exercise C Solution

PrintPrint

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 = r"C:\PSU\geog485\L3\PracticeExerciseC\Washington.gdb"
arcpy.env.overwriteOutput = True
 
# Set up the SQL expression to query the parking capacity
parkingQuery = "Approx_Par > " + str(parkingSpaces)

# Select the park and rides that applies the SQL expression
parkAndRideLayer = arcpy.SelectLayerByAttribute_management("ParkAndRide", "NEW_SELECTION", parkingQuery)

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

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

Video: Solution to Lesson 3, Practice Exercise C (3:32)

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

This video describes a solution to Lesson 3, Practice Exercise C, which requires selecting park and ride facilities that meet a certain minimum number of parking spaces, and then, copying them to their own new feature class.

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

Putting the variable at the top of the script is helpful in case we want to adjust the value and test with other values. It’s easy to find and not buried down later in our code.

In line 7, I'm setting up the arcpy workspace to be equal to my file geodatabase location.

Line 11 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 the number of parking spaces that was specified above on line 6. So if you were to look at this in Pro, 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 value of 500 into a string so that you can concatenate it with the rest of the query expression. However, you don't need to enclose that value in quotes because when you build queries based on numeric values, the number is not put in quotes.

Once you have that query string all set up, you can run SelectLayerByAttribute to get a ParkAndRide Feature Layer with just those selected park and rides that meet the criterion.

So, there are three parameters to pass in here. The first one is the name of the feature class that we're operating on. Because we set up the workspace, we can just put the name of the feature class rather than its full path. We also could have stored the name of the feature class in a variable and plugged that variable in here.

The second parameter is the selection method (whether we want to create a new selection, add to the existing selection, select from the current selection, etc.). Here we want to create a new selection.

And finally, the third parameter is that SQL query expression.

The tool returns to us a Feature Layer containing the selected ParkAndRide features, which we store in the parkAndRideLayer variable.

Once we have that feature layer, we can run the Copy Features tool to make a brand new feature class with just these selected features. So the two parameters here-- the first one is the variable that holds the feature layer that’s the source of the features we want to copy.  The second parameter, BigParkAndRideFacilities, 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.

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

Below is an alternate approach to the exercise.

# 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 = r"C:\PSU\geog485\L3\PracticeExerciseC\Washington.gdb"
arcpy.env.overwriteOutput = True

# 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")
arcpy.Delete_management("ParkAndRideLayer")

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

Video: Alternate Solution to Lesson 3, Practice Exercise C (1:18)

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

This video describes an alternate solution to Lesson 3, Practice Exercise C. This solution differs from the first one starting on line 14. Instead of using SelectLayerByAttribute and storing its returned Feature Layer in a variable, this version of the script uses the MakeFeatureLayer tool to create the Feature Layer that can be referred to later using the string “ParkAndRideLayer”.

Looking closely at the parameters supplied in line 14, it’s important to note that the first parameter, “ParkAndRide” is specifying the name of a feature class. Found where? Found in the workspace, which we set to the Washington geodatabase on line 7.

The second parameter is also a string, but it’s just the name that we’re giving to the temporary, in-memory feature layer we’re creating.

So in this version of the script, when we want to write the selected features to disk, we plug that named feature layer into the CopyFeatures statement rather than a variable.

And likewise, when we want to clean up the feature layer, we again specify that same name, as a string, rather than a variable.

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