Selecting features and rows with the Select Layer by Attribute tool

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.

Getting ready

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.

Getting ready

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 set
  • ADD_TO_SELECTION: This adds a selection set to the currently selected records based on a query
  • REMOVE_FROM_SELECTION: This removes records from a selection set based on a query
  • SUBSET_SELECTION: This combines selected records that are common to the existing selection set
  • SWITCH_SELECTION: This selects records that are not selected currently and unselects the existing selection set
  • CLEAR_SELECTION: This clears all records that are currently a part of the selected set

The 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.

How to do it…

Follow these steps to learn how to select records from a table or feature class using the Select Layer by Attributes tool:

  1. Open IDLE and create a new script window.
  2. Save the script to c:ArcpyBookCh7SelectLayerAttribute.py.
  3. Import the arcpy module:
    import arcpy
  4. Set the workspace to the City of San Antonio geodatabase.
    arcpy.env.workspace = "c:/ArcpyBook/data/CityOfSanAntonio.gdb"
  5. Start a try block:
    try:
  6. Create the query that we used in the first recipe in this chapter. This will serve as a 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''
  7. Make an in-memory copy of the Burglary feature class:
    flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer")
  8. Call the Select Layer by Attribute tool by passing in a reference to the feature layer we just created. Define this as a new selection set and pass in a reference to the query:
    arcpy.SelectLayerByAttribute_management(flayer, "NEW_SELECTION", qry)
  9. Print the number of selected records in the layer using the Get Count tool:
    cnt = arcpy.GetCount_management(flayer)
    print("The number of selected records is: " + str(cnt))
  10. Add an 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)
  11. The entire script should appear as shown in the following code snippet. Please remember to include indentation with the 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)
  12. Save the script.
  13. You can check your work by examining the c:ArcpyBookcodeCh7SelectLayerAttribute.py solution file.
  14. Run the script. If everything has been done correctly, you should see a message indicating that 7520 records have been selected:
    The total number of selected records is: 7520
    

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset