Strava: Calculating the Similarity of Two Runs
Interested in seeing just how similar (or different) two paths/routes are? You might not be Dr. Who after this post, but you'll be one step closer to becoming a Time Lord.
Join the DZone community and get the full member experience.
Join For FreeI go running several times a week and wanted to compare my runs against each other to see how similar they are.
I record my runs with the Strava app and it has an API that returns lat/long coordinates for each run in the Google encoded polyline algorithm format.
We can use the polyline library to decode these values into a list of lat/long tuples. For example:
|
Once we’ve got the route defined as a set of coordinates we need to compare them. My Googling led me to an algorithm called Dynamic Time Warping:
DTW is a method that calculates an optimal match between two given sequences (e.g. time series) with certain restrictions.
The sequences are “warped” non-linearly in the time dimension to determine a measure of their similarity independent of certain non-linear variations in the time dimension.
The fastdtw library implements an approximation of this library and returns a value indicating the distance between sets of points.
We can see how to apply fastdtw and polyline against Strava data in the following example:
|
Now let’s try it out on two runs, 1361109741 and 1346460542:
|
These two runs are both near my house so the value is small. Let’s change the second route to be from my trip to New York:
|
Much bigger!
I’m not really interested in the actual value returned but I am interested in the relative values. I’m building a little application to generate routes that I should run and I want it to come up with a routes that are different to recent ones that I’ve run. This score can now form part of the criteria.
Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments