Select Layer By Location (Data Management)

License Level:BasicStandardAdvanced

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.

Graphic examples of relationships

Usage

Syntax

SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type})
ParameterExplanationData 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.

  • INTERSECTThe features in the input layer will be selected if they intersect a selecting feature. This is the default.
  • INTERSECT_3DThe features in the input layer will be selected if they intersect a selecting feature in three-dimensional space (x, y, and z).
  • WITHIN_A_DISTANCEThe features in the input layer will be selected if they are within a specified distance of a selecting feature. Specify a distance in the Search Distance parameter.
  • WITHIN_A_DISTANCE_3DThe features in the input layer will be selected if they are within a specified distance of a selecting feature in three-dimensional space. Specify a distance in the Search Distance parameter.
  • CONTAINSThe features in the input layer will be selected if they contain a selecting feature. The input features must be polygons.
  • COMPLETELY_CONTAINSThe features in the input layer will be selected if they completely contain a selecting feature. The input features must be polygons.
  • CONTAINS_CLEMENTINIThis spatial relationship yields the same results as COMPLETELY_CONTAINS with one exception. If the selecting feature is entirely on the boundary of the input feature (no part is properly inside or outside), the feature will not be selected. CLEMENTINI defines the boundary polygon as the line separating inside and outside, the boundary of a line is defined as its end points, and the boundary of a point is always empty.
  • WITHINThe features in the input layer will be selected if they are within a selecting feature. The selecting features must be polygons.
  • COMPLETELY_WITHINThe features in the input layer will be selected if they are completely within or contained by a selecting feature. The selecting features must be polygons.
  • WITHIN_CLEMENTINIThe result will be identical to WITHIN except if the entirety of the feature in the input layer is on the boundary of the feature in the selecting layer, the feature will not be selected. CLEMENTINI defines the boundary polygon as the line separating inside and outside, the boundary of a line is defined as its end points, and the boundary of a point is always empty.
  • ARE_IDENTICAL_TOThe features in the input layer will be selected if they are identical (in geometry) to a selecting feature.
  • BOUNDARY_TOUCHESThe features in the input layer will be selected if they have a boundary that touches a selecting feature. The input and selecting features must be lines or polygons. Additionally, the feature in the input layer must be either completely inside or outside the polygon from the selecting layer.
  • SHARE_A_LINE_SEGMENT_WITHThe features in the input layer will be selected if they share a line segment with a selecting feature. The input and selecting features must be lines or polygons.
  • CROSSED_BY_THE_OUTLINE_OFThe features in the input layer will be selected if they are crossed by the outline of a selecting feature. The input and selecting features must be lines or polygons. If polygons are used for the input or selecting layer, the polygon's boundary (line) will be used. Lines that cross at a point will be selected, not lines that share a line segment.
  • HAVE_THEIR_CENTER_INThe features in the input layer will be selected if their center falls within a selecting feature. The center of the feature is calculated as follows: for polygon and multipoint, the geometry's centroid is used, and for line input, the geometry's midpoint is used.
  • CONTAINED_BYThis returns the same result as WITHIN. CONTAINED_BY is maintained to support backward compatibility with models and scripts built into releases prior to ArcGIS 9.3.
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.

  • NEW_SELECTIONThe resulting selection replaces any existing selection. This is the default.
  • ADD_TO_SELECTIONThe resulting selection is added to an existing selection, if one exists. If no selection exists, this is the same as the NEW_SELECTION option.
  • REMOVE_FROM_SELECTIONThe resulting selection is removed from an existing selection. If no selection exists, the operation will have no effect.
  • SUBSET_SELECTIONThe resulting selection is combined with the existing selection. Only records that are common to both remain selected.
  • SWITCH_SELECTIONSwitches the selection. All records that were selected are removed from the selection, and all records that were not selected are added to the selection. The Selecting Features and Relationship parameters are ignored when this option is selected.
String

Code Sample

SelectLayerByLocation example 1 (Python window)

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")
SelectLayerByLocation example 2 (stand-alone script)

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))

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Yes
ArcGIS for Desktop Standard: Yes
ArcGIS for Desktop Advanced: Yes
11/18/2013