Near (Analysis)
Summary
Determines the distance from each feature in the input features to the nearest feature in the near features, within the search radius.
Learn more about how proximity is calculated in geoprocessing tools
Illustration
Usage
-
The following two fields will be added to the attribute table of the input features. The field values are updated if the fields already exist.
- NEAR_FID—Stores the feature ID of the nearest feature.
- NEAR_DIST—Stores the distance from the input feature to the nearest feature. The value of this field is in the linear unit of the input's coordinate system.
-
The values for NEAR_FID and NEAR_DIST will be -1 if no feature is found within the search radius.
Optionally, NEAR_X, NEAR_Y, NEAR_ANGLE, and NEAR_FC fields can also be added to the attribute table of input features as explained in the near features and optional parameters entries. The value of a field is updated if the fields already exist. If no feature is found within the search radius the values of these fields will be -1 for NEAR_X and NEAR_Y, 0 for NEAR_ANGLE, and null for NEAR_FC.
-
Both input features and near features can be point, multipoint, line, or polygon.
-
The Near Features can include one or more feature classes of different shape types.
-
The same dataset can be used as both Input Features and Near Features. When an input feature's nearest feature is itself (NEAR_DIST is 0), this feature is ignored from the calculation and the next nearest feature is searched.
-
The input features can be a layer on which you have performed a selection. The selected features will be used and updated during the execution of the tool. The remaining features will have the values of the newly created fields (such as NEAR_FID and NEAR_DIST) set to -1.
The distances calculated by this tool are in the unit of the coordinate system of the input features. If your input is in a geographic coordinate system and you want output distances to be measured in a linear unit (as opposed to decimal degrees), you must first project your input to a projected coordinate system using the Project tool. For best results, use an equidistant projection or a projection intended for your study area (UTM, for example).
When more than one near features has the same shortest distance from an input feature, one of them is randomly chosen as the nearest feature.
Syntax
Parameter | Explanation | Data Type |
in_features |
The input features that can be point, polyline, polygon, or multipoint type. | Feature Layer |
near_features [near_features,...] |
Value used to find the nearest features from input features. There can be one or more entries of near features; each entry can be of point, polyline, polygon, or multipoint type. When multiple entries of near features are specified, a new field, NEAR_FC, is added to the input table to store the paths of the source feature class that contains the nearest features. | Feature Layer |
search_radius (Optional) |
Specifies the radius used to search for candidate near features. The near features within this radius are considered for calculating the nearest feature. If no value is specified, that is, the default (empty) radius is used, all near features are considered for calculation. The unit of the search radius defaults to the units of the coordinate system of the input features. The units can be changed to any other unit. However, this has no impact on the units of NEAR_DIST which is based on the units of the coordinate system of the input features. | Linear unit |
location (Optional) |
Specifies whether x- and y-coordinates of the nearest location of the near feature will be written to new fields NEAR_X and NEAR_Y, respectively.
| Boolean |
angle (Optional) |
Specifies whether the near angle values in decimal degrees will be calculated and written to a new field, NEAR_ANGLE. A near angle measures from the x-axis (horizontal axis) to the direction of the line connecting an input feature to its nearest feature at their closest locations, and it is within the range of 0 to 180 or 0 to -180 decimal degrees - 0 to the east, 90 to the north, 180 (-180°) to the west, and -90 to the south.
| Boolean |
Code Sample
The following Python interactive window script demonstrates how to use the Near function in immediate mode.
import arcpy
arcpy.env.workspace = "C:/data/city.gdb"
## find the nearest road from each house
arcpy.Near_analysis('houses', 'roads')
The following Python script demonstrates how to use the Near function in a stand-alone script.
# Name: Near.py
# Description: Finds nearest features from input feature class to near feature class.
import arcpy
from arcpy import env
# Set workspace environment
env.workspace = "C:/data/city.gdb"
try:
# set local variables
in_features = "houses"
near_features = "parks"
# find features only within search radius
search_radius = "5000 Meters"
# find location nearest features
location = "LOCATION"
# avoid getting angle of neares features
angle = "NO_ANGLE"
# execute the function
arcpy.Near_analysis(in_features, near_features, search_radius, location, angle)
# get geoprocessing messages
print arcpy.GetMessages()
except arcpy.ExecuteError:
print arcpy.GetMessages(2)
except Exception as ex:
print ex.args[0]
The following Python script demonstrate how to convert the near angle to azimuth.
# Name: near_angle_to_azimuth.py
import arcpy
# Near tool does not calculate angle in azimuths
# This script, using the output of the tool, converts the angles
# to azimuth angles.
in_table = r"C:/data/city.gdb/near_table"
angles = arcpy.da.SearchCursor(in_table, ["NEAR_ANGLE"])
azimuth_angles = []
with angles as rows:
for row in rows:
angle = row[0]
if angle <= 180 and angle > 90:
azimuth_angles.append(360.0 - (angle - 90))
else:
azimuth_angles.append(abs(angle - 90))
# Use these azimuth angles as necessary.
Demonstrates post-processing with Near tool's derived output. The script finds, for each near feature, which input features it is closest to.
# Name: features_closest_to_input.py
"""
This script finds, for each input feature, a list of near feature it is closest to.
If near features 6, 7, 10 are closest to input feature 3 then the
resulting dictionary will have a list [6, 7, 10] as value for key 3
"""
import os
import arcpy
in_fc = r"C:\data\cities.gdb\cities_many"
# create a dictionary to hold the list of near ids for each input id
nearest_dict = dict()
with arcpy.da.SearchCursor(in_fc, ["OID@", "NEAR_FID"]) as rows:
for row in rows:
nearest_id = row[0] # get OID value for that row
input_id = row[1] # get NEAR_FID value
if input_id in nearest_dict:
# if a dict key already exists, append near id to value list for that key
nearest_dict[input_id].append(nearest_id)
else:
# if the key does not exist then create a new list with near id
# and add it to the dictionary
nearest_dict[input_id] = [nearest_id]
print nearest_dict
# output will look like:
# {1: [13, 28], 2: [2, 9, 14, 20, 22], 3: [11, 12, 24, 25]}