Calculate distance, speed and elevation gain from the logs of your activity tracker


The previous article described how to import the log files from your activity tracker or app. We also created a heatmap showing the tracks on the map. This article will show you how to calculate some base metrics from the gpx log files like distance travelled, speed and bearing. Some activity loggers store this information in the log file but most don’t.

Calculate distance travelled

It sounds simple to calculate the distance travelled from your log file. The log consists of a series of lon/lat locations, so calculating the distance between all of time and summing these will result in the distance travelled. But calculating the distance between two lon/lat locations is tedious, even for short distances.

We will try several approaches to find the balance between speed and accuracy. As you might have guessed, high accuracy requires a lot more calculations compared to low accuracy. We will use the dataframe from the previous article with all activities and filter one out:

df = df[ == 'XXXXX'].reset_index(drop=True)
df = df[['timestamp', 'lat', 'lon', 'elevation']]

In this case we filter one activity based on its name, but it is also possible to use source file or start date and/or time.

Flat earth

Calculating distances with lon/lat requires some trigonometry. The trigonometry functions take radians as input. The gpx file stores longitude and latitude as degrees so we first add (line 4–5) the latitude and longitude in radians.

For each distance formula a method is created called dist_xxxx and the result of this calculation will be stored in the column named dist_xxxx. Line 14 loops over all implementations (one so far), line 16 creates the column an stores 0 in the first row. Lines 17–19 call the distance function with the current location and the previous location as parameters. The result is stored in the appropriate column. Line 20 prints the total distance travelled according to the distance formula.

This first implementation assumes that the distance between two points is a straight line (Pythagorean theorem). It does not take in account that the longitude lines get closer to each other when you get closer to one of the poles. It assumes these lines are parallel with a distance of 110.25 km for 1 degree longitude. The calculation is simple, but even on a short distance the inaccuracy is relative large. The result is 10,8 km.—-7f60cf5620c9—4