Leaflet.Elevation
Leaflet.Elevation copied to clipboard
Error when displaying tracks with partially missing elevation data
I got some GPX files here, where elevation data for a few single points is missing. This leads to a display error in the elevation chart where the lines indicating the elevation drop below 0 on the y-axis.
In order to fix this error, I added a check that makes sure, that elevation data is available before adding a point to the chart to the _addGPXdata function. Tested it with version 0.0.2 and 0.0.4.
Update: Just realized, that @opoto created a fork that includes something very similar the below changes.
The new _addGPXdata function in leaflet.elevation-0.0.4-src.js around line 580 looks like this:
/*
* Parsing function for GPX data as used by https://github.com/mpetazzoni/leaflet-gpx
*/
_addGPXdata: function(coords) {
var opts = this.options;
if (coords) {
var data = this._data || [];
var dist = this._dist || 0;
var ele = this._maxElevation || 0;
for (var i = 0; i < coords.length; i++)
{
var s = coords[i];
var e = coords[i ? i - 1 : 0];
var newdist = opts.imperial ? s.distanceTo(e) * this.__mileFactor : s.distanceTo(e);
dist = dist + Math.round(newdist / 1000 * 100000) / 100000;
ele = ele < s.meta.ele ? s.meta.ele : ele;
//only add points with valid elevation data
if(s.meta.ele != null)
{
data.push({
dist: dist,
altitude: opts.imperial ? s.meta.ele * this.__footFactor : s.meta.ele,
x: s.lng,
y: s.lat,
latlng: s
});
}
}
this._dist = dist;
this._data = data;
ele = opts.imperial ? ele * this.__footFactor : ele;
this._maxElevation = ele;
}
},
See also PR #84 that shows missing elevation values as gaps by using a d3 defined accessor function.
Ignoring data points with missing elevation as proposed here and in #60 has some drawbacks:
- throws error when there is no elevation data at all (5)
- diagram "zooms" (x axis cropped) to part with elevation when missing at start or end, which can be misleading (3, 4)
- interpolates missing values, which can be misleading and not match reality (2)
Comparison:
master | #60 (opoto) | #84 (nrenner) |
---|---|---|
Plunker | Plunker | Plunker |
![]() |
![]() |
![]() |
Diagrams shown are variants of the same elevation profile with missing values at different positions:
- original
- middle (left of second segment)
- left
- right
- all missing