Near (Analysis)

License Level:BasicStandardAdvanced

Summary

Calculates distance and additional proximity information between the input features and the closest feature in another layer or feature class.

Learn more about how proximity is calculated in geoprocessing tools

Illustration

Finding near feature by geometry type

Usage

Syntax

Near_analysis (in_features, near_features, {search_radius}, {location}, {angle}, {method})
ParameterExplanationData Type
in_features

The input features that can be point, polyline, polygon, or multipoint type.

Feature Layer
near_features
[near_features,...]

One or more feature layers or feature classes containing near feature candidates. The near features can be of point, polyline, polygon, or multipoint. If multiple layers or feature classes are specified, a field named NEAR_FC is added to the input table and will store the paths of the source feature class containing the nearest feature found. The same feature class or layer may be used as both input and near features.

Feature Layer
search_radius
(Optional)

The radius used to search for near features. If no value is specified, all near features are considered. If a distance but no unit or Unknown is specified, the units of the coordinate system of the input features are used. If the GEODESIC option is used, a linear unit such as Kilometers or Miles should be used.

Linear unit
location
(Optional)

Specifies whether x- and y-coordinates of the closest location of the near feature will be written to the NEAR_X and NEAR_Y fields.

  • NO_LOCATION Location information will not be written to the output table. This is the default.
  • LOCATION Location information will be written to the output table.
Boolean
angle
(Optional)

Specifies whether the near angle will be calculated and written to a NEAR_ANGLE field in the output table. A near angle measures direction of the line connecting an input feature to its nearest feature at their closest locations. When the PLANAR method is used in the method parameter, the angle is within the range of -180° to 180°, with 0° to the east, 90° to the north, 180° (or -180°) to the west, and -90° to the south. When the GEODESIC method is used, the angle is within the range of -180° to 180°, with 0° to the north, 90° to the east, 180° (or -180°) to the south, and -90° to the west.

  • NO_ANGLEThe near angle values will not be written. This is the default.
  • ANGLEThe near angle values will be written to the NEAR_ANGLE field.
Boolean
method
(Optional)

Determines whether to use a shortest path on a spheroid (geodesic) or a flat earth (planar) method. It is strongly suggested to use the GEODESIC method with data stored in a coordinate system that is not appropriate for distance measurements (for example, Web Mercator or any geographic coordinate system) and any analysis that spans a large geographic area.

  • PLANARUses planar distances between the features. This is the default.
  • GEODESICUses geodesic distances between features. This method takes into account the curvature of the spheroid and correctly deals with data near the dateline and poles.
String

Code Sample

Near example 1 (Python window)

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')
Near example 2 (stand-alone Python script)

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]
Near example 3 (stand-alone Python script)

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.
Near example 4 (stand-alone Python script)

Demonstrates postprocessing with the 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]}

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: No
ArcGIS for Desktop Standard: No
ArcGIS for Desktop Advanced: Yes
3/3/2014