Parameters
Label | Explanation | Data Type |
Input Features | The features that will be evaluated against the Selecting Features parameter values. The selection will be applied to these features. | Feature Layer; Raster Layer; Mosaic Layer |
Relationship (Optional) | Specifies the spatial relationship to be evaluated.
| String |
Selecting Features (Optional) | The features in the Input Features parameter will be selected based on their relationship to the features from this layer or feature class. | Feature Layer |
Search Distance (Optional) | The distance that will be searched. This parameter is only valid if the Relationship parameter is set to Within a distance, Within a distance geodesic, Within a distance 3D, Intersect, Intersect 3D, Have their center in, or Contains. If the Within a distance geodesic option is selected, use a linear unit such as kilometers or miles. | Linear Unit |
Selection Type (Optional) | Specifies how the selection will be applied to the input and how it will be combined with an existing selection. This tool does not include an option to clear an existing selection; use the Clear the current selection option on the Select Layer By Attribute tool to do that.
| String |
Invert Spatial Relationship (Optional) | Specifies whether the spatial relationship evaluation result or the opposite result will be used. For example, this parameter can be used to get a list of features that do not intersect or are not within a given distance of features in another dataset.
| Boolean |
Derived Output
Label | Explanation | Data Type |
Layer With Selection | The updated inputs with selections applied. | Feature Layer; Mosaic Layer |
Output Layer Names | The names of the updated inputs. | Feature Layer; Mosaic Layer |
Count | The number of selected records. | Long |
arcpy.management.SelectLayerByLocation(in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})
Name | Explanation | Data Type |
in_layer [in_layer,...] | The features that will be evaluated against the select_features parameter values. The selection will be applied to these features. | Feature Layer; Raster Layer; Mosaic Layer |
overlap_type (Optional) | Specifies the spatial relationship to be evaluated.
| String |
select_features (Optional) | The features in the Input Features parameter will be selected based on their relationship to the features from this layer or feature class. | Feature Layer |
search_distance (Optional) | The distance that will be searched. This parameter is only valid if the overlap_type parameter is set to WITHIN_A_DISTANCE, WITHIN_A_DISTANCE_GEODESIC, WITHIN_A_DISTANCE_3D, INTERSECT, INTERSECT_3D, HAVE_THEIR_CENTER_IN, or CONTAINS. If the WITHIN_A_DISTANCE_GEODESIC option is selected, use a linear unit such as kilometers or miles. | Linear Unit |
selection_type (Optional) | Specifies how the selection will be applied to the input and how it will be combined with an existing selection. This tool does not include an option to clear an existing selection; use the CLEAR_SELECTION option on the Select Layer By Attribute tool to do that.
| String |
invert_spatial_relationship (Optional) | Specifies whether the spatial relationship evaluation result or the opposite result will be used. For example, this parameter can be used to get a list of features that do not intersect or are not within a given distance of features in another dataset.
| Boolean |
Derived Output
Name | Explanation | Data Type |
out_layer_or_view | The updated inputs with selections applied. | Feature Layer; Mosaic Layer |
out_layers_or_views | The names of the updated inputs. | Feature Layer; Mosaic Layer |
count | The number of selected records. | Long |
Code sample
The following Python window script demonstrates how to use the SelectLayerByLocation function in immediate mode.
import arcpyarcpy.SelectLayerByLocation_management("parcel_lyr", "have_their_center_in", "c:/kamsack.gdb/city_limits")
The following stand-alone script shows how to use the SelectLayerByLocation function in a workflow to extract features to a new feature class based on location and an attribute query.
# Description: Extract features to a new feature class based on a # location and an attribute query# Import arcpy and set path to dataimport arcpyarcpy.env.workspace = "c:/data/mexico.gdb"# Make a layer and select cities that overlap the chihuahua polygonchihuahua_cities = arcpy.SelectLayerByLocation_management('cities', 'INTERSECT', 'chihuahua')# From the previous selection, select a subset of cities that have # population > 10,000arcpy.SelectLayerByAttribute_management(chihuahua_cities, 'SUBSET_SELECTION', '"population" > 10000')# If features matched criteria, write them to a new feature classmatchcount = int(arcpy.GetCount_management(chihuahua_cities)[0]) if matchcount == 0: print('no features matched spatial and attribute criteria')else: arcpy.CopyFeatures_management(chihuahua_cities, 'chihuahua_10000plus') print('{0} cities that matched criteria written to {0}'.format( matchcount, chihuahua_10000plus))
The following stand-alone script shows a variety of uses of the overlap_type parameter's WITHIN_A_DISTANCE and WITHIN_A_DISTANCE_GEODESIC options with the search_distance parameter.
# Description: Select features within a distance# Import arcpy and set path to dataimport arcpyarcpy.env.workspace = r"c:\data\mexico.gdb"arcpy.SelectLayerByLocation_management('cities', 'WITHIN_A_DISTANCE', 'chihuahua', '1.5 Miles')arcpy.SelectLayerByLocation_management('cities', 'WITHIN_A_DISTANCE_GEODESIC', 'chihuahua', '200 Kilometers')# When using WITHIN_A_DISTANCE, if distance units are not specified, the # distance value is assumed to be in the units of the input dataset's coordinate # systemarcpy.SelectLayerByLocation_management('cities', 'WITHIN_A_DISTANCE', 'chihuahua', '200')# When using WITHIN_A_DISTANCE_GEODESIC, if distance units are not specified, # the distance value is assumed to be in metersarcpy.SelectLayerByLocation_management('cities', 'WITHIN_A_DISTANCE_GEODESIC', 'chihuahua', '200')