[GIS] How to calculate the distances between the points in sequence automatically

arcgis-desktopdistance

I am trying to measure distances between points and writing the calculated measure between these points in the attribute table.

The points represents a vehicle's location based on GPS data according to existence location in time aspect. I could add the longitude and latitude data from Excel to a shape layer.

table photo

how the points seen in the map

After creating shapefile, I could be able to measure the euclidean distances between the nodes, but if I will do the calculations manually, I might need to have immortal life.

How can I calculate automatically the distances between the nodes and write the calculations in my table or Excel or CSV data.

the distance will calculate according to the id's.

I mean the first distance will be for FID 1 and 2; the second will be for 2-3, third 3-4 and etc.

Please help me 🙂

Best Answer

An advanced method that will work on all levels of ArcMap (must be 10.1 or above though) would be to read the geometry of each point and project it on the fly to compute the distance between the features. This is a good read about how the Near tool works.

The following code does this (all you'll need to do is find a good PCS for your dataset):

import arcpy
from math import hypot    

shp = "path to shapefile"
#Create a spatial reference object for the PCS...change as needed
spatref = arcpy.SpatialReference(26915) #UTM 15N

#Get coordinates of each feature (in PCS) and store it as a list of tuples.
coord = [x[0] for x in arcpy.da.SearchCursor(shp, "SHAPE@XY",
                                             spatial_reference=spatref)]

#Compute distances between features.
distances = [hypot(coord[i+1][0]-coord[i][0],
                   coord[i+1][1]-coord[i][1]) for i,_ in enumerate(coord[:-1])]

#Insert a 0 in the first position since it has nothing to measure against
distances.insert(0, 0)

#Add field to store distances
arcpy.AddField_management(shp, "DIST", "DOUBLE")

#Write distances back to shapefile.
with arcpy.da.UpdateCursor(shp, "DIST") as rows:
    for j,row in enumerate(rows):
        row[0] = distances[j]
        rows.updateRow(row)