

The interpolated gradient will be -2,95, as (0 + (-5.9)) / 2 = -2.95: route_df.interpolate() Image 8 - Interpolated gradient for a single data point (image by author) For example, the missing gradient before the missing value was 0, and after it was -5.9. It will replace the missing value with the average of the data point before and after it. We'll impute the missing values with interpolation. To get the idea of the approach, let's isolate the second one with the surrounding couple of rows: route_df Image 7 - Data points surrounding the missing gradient value (image by author) We can ignore the first one, as it's missing for a whole different reason. Let's see where the missing values are: route_df.isna()] Image 6 - All missing gradient values in the dataset (image by author) Route_df.head() Image 5 - Route dataset with gradient info (image by author)
Download gpx file from strava how to#
How to Interpolate Incorrect Gradients From a Strava Routeįirst things first, we'll assign the calculated gradients to a new dataset column: route_df = gradients The thing is - there's a stupidly simple fix. It's a step in the right direction, but we now have a couple of missing data points. Plt.plot(np.arange(len(gradients)), gradients, lw=2, color='#101010') Image 4 - Estimated average route gradient v2 (image by author) Let's see how it looks like: plt.title('Terrain gradient on the route', size=20) There are no gradients above 30% on this route, and such gradients are extremely rare overall. We'll mitigate this issue by adding a condition - if the estimated average gradient is greater than 30%, we'll append NaN to the list. In theory, that would mean you gain 1200 meters of elevation after 100 meters of distance. A single data point has over 1200% gradient, which is impossible. There appears to be something wrong with the route file. Plt.plot(np.arange(len(gradients)), gradients, lw=2, color='#101010') Image 3 - Estimated average route gradient v1 (image by author) Gradients Image 2 - First ten estimated gradients (image by author)Ĭalculations done - let's now visualize the average gradient for each of the 835 data points: plt.title('Terrain gradient on the route', size=20) Gradients are usually rounded up to single decimal points, but that's a convention, not a requirement. We'll skip the first row, as there's nothing to compare it with. The point is - we can't know for sure, and the above logic is the best we can do. On the other hand, the entire 87-meter segment could have a perfectly distributed 2.1% grade. The first 15 meters of the segment could have a 10% gradient, and the remaining 72 meters could be completely flat. It's only an average, so keep that in mind. This means that, provided the same gradient continues, you would gain 2.1 meters of elevation after 100 meters of distance. The average grade from the route start to the second data point was 2.1%. Let's test the logic with hardcoded values from the second data point (1.86 meters of elevation gained over 87.59 meters): (1.86 / 87.59) * 100 We can estimate the average gradient between two data points by dividing the elevation difference between them with the distance covered and multiplying the results by 100. We'll do our best, but everything you'll see from this point is just an estimation. Our data is quite limited, as we only have 835 data points spread over 36 kilometers. How to Calculate Gradient from a Strava RouteĪ gradient is nothing but a slope of the surface you're riding on. We can use the elevation difference and distance data to estimate average gradients on each of 835 individual segments. To recap, there are 835 data points in total on this 36,4-kilometer route, so there are on average 43,6 meters between data points. Read the last week's article if you don't have the route dataset in this format.

Route_df.head() Image 1 - Strava route dataset with distance and elevation data (image by author) Plt.rcParams = Falseįrom here, load the route dataset: route_df = pd.read_csv('./data/route_df_elevation_distance.csv')

To start, import the usual suspects and tweak Matplotlib's default styling: import numpy as np We won’t bother with GPX files today, as we already have route data points, elevation, and distance extracted to a CSV file.
Download gpx file from strava code#
You can download the source code on GitHub. We have a lot to cover today, so let's dive straight in.ĭon't feel like reading? Watch my video instead: These represent the slope of the surface you're riding on.Īs it turns out, you can estimate them effortlessly with basic Python and math skills. One thing cyclists love talking about is gradients. It was a step in the right direction, as you'll need both elevation and distance data today. Last week you've seen how easy it is to calculate the elevation difference and distance of a Strava route. Part 4/6 - Calculate and visualize gradients of a Strava route GPX files with Python
