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.
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.
c:ArcpyBookCh7SpatialAttributeQuery.py
.arcpy
module:import arcpy
arcpy.env.workspace = "c:/ArcpyBook/data/CityofSanAntonio.gdb"
try
block. You'll have to indent the following line up to the except
block:try:
where
clause:qry = '"DOW" = 'Mon''
flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer")
arcpy.SelectLayerByLocation_management (flayer, "COMPLETELY_WITHIN", "c:/ArcpyBook/Ch7/EdgewoodSD.shp")
qry
variable. This should be defined as a subset query:arcpy.SelectLayerByAttribute_management(flayer, "SUBSET_SELECTION", qry)
cnt = arcpy.GetCount_management(flayer) print("The total number of selected records is: " + str(cnt))
except
block:except Exception as e: print(e.message)
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)
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
c:ArcpyBookcodeCh7SpatialAttributeQuery.py
solution file.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.