Handle undefined altitude coordinate on latLng points

This commit is contained in:
alexcojocaru 2020-11-02 21:14:49 -08:00
parent 0005752cee
commit 6bb769c2da

View file

@ -143,81 +143,62 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
*/ */
_buildGeojsonFeatures: function(latLngs) { _buildGeojsonFeatures: function(latLngs) {
var self = this; var self = this;
// TODO set the alt to undefined on the first few points
var features = []; // TODO set the alt to undefined on the last few points
// TODO set the alt to undefined on the first and last few points
// this is going to be initialized on the first buffer flush, no need to initialize now // TODO set the alt to undefined on all but first point
var currentFeature; // TODO set the alt to undefined on all but last point
// TODO set the alt to undefined on all points
// undefined is fine, as it will be different than the current gradient // TODO set the alt to undefined on all between first and last points
// when the buffer is flushed for the first time // TODO set the alt to undefined on all between first and middle point, and on all between middle and last point
var previousGradient;
// since the altitude coordinate on points is not very reliable, let's normalize it // since the altitude coordinate on points is not very reliable, let's normalize it
// by averaging the gradient over several point (within the min distance defined below) // by taking into account only the altitude on points at a given min distance
var buffer = [];
var bufferDistance = 0;
// the minimum distance (in meters) between the points in the buffer; // the minimum distance (in meters) between points which we consider
// once reached, the buffer is flushed; // for the purpose of calculating altitudes and gradients;
// consider 200 segments on the route, at least 200m long each
// for short routes, make sure we still have enough of a distance to normalize over; // for short routes, make sure we still have enough of a distance to normalize over;
// for long routes, we can afford to normalized over a longer distance, // for long routes, we can afford to normalized over a longer distance,
// hence increasing the accuracy // hence increasing the accuracy
var totalDistance = self._calculateDistance(latLngs); var totalDistance = self._calculateDistance(latLngs);
var bufferMinDistance = Math.max(totalDistance / 200, 200); var bufferMinDistance = Math.max(totalDistance / 200, 200);
if (latLngs.length > 0) { var segments = self._partitionByMinDistance(latLngs, bufferMinDistance);
buffer.push(latLngs[0]);
}
for (var i = 1; i < latLngs.length; i++) {
buffer.push(latLngs[i]); // the buffer contains at least 2 points by now
bufferDistance =
bufferDistance +
// never negative
buffer[buffer.length - 1].distanceTo(buffer[buffer.length - 2]);
// if we reached the tipping point, var features = [];
// each point in the buffer gets the same gradient rating,
// and the buffer is flushed into the existing feature or a new one
if (bufferDistance >= bufferMinDistance) {
var currentGradient = self._calculateGradient(buffer);
if (currentGradient == previousGradient) { // this is going to be initialized in the first loop, no need to initialize now
// the gradient hasn't changed, we can flush the buffer into the last feature; var currentFeature;
// since the buffer contains, at index 0,
// the last point on the feature (it was pushed into it on buffer reset),
// add only points from index 1 onward
self._addPointsToFeature(currentFeature, buffer.slice(1));
} else {
// the gradient has changed; flush into a new feature
currentFeature = self._buildFeature(buffer, currentGradient);
features.push(currentFeature);
}
// reset to prepare for the next iteration // undefined is fine, as it will be different to the current gradient in the first loop
previousGradient = currentGradient; var previousGradient;
var lastPoint = buffer[buffer.length - 1]; // before clearing the buffer
buffer = [lastPoint];
bufferDistance = 0;
}
}
// handle the remaining points in the buffer segments.forEach(function(segment) {
if (typeof currentFeature === 'undefined') { var currentGradient = self._calculateGradient(segment);
// no feature was build so far
if (buffer.length > 1) { if (typeof currentGradient === 'undefined') {
// building a feature with the few points on the route // not enough points on the segment to calculate the gradient
var currentGradient = self._calculateGradient(buffer); currentFeature = self._buildFeature(segment, currentGradient);
currentFeature = self._buildFeature(buffer, currentGradient); features.push(currentFeature);
} else if (currentGradient == previousGradient) {
// the gradient hasn't changed, we can append this segment to the last feature;
// since the segment contains, at index 0 the last point on the feature,
// add only points from index 1 onward
self._addPointsToFeature(currentFeature, segment.slice(1));
} else {
// the gradient has changed; create a new feature
currentFeature = self._buildFeature(segment, currentGradient);
features.push(currentFeature); features.push(currentFeature);
} }
} else {
// adding the extra points to the last feature; // reset to prepare for the next iteration
// since the buffer contains, at index 0, previousGradient = currentGradient;
// the last point on the feature (it was pushed into it on buffer reset), });
// add only points from index 1 onward // TODO at the end of 3rd render (breakpoint on line 203), feature 37 has undefined points;
self._addPointsToFeature(currentFeature, buffer.slice(1)); // test with previous working version for errors in console
}
// TODO when elevation profile is open, the toggle button should be blue, not gray
return [ return [
{ {
@ -232,6 +213,63 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
]; ];
}, },
/**
* Given the list of latLng points, partition them into segments
* at least _minDistance__ meters long,
* where the first and last points always have a valid altitude.
* NOTE: Given that some of the given points might not have a valid altitude,
* the first point(s) in the first buffer, as well as the last point(s)
* in the last buffer, might not have a valid altitude.
*/
_partitionByMinDistance: function(latLngs, minDistance) {
var segments = [];
// temporary buffer where we add points
// until the distance between them is at least minDistance
var buffer = [];
// push all points up to (and including) the first one with a valid altitude
var index = 0;
for (; index < latLngs.length; index++) {
var latLng = latLngs[index];
buffer.push(latLng);
if (typeof latLng.alt !== 'undefined') {
break;
}
}
var bufferDistance = this._calculateDistance(buffer);
for (; index < latLngs.length; index++) {
var latLng = latLngs[index];
buffer.push(latLng); // the buffer contains at least 2 points by now
bufferDistance =
bufferDistance +
// never negative
buffer[buffer.length - 1].distanceTo(buffer[buffer.length - 2]);
// if we reached the tipping point, add the buffer to segments, then flush it;
// if this point doesn't have a valid alt, continue to the next one
if (bufferDistance >= minDistance && typeof latLng.alt !== 'undefined') {
segments.push(buffer);
// re-init the buffer with the last point from the previous buffer
buffer = [buffer[buffer.length - 1]];
bufferDistance = 0;
}
}
// if the buffer is not empty, add all points from it into the last segment
if (segments.length === 0) {
segments.push(buffer);
} else if (buffer.length > 0) {
var lastSegment = segments[segments.length - 1];
buffer.forEach(function(p) {
lastSegment.push(p);
});
}
return segments;
},
/** /**
* Calculate the distance between all LatLng points in the given array. * Calculate the distance between all LatLng points in the given array.
*/ */
@ -249,6 +287,7 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
* The array must have at least 2 elements. * The array must have at least 2 elements.
*/ */
_calculateGradient: function(latLngs) { _calculateGradient: function(latLngs) {
// TODO what if .alt is undefined on the heading or trailing points
// the array is guaranteed to have 2+ elements // the array is guaranteed to have 2+ elements
var altDelta = latLngs[latLngs.length - 1].alt - latLngs[0].alt; var altDelta = latLngs[latLngs.length - 1].alt - latLngs[0].alt;
var distance = this._calculateDistance(latLngs); var distance = this._calculateDistance(latLngs);
@ -279,7 +318,6 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
geometry: { geometry: {
type: 'LineString', type: 'LineString',
coordinates: coordinates coordinates: coordinates
// coordinates: [[point1.lng, point1.lat, point1.alt], [point2.lng, point2.lat, point2.alt]]
}, },
properties: { properties: {
attributeType: gradient attributeType: gradient