Calculating straight line distance between consecutive locations

4303
2
08-30-2013 11:51 PM
XavierGlaudas
New Contributor
Hello,

Well I am back to dealing with the un-user friendly ArcGIS, and I have a basic question. I import a bunch of GPS coordinates in Arcscene, and I would like to add a field in my attribute table. I would like to calculate the distance between consecutive points (all data is arranged in chronological order). What is the easiest way to do it?

Thanks for your help!
0 Kudos
2 Replies
ElisabethCondon
New Contributor
I don't have an answer for you, but I have the same question! With the additional detail of having my points in date order, but also having different categories of points for which I would like to calculate distance moved in date order. Feedback on this would be greatly appreciated.
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Gludo and Elisabeth,

I think you can use some Python to achieve this. See example below:

def main():

    import arcpy

    # assume data is available as point featureclass
    # and fc has projected coordinate system
    fc = "c:/folder/yourGeodatabase.gdb/yourFCname" # change this (path to featureclass GPS points)
    fldDistance = "DistGPSpoints"                   # change this (output name distance field)


    # add distance field if it doesn't exist
    if FieldExist(fc,fldDistance) == False:
        arcpy.AddField_management(fc, fldDistance, "DOUBLE")

    # Create update cursor for feature class
    fields = ['SHAPE@XY', fldDistance]
    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        X = 0
        Y = 0
        cnt = 0
        for row in cursor:
            cnt+=1
            prevX = X
            prevY = Y
            X = row[0][0]
            Y = row[0][1]
            if cnt == 1:
                # for first point distance is 0
                distance = 0
            else:
                distance = getDist(X,Y,prevX,prevY)

            # Update row
            row[1] = distance
            cursor.updateRow(row)

    del row


def getDist(X1,Y1,X2,Y2):
    import math
    return math.hypot(X2 - X1, Y2 - Y1)


def FieldExist(featureclass, fieldname):
    import arcpy

    fieldList = arcpy.ListFields(featureclass, fieldname)
    fieldCount = len(fieldList)

    if (fieldCount == 1):
        return True
    else:
        return False



if __name__ == '__main__':
    main()


You will need to change the path to the featureclass and the name of the output field. It assumes the data is available as point featureclass and the point should be projected (not in a geographic coordinate system).

The script will:

  • add the output distance field (now named "DistGPSpoints") if it doesn't exist yet

  • loop through the points and calculate the distance to the previous point


Kind regards,

Xander
0 Kudos