# Obtaining closest distance using ArcGIS Desktop?

I am using ArcGIS Desktop 10.5.

I have a shapefile containing say 10 locations from a country with latitude and longitude.

How can I look at each location (point) and getting the distance from each of the other 9 locations (points) and storing the "closest distance" and the location that appears the closest?

In the end, I would have a table showing the 10 locations along with the closest location name with the distance.

• Welcome to GIS SE! As a new user, please be sure to take the short tour to learn about this site's focused Q&A format. Also please edit your question to indicate whether you have an Advanced license. If you do, the Near tool may do what you need.
– Andy
Feb 7 '19 at 15:56

The Generate Near Table tool can create a table of the closest point(s) to input locations.

The output table includes the FIDs of each input feature and its closest near feature, along with the distance.

From the documentation:

The same feature class or layer may be used as both input and near features. In this situation, the input feature being evaluated is excluded from the near features candidates to avoid all features being closest to themselves.

As noted by Mapperz and Andy, this tool requires an Advanced license for ArcGIS.

– Mapperz
Feb 8 '19 at 2:46

You can use arcpy and the distanceTo method (see PointGeometry):

``````import arcpy
fc = 'tx_riks_sample' #Change
fields = ['TEXT','Closest','Distance','SHAPE@'] #Change all but SHAPE@. The fields need to be added before code is executed. In my example Closest and Distance is being calculated

all_points = [i for i in arcpy.da.SearchCursor(fc,fields)]

with arcpy.da.UpdateCursor(fc,fields) as cursor:
for row in cursor:
for point in all_points:
distances = [row[-1].distanceTo(point[-1]) for point in all_points]
idx = distances.index(sorted(distances)[1]) #The 1 is to get index of second smallest distance so that the closest Point is not the Point itself
row[1], row[2] = all_points[idx][0], distances[idx]
cursor.updateRow(row)
``````