Parameters
Label | Explanation | Data Type |
Input Features | The features that will be evaluated using 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 that will 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 Select Layer By Attribute tool with the Selection Type parameter set to Clear the current selection 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 first input with selection applied. | Feature Layer; Mosaic Layer |
Output Layer Names | A multivalue parameter including all inputs with selections applied. Use this output parameter in a model to connect to a tool with a multivalue input such as the Merge tool. | Feature Layer; Mosaic Layer |
Count | The number of selected records. If there are multiple Input Features parameter values, the counts will be in the same order as the inputs. | 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 using 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 that will 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 Select Layer By Attribute tool with the selection_type parameter set to CLEAR_SELECTION 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 first input with selection applied. | Feature Layer; Mosaic Layer |
out_layers_or_views | A multivalue parameter including all inputs with selections applied. Use this output parameter in a model to connect to a tool with a multivalue input such as the Merge tool. | Feature Layer; Mosaic Layer |
count | The number of selected records. If there are multiple in_features parameter values, the counts will be in the same order as the inputs. | Long |
Code sample
The following Python window script demonstrates how to use the SelectLayerByLocation function in immediate mode.
import arcpyarcpy.management.SelectLayerByLocation("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.management.SelectLayerByLocation('cities', 'INTERSECT', 'chihuahua')# From the previous selection, select a subset of cities that have # population > 10,000arcpy.management.SelectLayerByAttribute(chihuahua_cities, 'SUBSET_SELECTION', '"population" > 10000')# If features matched criteria, write them to a new feature classmatchcount = int(arcpy.management.GetCount(chihuahua_cities)[0]) if matchcount == 0: print('no features matched spatial and attribute criteria')else: arcpy.management.CopyFeatures(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.management.SelectLayerByLocation('cities', 'WITHIN_A_DISTANCE', 'chihuahua', '1.5 Miles')arcpy.management.SelectLayerByLocation('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.management.SelectLayerByLocation('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.management.SelectLayerByLocation('cities', 'WITHIN_A_DISTANCE_GEODESIC', 'chihuahua', '200')