Combining a spatial and attribute query with the Select by Location tool

There may be times when you may want to select features using a combined attribute and spatial query. For example, you might want to select all burglaries within the Edgewood school district that occurred on a Monday. This can be accomplished by running the Select by Location and Select by Attributes tools sequentially and applying a SUBSET SELECTION selection type.

Getting ready

This recipe will require that you create a feature layer that will serve as a temporary layer, which will be used with the Select by Location and Select Layer by Attributes tools. The Select by Location tool will find all burglaries that are within the Edgewood School District and apply a selection set to these features. The Select Layer by Attributes tool uses the same temporary feature layer and applies a where clause that finds all burglaries that occurred on a particular Monday. In addition to this, the tool also specifies that the selection should be a subset of the currently selected features found by the Select by Location tool. Finally, you'll print the total number of records that were selected by the combined spatial and attribute query.

How to do it...

  1. Open IDLE and create a new script window.
  2. Save the script as c:ArcpyBookCh7SpatialAttributeQuery.py.
  3. Import the arcpy module:
    import arcpy 
  4. Set the workspace to the geodatabase of the City of San Antonio:
    arcpy.env.workspace = "c:/ArcpyBook/data/CityofSanAntonio.gdb"
  5. Start a try block. You'll have to indent the following line up to the except block:
    try:
  6. Create a variable for the query and define the where clause:
    qry = '"DOW" = 'Mon''
  7. Create the feature layer:
    flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer")
  8. Execute the Select by Location tool to find all burglaries within the Edgewood School District:
    arcpy.SelectLayerByLocation_management (flayer, "COMPLETELY_WITHIN", "c:/ArcpyBook/Ch7/EdgewoodSD.shp")
  9. Execute the Select Layer by Attributes tool to find all the burglaries that match the query we previously defined in the qry variable. This should be defined as a subset query:
    arcpy.SelectLayerByAttribute_management(flayer, "SUBSET_SELECTION", qry)
  10. Print the number of records that were selected:
    cnt = arcpy.GetCount_management(flayer)
    print("The total number of selected records is: " + str(cnt))
  11. Add the except block:
    except Exception as e:
      print(e.message)
  12. The entire script should appear as follows:
    import arcpy
    arcpy.env.workspace = "c:/ArcpyBook/data/CityOfSanAntonio.gdb"
    try:
      qry = '"DOW" = 'Mon''
      flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer")
      arcpy.SelectLayerByLocation_management (flayer, "COMPLETELY_WITHIN", "c:/ArcpyBook/Ch7/EdgewoodSD.shp")
      arcpy.SelectLayerByAttribute_management(flayer, "SUBSET_SELECTION", qry)
      cnt = arcpy.GetCount_management(flayer)
      print("The total number of selected records is: " + str(cnt))
    except Exception as e:
      print(e.message)
  13. Save and run the script. If everything was done correctly, you should see a message indicating that 197 records have been selected. This will select all the burglaries within the boundaries of the Edgewood School District that occurred on a Monday:
    The total number of selected records is: 197
    
  14. You can check your work by examining the c:ArcpyBookcodeCh7SpatialAttributeQuery.py solution file.

How it works...

A new feature layer is created with the Make Feature Layer tool and assigned to the variable flayer. This temporary layer is then used as an input to the Select by Location tool along with a COMPLETELY_WITHIN spatial operator, to find all the burglaries within the Edgewood School District. This same feature layer, with a selection set already defined, is then used as an input parameter to the Select Layer by Attributes tool. In addition to passing a reference to the feature layer, the Select Layer by Attributes tool is also passed a parameter that defines the selection type and a where clause. The selection type is set to SUBSET_SELECTION. This selection type creates a new selection that is combined with the existing selection. Only the records that are common to both remain selected. The where clause passed in as the third parameter is an attribute query to find all the burglaries that occurred on a Monday. The query uses the DOW field and looks for a value of Mon. Finally, the Get Count tool is used against the flayer variable to get a count of the number of selected records, and this is printed on the screen.

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

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