How to Calculate Elevation Gain from GPX (and Why Tools Disagree)
To calculate elevation gain from a GPX file, read the <ele> values in order, keep a reference height, and count a change only after it reaches a threshold such as 5 m. The threshold decides the answer. On our 4,531-point
To calculate elevation gain from a GPX file, read the <ele> values in order, keep a reference height, and count a change only after it reaches a threshold such as 5 m. The threshold decides the answer. On our 4,531-point sample ride, adding every positive step gives 1,818 m. The 5 m rule gives 1,518 m. gpx.studio showed 1,519 m for the same track. Below are the Python code, our measurements, and three smaller effects that also move the total: the comparison operator, track splitting, and FIT altitude precision.
All our test files are synthetic samples from a test data generator. There is no surveyed "true" climb for them, so these numbers compare methods, not accuracy.
How do you calculate elevation gain from a GPX file?
Adding every positive difference between neighbouring points treats each 0.1 m wobble as climbing. A threshold with a moving reference point (hysteresis) ignores wobbles until they add up to a real change. This is a Python version of the TypeScript function our site uses, reduced to a single unbroken line:
import sys
import xml.etree.ElementTree as ET
def read_ele(path):
ns = {"g": "http://www.topografix.com/GPX/1/1"}
return [float(e.text) for e in ET.parse(path).findall(".//g:trkpt/g:ele", ns)]
def climb(ele, threshold=5.0):
gain = loss = 0.0
ref = ele[0]
for e in ele[1:]:
if e - ref >= threshold and e > ref:
gain, ref = gain + (e - ref), e
elif ref - e >= threshold and e < ref:
loss, ref = loss + (ref - e), e
return gain, loss
print(climb(read_ele(sys.argv[1])))
The reference moves only when a change is counted. A slow climb of 0.2 m per point is still collected, because the distance from the reference keeps growing until it crosses 5 m. The script reads GPX 1.1 track points only and expects at least one height. It does not read routes, and it does not skip jumps between separate pieces of a track.
How much does the threshold change elevation gain?
We ran the function on two samples: a 36.00 km ride (4,531 points) and a 4.69 km hike (944 points).
| Threshold | Ride gain | Ride loss | Hike gain | Hike loss |
|---|---|---|---|---|
| 0 m (every step) | 1,818.4 m | 1,428.9 m | 98.6 m | 98.5 m |
| 2 m | 1,667.1 m | 1,277.0 m | 80.5 m | 80.7 m |
| 3 m | 1,596.3 m | 1,206.5 m | 72.5 m | 74.7 m |
| 5 m | 1,517.9 m | 1,125.1 m | 71.3 m | 71.5 m |
| 10 m | 1,372.7 m | 981.7 m | 50.7 m | 60.5 m |
Between 0 m and 10 m, the ride loses 445.7 m of climb without a single coordinate changing.
We also uploaded both tracks to gpx.studio on 29 November 2026. It showed 1,519 m of gain and 1,129 m of loss for the ride, close to our 5 m result. For the hike it showed 82 m, against our 71 m. One close match does not tell you that two tools share a method, and we do not know the details of theirs.
Does >= versus > change the result?
Yes, and by more than we expected. Our sample heights sit on a 0.1 m grid, so a difference of exactly the threshold does happen. Whether it counts moves the reference point, and every later comparison starts from a different place. Here is the 5 m rule with only that operator changed:
| Sample file | >= 5 |
> 5 |
|---|---|---|
| climbing-ride | 1,517.9 m | 1,508.2 m |
| hike-loop | 71.3 m | 72.5 m |
| road-ride | 707.7 m | 740.5 m |
| ride-gap-a | 193.6 m | 217.0 m |
| ride-day-1 | 84.7 m | 106.1 m |
Neither operator is always higher. On ride-day-1 the two answers are 21.4 m apart. The last two files are pieces cut from road-ride, so they are not independent recordings.
Floating point adds a smaller effect. We repeated the >= run with heights converted to integer decimetres: road-ride moved from 707.7 m to 707.5 m, and ride-day-1 from 84.7 m to 84.5 m. If you need two languages to agree, convert to integers first and write the operator into your spec.
Is moving-average smoothing a substitute for a threshold?
It is a different filter with its own knob. We applied a centred moving average, then summed every positive step:
| Window (points) | Ride gain | Hike gain |
|---|---|---|
| 5 | 1,747.8 m | 96.6 m |
| 15 | 1,509.3 m | 90.3 m |
| 31 | 1,312.9 m | 80.2 m |
A 15-point window lands near the 5 m result on the ride (1,509.3 m against 1,517.9 m) but not on the hike (90.3 m against 71.3 m). A window counted in points covers a different time span when the recording interval changes, so tuning on one file may not carry over to another.
Do track segments and split files change the climb?
They can, if your counter restarts. We cut each sample into fixed-size chunks and compared a counter that resets per chunk with one that carries its reference across:
| Chunk size | Ride, reset | Ride, carried | Hike, reset | Hike, carried |
|---|---|---|---|---|
| 1,000 points | 1,508.6 m | 1,517.9 m | 71.3 m | 71.3 m |
| 100 points | 1,459.9 m | 1,517.9 m | 66.5 m | 71.3 m |
| 10 points | 974.6 m | 1,517.9 m | 10.1 m | 71.3 m |
In this table, each reset loses the climb collected below the threshold, plus the height step at the joint.
Splitting a recording into two files also restarts the counter. We tried all 4,530 cut points on the ride, counted each half separately and added them. Compared with the unsplit file, the sum ranged from 22.5 m lower to 11.8 m higher. On the hike (943 cut points) the range was 9.9 m lower to 5.8 m higher. A restart can raise a total as well as lower it, because the new reference point changes later comparisons. So a merged file need not equal the sum of its parts.
Our rule carries the state across segments. It leaves out the joint step only at a real break: ends more than 200 m apart, or a height jump of at least 5 m that is steeper than 40%.
Does converting GPX to FIT change elevation gain?
The files our converter writes store altitude in 0.2 m steps. We wrote each GPX sample to a FIT course, read it back and ran the same 5 m function. We also simulated cutting each height down to the step below instead of rounding to the nearest one. The ride-day files are two more pieces of road-ride.
| Sample file | GPX | FIT read back (nearest step) | Simulated cut-down |
|---|---|---|---|
| climbing-ride | 1,517.9 m | 1,509.8 m (-0.53%) | 1,485.8 m |
| hike-loop | 71.3 m | 70.8 m (-0.70%) | 70.8 m |
| ride-day-2 | 229.3 m | 224.0 m (-2.31%) | 226.2 m |
| ride-day-3 | 398.7 m | 386.6 m (-3.03%) | 383.6 m |
Rounding to the nearest step helped a lot on climbing-ride, yet cut-down came out closer on ride-day-2. A height change of a fraction of a metre can push a point across the threshold. The FIT lap also has its own total ascent field. We fill it from the GPX calculation, so it need not match a recount of the stored points.
What did we learn from our own site?
This part is our own experience building a free GPX editor.
- One file, two answers. On 29 November 2026 our viewer showed 1,518 m for the sample ride, while our merge page and FIT export reported 1,818 m. The hike showed 71 m in one place and 99 m in another. The cause was two calculations in one product. We moved to one shared function with the 5 m rule.
-
Our check script disagreed too. A separate analysis script used
>where the site code uses>=. It printed 1,508 m for the 5 m rule, 10 m away from the site. - A format conversion is a data change. The FIT writing library we use cuts off the remainder of an altitude step, so our converter adjusts each height to get the nearest step.
If you only need the numbers for one file, our GPX viewer shows climb and descent with an elevation profile. For your own pipeline, publish the threshold, operator, segment rule and elevation source next to the total.
FAQ
Why does Strava show different elevation than Garmin?
The two can start from different height data and filter it differently. Strava's Elevation on Strava FAQs says its "thresholds and smoothing process may differ from those of other platforms or GPS devices." The same page says climbing must occur consistently for more than 10 metres without strong barometric data, or more than 2 metres with it, before it is added. It also notes that accuracy depends on your file's GPS data and Strava's basemap. We did not test Strava or a Garmin device.
How to calculate elevation gain from a GPX file?
Read the track point heights in order and apply the threshold function above. Report the threshold with the result, for example "1,518 m at 5 m".
Why is my elevation gain different on every app?
The height source and the filtering can both differ. Strava's Elevation help article says it generally considers barometric data higher quality than GPS-derived data, and treats elevation gain as an estimate. Recording conditions matter too. A study of 28 barometric cycling devices (Menaspa and colleagues, 2016) compared them with an online mapping tool, reported standard errors of 1.5% and 1.9% for two brands, and found that wet weather significantly decreased accuracy. A small flat-track study (Ammann and colleagues, 2016, three participants) measured larger elevation errors with sport watches worn on the wrist than on the hip.
What threshold should I use to calculate elevation gain?
There is no universal value. We use 5 m so that every page of our site gives one answer. That is a consistency choice, not proof that 5 m is physically correct. Run your own files at several thresholds and compare them with a reference you trust.
How do I calculate elevation gain from GPX in Python?
Save the script above as gain.py and run python gain.py ride.gpx. It uses only the standard library and prints gain and loss in metres. Call climb(ele, threshold=3.0) to try another threshold.
Written by the GPX Editor team. We make the editor and viewer linked in this article.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.