## Calculating distance from Lat & Lon coordinates using Python & Pandas

Given a GPS log file structured as follows (column separators omitted for clarity):

```                     Speed      Latitud   Longitud
Time
2019-01-07 06:15:27          0  59.649582  17.721365
2019-01-07 06:16:28          0  59.649583  17.721372
2019-01-07 06:17:28          0  59.649583  17.721370
2019-01-07 06:18:28          0  59.649583  17.721372
2019-01-07 06:19:29          0  59.649600  17.721372
2019-01-07 06:20:06          6  59.649605  17.721278
2019-01-07 06:20:07          7  59.649625  17.721258
2019-01-07 06:20:08          6  59.649642  17.721247
2019-01-07 06:20:10          7  59.649670  17.721228
2019-01-07 06:20:14          7  59.649760  17.721238
2019-01-07 06:20:25         13  59.650020  17.721323
2019-01-07 06:20:28         13  59.650122  17.721323
2019-01-07 06:20:30         13  59.650187  17.721292

```

And you’d want to calculate distance (Rhumb Line) between each gps fix, and the cumulative distance: the Python code below does the trick.

```import numpy as np
import math
import pandas as pd

index_col=0,usecols=[0,1,2,3])

R = 6378.1e3

def pos2d(lat1,lon1,lat2,lon2):

a = math.sin(dla/2) * math.sin(dla/2) + \
math.cos(la1) * math.cos(la2) * math.sin(dlo/2) * math.sin(dlo/2)

c = 2 * math.atan2(math.sqrt(a),math.sqrt(1-a))

return R * c / 1000

df_shifted = df.shift()

df['Dist'] = np.vectorize(pos2d)(
df['Latitud'],df['Longitud'],df_shifted['Latitud'],df_shifted['Longitud'])

df['Dist'] = df['Dist'].fillna(0)
df['TotDist'] = df['Dist'].cumsum()

print (df.to_string())

```

Output:

```2019-01-07 06:15:27          0  59.649582  17.721365  0.000000    0.000000
2019-01-07 06:16:28          0  59.649583  17.721372  0.000409    0.000409
2019-01-07 06:17:28          0  59.649583  17.721370  0.000112    0.000522
2019-01-07 06:18:28          0  59.649583  17.721372  0.000112    0.000634
2019-01-07 06:19:29          0  59.649600  17.721372  0.001892    0.002527
2019-01-07 06:20:06          6  59.649605  17.721278  0.005317    0.007843
2019-01-07 06:20:07          7  59.649625  17.721258  0.002494    0.010338
2019-01-07 06:20:08          6  59.649642  17.721247  0.001991    0.012329
2019-01-07 06:20:10          7  59.649670  17.721228  0.003295    0.015624
2019-01-07 06:20:14          7  59.649760  17.721238  0.010034    0.025658
2019-01-07 06:20:25         13  59.650020  17.721323  0.029335    0.054993
2019-01-07 06:20:28         13  59.650122  17.721323  0.011355    0.066348
2019-01-07 06:20:30         13  59.650187  17.721292  0.007443    0.073791
2019-01-07 06:20:44         15  59.650737  17.720778  0.067708    0.141499
2019-01-07 06:20:45         15  59.650773  17.720725  0.004995    0.146493
2019-01-07 06:20:46         16  59.650790  17.720668  0.003723    0.150216
2019-01-07 06:20:49         15  59.650817  17.720532  0.008219    0.158435
2019-01-07 06:20:51         19  59.650817  17.720373  0.008943    0.167378
2019-01-07 06:20:58         17  59.650777  17.719705  0.037835    0.205213
2019-01-07 06:21:01         16  59.650817  17.719472  0.013841    0.219054
2019-01-07 06:21:05         19  59.650855  17.719117  0.020410    0.239465
2019-01-07 06:21:08         19  59.650858  17.718827  0.016315    0.255779
2019-01-07 06:21:13          9  59.650822  17.718467  0.020641    0.276421
2019-01-07 06:22:13         51  59.648910  17.707070  0.675463    0.951884
2019-01-07 06:22:16         48  59.648868  17.706333  0.041718    0.993602
2019-01-07 06:22:58          7  59.648642  17.702463  0.219134    1.212736
2019-01-07 06:23:00         13  59.648592  17.702390  0.006917    1.219653
2019-01-07 06:23:01         14  59.648550  17.702375  0.004751    1.224404
2019-01-07 06:23:04         30  59.648340  17.702412  0.023469    1.247873
2019-01-07 06:24:04         57  59.640857  17.704502  0.841256    2.089129
2019-01-07 06:24:26         56  59.637767  17.706400  0.360171    2.449300
2019-01-07 06:24:29         59  59.637297  17.707105  0.065658    2.514959

```