Select Layer By Location (Data Management)
Summary
Selects features in a layer based on a spatial relationship to features in another layer.
Each feature in the Input Feature Layer is evaluated against the features in the Selecting Features layer or feature class; if the specified Relationship is met, the input feature is selected.
Usage
-
The input must be a feature layer; it cannot be a feature class.
-
Valid inputs for this tool are layers in the ArcMap, ArcGlobe, or ArcScene table of contents, and also on layers created in ArcCatalog or in scripts using the Make Feature Layer tool.
-
The coordinate system in which the spatial relationship is evaluated may affect the result. Features that intersect in one coordinate system may or may not intersect in another.
- This tool evaluates spatial relationship in the coordinate system of the Input Feature Layer's data source (the feature class on disk). Set the output coordinate system environment to evaluate the spatial relationship in a different coordinate system.
- The Select By Location tools available in the Selection menu in ArcMap, ArcScene, and ArcGlobe evaluate spatial relationship in the coordinate system of that application's display (the current data frame). For the geoprocessing tool to match this behavior (and get identical results), set output coordinate system environment to Same as Display.
-
This tool can be used to select features based on their spatial relationships to other features within the same layer. For some examples, see: Select by location within a layer.
-
The Get Count tool can be used to find the number of features selected by the Select Layer By Location tool. This can be useful for determining if any features matched the desired spatial relationship before proceeding to further analysis as part of an automated workflow (that is, script or model).
-
For more information about using the three-dimensional spatial relationships (INTERSECT_3D and WITHIN_A_DISTANCE_3D), see Select by location 3D relationships.
Syntax
Parameter | Explanation | Data Type |
in_layer |
The layer containing the features that will be evaluated against the Selecting Features. The selection will be applied to this layer. The input can be a layer in the ArcMap table of contents, or a layer created in ArcCatalog or in scripts using the Make Feature Layer tool. The input cannot be the path to a feature class on disk. | Feature Layer; Mosaic Layer; Raster Catalog Layer |
overlap_type (Optional) |
The spatial relationship to be evaluated.
| String |
select_features (Optional) |
The features in the Input Feature Layer will be selected based on their relationship to the features from this layer or feature class. | Feature Layer |
search_distance (Optional) |
This parameter is only valid if the Relationship parameter is set to one of the following: WITHIN_A_DISTANCE, WITHIN_A_DISTANCE_3D, INTERSECT, INTERSECT_3D, HAVE_THEIR_CENTER_IN, CONTAINS, or WITHIN. | Linear unit |
selection_type (Optional) |
Determines how the selection will be applied to the input and how to combine with an existing selection. Note that there is no option here to clear an existing selection. To clear a selection, use the CLEAR_SELECTION option on the Select Layer By Attribute tool.
| String |
Code Sample
The following Python window script demonstrates how to use the SelectLayerByLocation function in immediate mode.
import arcpy # First, make a layer from the feature class arcpy.MakeFeatureLayer_management("c:/kamsack.gdb/parcel", "parcel_lyr") # Then add a selection to the layer based on location to features in another feature class arcpy.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.
# Name: ExtactFeaturesByLocationAndAttribute.py # Description: Extract features to a new feature class based on a Location and an attribute query # Import arcpy and set path to data import arcpy arcpy.env.workspace = "c:/data/mexico.gdb" # Make a layer and select cities which overlap the chihuahua polygon arcpy.MakeFeatureLayer_management('cities', 'cities_lyr') arcpy.SelectLayerByLocation_management('cities_lyr', 'intersect', 'chihuahua') # Within the previous selection sub-select cities which have population > 10,000 arcpy.SelectLayerByAttribute_management('cities_lyr', 'SUBSET_SELECTION', '"population" > 10000') # If features matched criteria write them to a new feature class matchcount = int(arcpy.GetCount_management('cities_lyr').getOutput(0)) if matchcount == 0: print('no features matched spatial and attribute criteria') else: arcpy.CopyFeatures_management('cities_lyr', 'chihuahua_10000plus') print('{0} cities that matched criteria written to {0}'.format( matchcount, chihuahua_10000plus))