mpetazzoni / leaflet-gpx

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

get_distance() is wrong when <ele> is missing (suite of issue #110) #115

Closed andre92160 closed 3 years ago

andre92160 commented 3 years ago

You've misunderstood the issue #110, which describes the effect of missing \<ele> on distance. It's not a problem of accuracy getting the distance from longitude and latitude. Your function distance() calculates the horizontal distance h from longitudes and latitudes, the vertical distance v from the altitudes, and then calculates d = sqrt(h²+v²). Your calcul of h is ok. What is wrong is your calcul of v: you assume the elevation of a point is 0 when the point has no \<ele>. Take 3 points: first and third have an \<ele> of 3000 m; but second one has no \<ele>. The horizontal distance between 2 consecutives points, calculated from their longitudes and latitudes is 100 m. As it doesn't have an \<ele> you assume the 2nd point has an elevation of 0m. Thus the distance between the first and the second point is sqrt(100²+3000²)=3002m. And the distance between the second and third point is the same: 3002m. So the distance betwwen points 1 and 3 is 6004m! You shoud assume that point 2, which has no \<ele> has an elevation which is the mean between points 1 and 3: 3000m. So the distance between points 1 and 3 becomes 100m + 100m = 200m... instead of 6004m. It's not just a problem of accuracy...

mpetazzoni commented 3 years ago

My diff should also fix that problem. It ensures that if a point does not define an elevation from an <ele> tag, we use the previous point's distance (transitively).

And because the _dist3d() uses the .meta.ele property as well (see line 610), in

this._info.length += this._dist3d(last, ll);

var t = ll.meta.ele - last.meta.ele;
if (t > 0) {
  this._info.elevation.gain += t;
} else {
  this._info.elevation.loss += Math.abs(t);
}

Both the calculation through _dist3d() and the one to accumulate into elevation.gain or elevation.loss should be correct and not be impacted by a point with no <ele>. Using the latest code in the main branch, is that not the behavior you're seeing?