The Select Layer by Location tool, as shown in the next screenshot, can be used to select features based on some type of spatial relationship. Since it deals with spatial relationships, this tool only applies to feature classes and their corresponding in-memory feature layers.
There are many different types of spatial relationships that you can apply while selecting features using the Select by Location tool, including intersect, contains, within, boundary touches, is identical, and many others. If it's not specified, the default intersect spatial relationship will be applied. The input feature layer is the only required parameter, but there are a number of optional parameters, including the spatial relationship, search distance, a feature layer, or feature class to test against the input layer, and a selection type. In this recipe, you will learn how to use the Select by Location tool in a Python script to select features based on a spatial relationship. You'll use the tool to select burglaries that are within the boundaries of the Edgewood school district.
Follow these steps to learn how to perform a spatial query using the Select by Location tool:
c:ArcpyBookCh7SelectByLocation.py
.arcpy
module:import arcpy
arcpy.env.workspace = "c:/ArcpyBook/data/CityOfSanAntonio.gdb"
try
block:try:
Burglary
feature class:flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer")
COMPLETELY_WITHIN
, meaning that we want to find all burglaries that are completely within the boundaries of the comparison layer. Define EdgewoodSD.shp
as the comparison layer:arcpy.SelectLayerByLocation_management (flayer, "COMPLETELY_WITHIN", "c:/ArcpyBook/Ch7/EdgewoodSD.shp")
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: flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer") arcpy.SelectLayerByLocation_management (flayer, "COMPLETELY_WITHIN", "c:/ArcpyBook/Ch7/EdgewoodSD.shp") cnt = arcpy.GetCount_management(flayer) print("The number of selected records is: " + str(cnt)) except Exception as e: print("An error occurred during selection")
c:ArcpyBookcodeCh7SelectByLocation_Step1.py
solution file.1470
records have been selected:The total number of selected records is: 1470
In this case, we did not define the optional search distance and selection type parameters. By default, a new selection will be applied as the selection type. We didn't apply a distance parameter in this case, but we'll do this now to illustrate how it works.
arcpy.SelectLayerByLocation_management (flayer, "WITHIN_A_DISTANCE", "c:/ArcpyBook/Ch7/EdgewoodSD.shp","1 MILES")
c:ArcpyBookcodeCh7SelectByLocation_Step2.py
solution file.2976
records have been selected. This will select all burglaries within the boundaries of the Edgewood school district along with any burglaries within one mile of the boundary:The total number of selected records is: 2976
The final thing you'll do in this section is use the Copy Features tool to write the temporary layer to a new feature class.
## cnt = arcpy.GetCount_management(flayer) ## print("The number of selected records is: " + str(cnt))
EdgewoodBurglaries.shp
:arcpy.CopyFeatures_management(flayer, 'c:/ArcpyBook/Ch7/EdgewoodBurglaries.shp')
try
and except
blocks:import arcpy arcpy.env.workspace = "c:/ArcpyBook/data/CityOfSanAntonio.gdb" try: flayer = arcpy.MakeFeatureLayer_management("Burglary","Burglary_Layer") arcpy.SelectLayerByLocation_management (flayer, "WITHIN_A_DISTANCE", "c:/ArcpyBook/Ch7/EdgewoodSD.shp","1 MILES") arcpy.CopyFeatures_management(flayer, 'c:/ArcpyBook/Ch7/EdgewoodBurglaries.shp') #cnt = arcpy.GetCount_management(flayer) #print("The total number of selected records is: " + str(cnt)) except Exception as e: print(e.message)
c:ArcpyBookcodeCh7SelectByLocation_Step3.py
solution file.c:ArcpyBookCh7
folder to see the output shapefile.The Select by Location tool requires that a feature layer be passed as the first parameter. In this recipe, we pass 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 Location tool as the first parameter. In this script, we've also passed a parameter that indicates the spatial relationship that we'd like to apply. Finally, we've also defined a source layer to use for the comparison of the spatial relationship. Other optional parameters that can be applied include a search distance and a selection type.