mpetazzoni / leaflet-gpx

A GPX track plugin for Leaflet.js
http://mpetazzoni.github.io/leaflet-gpx
BSD 2-Clause "Simplified" License
529 stars 114 forks source link

get_elevation_gain / get_elevation_loss #78

Closed yamila-moreno closed 5 years ago

yamila-moreno commented 5 years ago

Hi there!

I'm happily using this library for my routes (https://yamila-moreno.github.io/routes); I was using it to get the distance, which is very accurate.

Recently I wanted to add get_elevation_gain and loss to the description of my routes, and I found that the result of those functions is not the real gain or loss; I didn't find any consistency in the error (sometimes the given result is 4 times smaller than the real gain, and sometimes is 6 or other...). I tried to understand how is it calculated without any success.

This is the way I get the elevation:

var gain = (e.target.get_elevation_gain() / 10).toFixed(0);
var loss = (e.target.get_elevation_loss() / 10).toFixed(0);

is this correct? any help would be very appreciated. Thank you for your awesome library :)

mpetazzoni commented 5 years ago

Calculating elevation gain/loss is unfortunately very difficult, and depending on the resolution of the data will get widely different results. The library doesn't do anything specific here and simply adds up all the positive elevation increments in the "gain" and all the negative increments in the "loss". This may indeed lead to highly inflated total gain as your measured elevation may bounce up and down between each point.

I'd welcome more optimized and more accurate algorithms to calculate total gain and loss, but I'm honestly not sure how to approach this problem myself.

yamila-moreno commented 5 years ago

Hi @mpetazzoni Thanks for your answer. I took a deep look into the code to understand this part, and got to the same conclusion: the method may lead to wrong data (in my specific case all the gain/loss read from the gpx are significantly smaller than the real gain/loss) depending on the number of points (and the tracks I was using had few points).

Before trying to understand how other applications do this calculation, I tried to see the dispersion in the results. Oruxmaps, wikiloc or endomondo read the same .gpx file very differently. Which is real then? Is any of them close to the reality?

To sum up: the reality is difficult to measure.

Again, thanks for your answer; should I find a better way, I'd try with this library. Have a great week!

mpetazzoni commented 5 years ago

Yup as I said it's a very complicated problem, different algorithms might be used there, some solutions even also use corrected elevation data from 3rd party sources, etc.