Attribute queries can be executed against a feature class or table through the use of the Select Layer by Attribute tool. A where
clause can be included to filter the selected records and various selection types can be included.
The Select Layer by Attribute tool, shown in the following screenshot, is used to select records from a feature class or table based on a query that you define. We covered the somewhat complex topic of queries in an earlier recipe in this chapter, so hopefully, you now understand the basic concepts of creating a query. You have also learned how to create a temporary, in-memory representation of a feature class or table, which is a pre-requisite to using either the Select by Attributes or Select by Location tool.
The Select by Attributes tool uses a query along with either a feature layer or table view and a selection method to select records. By default, the selection method will be a new selection set. Other selection methods include "add to selection", "remove from selection", "subset selection", "switch selection", and "clear selection". Each of the selection methods is summarized as follows:
NEW_SELECTION
: This is the default selection method and it creates a new selection setADD_TO_SELECTION
: This adds a selection set to the currently selected records based on a queryREMOVE_FROM_SELECTION
: This removes records from a selection set based on a querySUBSET_SELECTION
: This combines selected records that are common to the existing selection setSWITCH_SELECTION
: This selects records that are not selected currently and unselects the existing selection setCLEAR_SELECTION
: This clears all records that are currently a part of the selected setThe syntax to call the Select by Attributes tool is as follows:
arcpy.SelectLayerByAttribute_management(<input feature layer or table view>, {selection method}, {where clause})
In this recipe, you'll learn how to use the Select by Attributes tool to select features from a feature class. You'll use the skills you learned in previous recipes to build a query, create a feature layer, and finally call the Select by Attributes tool.
Follow these steps to learn how to select records from a table or feature class using the Select Layer by Attributes tool:
c:ArcpyBookCh7SelectLayerAttribute.py
.arcpy
module:import arcpy
arcpy.env.workspace = "c:/ArcpyBook/data/CityOfSanAntonio.gdb"
try
block:try:
where
clause that will select all the records with a service area of North
. This line of code and the next four should be indented:qry = '"SVCAREA" = 'North''
Burglary
feature class:flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer")
arcpy.SelectLayerByAttribute_management(flayer, "NEW_SELECTION", qry)
cnt = arcpy.GetCount_management(flayer) print("The number of selected records is: " + str(cnt))
except
block and a line of code to print an error message in the event of a problem:except Exception as e: print(e.message)
try
and except
blocks:import arcpy arcpy.env.workspace = "c:/ArcpyBook/data/CityOfSanAntonio.gdb" try: qry = '"SVCAREA" = 'North'' flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer") arcpy.SelectLayerByAttribute_management(flayer, "NEW_SELECTION", qry) cnt = arcpy.GetCount_management(flayer) print("The number of selected records is: " + str(cnt)) except Exception as e: print(e.message)
c:ArcpyBookcodeCh7SelectLayerAttribute.py
solution file.7520
records have been selected:The total number of selected records is: 7520
The Select by Attributes tool requires that either a feature layer or table view be passed as the first parameter. In this recipe, we passed a feature layer that was created by the Make Feature Layer tool in the preceding line. We used Make Feature Layer to create a feature layer from the Burglary
feature class. This feature layer was assigned to the flayer
variable, which is then passed into the Select by Attribute tool as the first parameter. In this script, we also passed in a parameter indicating that we'd like to create a new selection set along with the final
parameter, which is a where
clause. The where
clause is specified in the qry
variable. This variable holds a query that will select all the features with a service area of North
.