5

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

3 Answers 3

5

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

It's easy to do in Excel: use either the Haversine formula or else adjust the difference in longitudes for the latitude and use the Pythagorean formula.

The approximate formula is for short distances only (up to a few tens or hundreds of kilometers, depending on accuracy needs) and should not be used next to either pole. However, it's fast, simple, and easily remembered: all you have to do is shrink the difference of longitudes by the cosine of the latitude and use the usual Euclidean distance formula.

Both formulas are approximate in that they use a spherical model of the Earth. They could be adjusted for an ellipsoidal model, but the 0.3% improvement (max) in absolute accuracy is rarely worthwhile. Both have an advantage over doing the calculation in ArcGIS: no projection is needed.


Because all approaches depend ultimately on the cosine of the latitude and Excel expects the arguments of trigonometric functions to be in radians, begin by creating two new columns with the (lat, lon) coordinates in radians (use Radians). Let's suppose these are in columns D and E, respectively. Here are examples of the Haversine and approximate formulas, respectively:

=2 * 6378137 * ASIN(SQRT((1 - COS(D3-D2))/2 + COS(D2)*COS(D3) * (1 - COS(E3-E2))/2))
=SQRT((COS((D2+D3)/2) * (C3-C2))^2 + (B3-B2)^2) * 111320

(The square root of the sum of squares is apparent in the second (approximate) formula, as well as the use of the cosine of an average latitude to adjust the "leg" given by the longitude.)

The spreadsheet might look like this (distances are in meters):

Spreadsheet

The plot in this spreadsheet compares the two formulas: they agree very closely--within a centimeter for these kilometer-scale distances.

Here are the relevant formulas. They were typed into the first row and just copied down to the other rows:

Formulas

1
  • 1
    thanks a lot. i really love that solution to my problem. actually my advisor was looking that kind of solution. and also i'm thankful for your amazing answer, i really owe you a big timie.
    – ionden
    Commented Nov 2, 2013 at 23:19
3

As you have got ArcInfo and you only have 226 points I would suggest you run your data through the Generate Near Table tool that is in the Analysis toolbox. This will give you all combinations but you could easy delete the unwanted combinations leaving you with your 1> 2, 2>3, 3>4..etc.

2
  • 2
    Since it looks like OP's shapefile is in a GCS, he'll need to project it to a PCS if he wants linear unit distances as output.
    – Paul
    Commented Nov 2, 2013 at 20:50
  • thank you, it let me find what i search for. i thought, i ll be needed to use a code which consists a for definition, but this table generation is absolutely useful.
    – ionden
    Commented Nov 2, 2013 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.