Before tackling this tutorial, you will need to download and install a dataset following these instructions:
Create a folder called Selection_exercise somewhere under your personal directory (e.g. C:\Users\jdoe\Documents\Tutorials\Selection_exercise\).
Download the data for this exercise then extract the contents of Selection_exercise.zip into your newly created Selection_exercise folder.
Open the Selections.aprx file.
The map document consists of a polygon layer and a point layer. The Schools point layer lists school names. The Counties polygon layer lists various county statistics including the fraction of the population not having attained a high school degree, Frac_No_HS, and the fraction of the population having attained a bachelors degree, Frac_Bach.
Familiarize yourself with both attribute tables before proceeding with this tutorial.
In the following series of examples, you will learn how to subset features based on attribute queries. These exercises will make use of the Select Layer by Attribute geoprocessing tool.
To access the geoprocessing tool, click on the Geoprocessing toolbox under the View tab.
In the Geoprocessing pane (which should be docked on the right side of the window), type Select Layer by Attribute in the Search window to find the tool.
The first search result should be the geoprocessing tool of interest.
Click on Select Layer By Attribute to activate it.
You will use this tool to tackle the next three queries.
Note that a shortcut to the tool can be found under the Map tab
Here, the attribute of interest is Frac_No_HS. We will build a query expression around this field.
Select Counties from the pull-down option in the Input Rows field.
Choose New selection as the selection type.
Fill the fields as follows. It should read:
Where Frac_No_HS is greater than or equal to 0.15
Click OK (if you accessed the tool from the ribbon) or Run (if you accessed the tool from the geoprocessing toolbox) to complete the selection.
Alternatively, you could have made use of an SQL expression. SQL is a popular language used with relational databases.
To use the SQL option, flip the SQL switch and type the following expression:
Frac_No_HS >= 0.15
(If you already defined the expression in the earlier step, this expression will already appear in the expression box).
When done, you should see several selected polygons, 10 to be exact.
To see the exact number of selected polygons, click the List by Selection tab in the Contents pane
Note that when you have the List by Selection selected in the Contents pane, the check boxes next to the layers do not turn the layers on and off. Instead, they enable or disable graphical selection of the layer (i.e. selections that make use of your mouse).
This new query will make use of two expressions that will be combined using the And boolean operator.
If you are continuing from the previous step, you will already have satisfied the first criterion. If not, do so before continuing on to the next step.
Click on the Add Clause link to add the second expression that needs to be satisfied.
Add the expression Frac_Bach is greater than or equal to 0.20. You will join this expression with the previous one using the And boolean operator.
Click OK (if you accessed the tool from the ribbon) or Run (if you accessed the tool from the geoprocessing toolbox) to complete the selection.
You should have 5 polygons that satisfy the two criteria.
NOTE: Do not clear the selected polygon features! They will be exported to a new shapefile later in this tutorial.
Before starting this exercise, you will need to turn the Schools point layer on if it's not already activated. If you are in the List by Selection mode, you will need to switch back to the List by Drawing Order mode in the Contents pane.
This next query has you working with the Schools point layer. However, this layer does not have an attribute that identifies the type of school (e.g. High School, College, etc...). So, this query will need to make use of a regular expression whereby we will look for key words in the Name column. More specifically, we will look for the words "High School" (this is, of course, a limiting assumption given that many high schools do not have those words in their names).
Note, however, that there is a problem with this approach in that the words "High School" can also be found in "Junior High School". So to avoid selecting Junior High Schools we will combine two expressions whereby one of the expressions will remove all instances of "Junior High School".
Create a new Select Layer By Attribute query as shown below.
Note that the expression is case sensitive. So the text "high school" is treated differently from "High School"
The SQL equivalent should look like:
NAME LIKE '%High School%' And NAME NOT LIKE '%Junior High School%'
Click OK (if you accessed the tool from the ribbon) or Run (if you accessed the tool from the geoprocessing toolbox) to complete the selection.
You should have 858 polygons that satisfy the two criteria. (Don't forget to switch back to List by Selection mode to see the number of selected points).
So far, this exercise has shown you how to identify features that meet a series of criteria. But some workflows might require that the selected features be saved as a dedicated GIS data file. In this next step, you will export the selected points to a new shapefile called High_schools.shp.
With the high school points selected, right-click on the Schools layer and select Data >> Export Features.
Make sure that the blue Selection switch is turned on (this ensures that just those selected features will be exported)
Set the output location to the Selection_exercise folder and the output filename to high_schools.shp.
Click OK to run the geoprocess.
The high_schools shapefile should be automatically added to your Contents pane.
On your own, export the 5 counties selected in an earlier step to a shapefile that you will name bac_and_NoHS.shp.
Make sure that the county polygons are selected before exporting the features. If they are not, ALL features will be exported to the new GIS file.
When running most geoprocessing tools (including exporting features), if one or more features are selected, only those selected features will take part in subsequent geoprocesses. So any selection should only persist as long as is needed. In this example we exported the selected features to dedicated shapefiles, so we can now clear the selections from the layer.
To clear the selected points in just the Schools layer (i.e. without clearing the selection from other layers in your project), right-click on the Schools layer and click on Selection >> Clear Selection.
On your own, clear the selection in the Counties layer.
Note that if you wanted to clear all selections from all layers in your current map, you could have clicked on the Clear button under the Map tab.
The Selection tool allows for more complex queries whereby selections can be nested. For example, let's select features in the Counties layer where one of two sets of conditions are met. Here, we'll identify all features where the fraction of the population not having attained a high school degree is between 0.05 and 0.1 (exclusive), or where the fraction of the population having attained a bachelor's degree is between 0.3 and 0.4 (exclusive). The expression looks something like this:
(Frac_No_HS > 0.05 AND Frac_No_HS < 0.1) OR (Frac_Bach > 0.3 AND Frac_Bach < 0.4)
Note the OR boolean operator that combines both sets of queries.
The above can be written out as an expression in the SQL query window. It can also be created in the point-and-click window as shown next.
In the attribute selection tool, clear any existing selections you might have by clicking on the Remove button
Add two clauses that satisfy the Frac_No_HS query. Note that the two queries are tied together by the AND operator.
To nest these combined queries, select the leftmost tabs for both clauses while holding down the shift key.
You will know when you have properly selected the expressions by the blue highlight around each clause.
With both clauses selected, click the Grouping button just above the clauses.
Once grouped, the two clauses should be indented.
Next, add two more clauses that satisfy the Frac_Bach queries.
The first clause must be preceded with the OR operator. But, note that the two clauses are linked by an AND operator.
Next, select these last two clauses (while holding down the shift key) and group them.
They should now be indented like the first two clauses
You can check that the query is properly constructed by flipping on the SQL mode. Note the use of the parentheses that explicitly define the two sub-queries.
Click Run or OK, to complete the selection.
You should have 37 polygons selected.
There is no need to export this selection for this exercise. Feel free to clear the selection when complete.
A word of caution when constructing your queries. It can be easy to inadvertently check the Invert Where Clause radio box.
Doing so will invert the selection!
Note that we already created the two layers that will be needed in this step in earlier steps. These are the high_schools and bac_and_NoHS layers.
In the Geoprocessing search bar, type Select Layer by Location.
Note that you might need to click on the Geoprocessing back arrow button to get back to the Geoprocessing landing pane.
The first search result should highlight the desired tool. Click on it to open it's window,
Note that you can also access this tool from the Map ribbon.
In the Select Layer by Location geoprocessing window, set the Input Features to high_schools. This is the layer that we are selecting from.
The spatial relationship we are interested in is containment. So we will choose the Within option from the pull-down menu in the Relationship field.(i.e. we are seeking all points that are within said polygons).
The bac_and_NoHS layer is the polygon layer that is used in the selection (but is not selected from).
Click Apply or Run.
This should return 124 selected points that satisfy this criterion.
ArcGIS Pro offers a wide selection of spatial relationship types. While most options seem self-explanatory, some may need further explanation. To learn more about these options, view this online help link.
In this example, the relationship in question defines a certain distance between features. But before we start the selection by location procedure, we will need to create a Junior High schools layer from the Schools layer (recall that we extracted the High schools features earlier in this exercise and not the Junior High Schools).
Using the Select Layer by Attribute tool, select all Schools point features that have the words "Junior High School". Then export to a dedicated shapefile that you will name junior_high.shp.
You should end up with 243 selected points.
Once you've exported the Junior High schools points, clear your selection. (Remember that it's good practice to clear your selection when done).
Now that you have a dedicated Junior High schools points layer, you can proceed with the selection by location workflow.
Bring up the Select Layer by Location geoprocess and populate the fields as shown in the accompanying figure.
[Note that if you wish to avoid projected coordinate system distortions, you could choose the Within a distance geodesic option from the Relationship pull-down menu.]
The process should return 464 selected high school points.
Export the selected point to a new shapefile that you will name HS_5mi_JHS.shp.
Clear the selected features.
In this last query, you will invert the previous query. You will identify all high schools that are more than 5 miles from a nearest junior high school. You will, however, note that the selection tool does not have a "more than X distance" option in its spatial relationship pulldown menu. The solution is to make use of the Invert Spatial Relationship option.
Working off of the last query, you will invert the selection by checking the Invert Spatial Relationship box.
The process should return 394 selected high school points.
You do not need to export this selection.
Save and close your project.
This wraps up this tutorial.