improvements to gradient normalization
This commit is contained in:
parent
0adc28d5df
commit
73ad89e727
1 changed files with 58 additions and 53 deletions
|
|
@ -12,23 +12,23 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
mappings: {
|
mappings: {
|
||||||
gradient: {
|
gradient: {
|
||||||
'-5': {
|
'-5': {
|
||||||
text: '16%+',
|
text: '- 16%+',
|
||||||
color: '#028306'
|
color: '#028306'
|
||||||
},
|
},
|
||||||
'-4': {
|
'-4': {
|
||||||
text: '10-15%',
|
text: '- 10-15%',
|
||||||
color: '#2AA12E'
|
color: '#2AA12E'
|
||||||
},
|
},
|
||||||
'-3': {
|
'-3': {
|
||||||
text: '7-9%',
|
text: '- 7-9%',
|
||||||
color: '#53BF56'
|
color: '#53BF56'
|
||||||
},
|
},
|
||||||
'-2': {
|
'-2': {
|
||||||
text: '4-6%',
|
text: '- 4-6%',
|
||||||
color: '#7BDD7E'
|
color: '#7BDD7E'
|
||||||
},
|
},
|
||||||
'-1': {
|
'-1': {
|
||||||
text: '1-3%',
|
text: '- 1-3%',
|
||||||
color: '#A4FBA6'
|
color: '#A4FBA6'
|
||||||
},
|
},
|
||||||
'0': {
|
'0': {
|
||||||
|
|
@ -117,23 +117,6 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
if (track && track.getLatLngs().length > 0) {
|
if (track && track.getLatLngs().length > 0) {
|
||||||
var geojsonFeatures = this._buildGeojsonFeatures(track.getLatLngs());
|
var geojsonFeatures = this._buildGeojsonFeatures(track.getLatLngs());
|
||||||
this.addData(geojsonFeatures);
|
this.addData(geojsonFeatures);
|
||||||
// TODO
|
|
||||||
/*
|
|
||||||
var geojson = track.toGeoJSON();
|
|
||||||
geojson.properties = { attributeType: 0 };
|
|
||||||
var data = [
|
|
||||||
{
|
|
||||||
type: 'FeatureCollection',
|
|
||||||
features: [geojson],
|
|
||||||
properties: {
|
|
||||||
Creator: 'OpenRouteService.org',
|
|
||||||
records: 1,
|
|
||||||
summary: 'gradient'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
];
|
|
||||||
this.addData(data);
|
|
||||||
*/
|
|
||||||
|
|
||||||
// re-add handlers
|
// re-add handlers
|
||||||
if (layer) {
|
if (layer) {
|
||||||
|
|
@ -163,11 +146,11 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
|
|
||||||
var features = [];
|
var features = [];
|
||||||
|
|
||||||
// this is going to be initialized on the first buffer flush
|
// this is going to be initialized on the first buffer flush, no need to initialize now
|
||||||
var currentFeature;
|
var currentFeature;
|
||||||
|
|
||||||
// undefined is fine, as it will be different than the current gradient
|
// undefined is fine, as it will be different than the current gradient
|
||||||
// in the first buffer flush
|
// when the buffer is flushed for the first time
|
||||||
var previousGradient;
|
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
|
||||||
|
|
@ -180,9 +163,13 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
var lastFeaturePoint;
|
var lastFeaturePoint;
|
||||||
|
|
||||||
// the minimum distance (in meters) between the points in the buffer;
|
// the minimum distance (in meters) between the points in the buffer;
|
||||||
// once reached, the buffer is flushed
|
// once reached, the buffer is flushed;
|
||||||
var bufferMinDistance = 200;
|
// for short routes, make sure we still have enough of a distance to normalize over;
|
||||||
// TODO calculate min distance based on the total route distance
|
// for long routes, we can afford to normalized over a longer distance,
|
||||||
|
// hence increasing the accuracy
|
||||||
|
var totalDistance = self._calculateDistance(latLngs);
|
||||||
|
var bufferMinDistance = Math.max(totalDistance / 200, 200);
|
||||||
|
console.log('using buffer min distance:', bufferMinDistance);
|
||||||
|
|
||||||
if (latLngs.length > 0) {
|
if (latLngs.length > 0) {
|
||||||
buffer.push(latLngs[0]);
|
buffer.push(latLngs[0]);
|
||||||
|
|
@ -197,20 +184,18 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
console.log('point:', latLngs[i], 'bufferDistance:', bufferDistance);
|
console.log('point:', latLngs[i], 'bufferDistance:', bufferDistance);
|
||||||
// if we reached the tipping point,
|
// if we reached the tipping point,
|
||||||
// each point in the buffer gets the same gradient rating,
|
// each point in the buffer gets the same gradient rating,
|
||||||
// and flush the buffer
|
// and the buffer is flushed into the existing feature or a new one
|
||||||
if (bufferDistance >= bufferMinDistance) {
|
if (bufferDistance >= bufferMinDistance) {
|
||||||
var altDelta = buffer[buffer.length - 1].alt - buffer[0].alt;
|
var currentGradient = self._calculateGradient(buffer);
|
||||||
var currentGradientPercentage = (altDelta * 100) / bufferDistance; // never division by 0
|
|
||||||
var currentGradient = self._mapGradient(currentGradientPercentage);
|
|
||||||
console.log('currentGradient:', currentGradient);
|
console.log('currentGradient:', currentGradient);
|
||||||
|
|
||||||
if (currentGradient == previousGradient) {
|
if (currentGradient == previousGradient) {
|
||||||
// the gradient hasn't changed; flush into the last feature
|
// the gradient hasn't changed, we can flush the buffer into the last feature;
|
||||||
|
// 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
|
||||||
console.log('adding points in buffer to the current feature:', buffer);
|
console.log('adding points in buffer to the current feature:', buffer);
|
||||||
buffer.forEach(function(point) {
|
self._addPointsToFeature(currentFeature, buffer.slice(1));
|
||||||
var coordinate = [point.lng, point.lat, point.alt];
|
|
||||||
currentFeature.geometry.coordinates.push(coordinate);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// the gradient has changed; flush into a new feature
|
// the gradient has changed; flush into a new feature
|
||||||
currentFeature = self._buildFeature(buffer, currentGradient);
|
currentFeature = self._buildFeature(buffer, currentGradient);
|
||||||
|
|
@ -218,10 +203,9 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
features.push(currentFeature);
|
features.push(currentFeature);
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare for the next iteration
|
// reset to prepare for the next iteration
|
||||||
previousGradient = currentGradient;
|
previousGradient = currentGradient;
|
||||||
lastFeaturePoint = buffer[buffer.length - 1]; // before clearing the buffer
|
lastFeaturePoint = buffer[buffer.length - 1]; // before clearing the buffer
|
||||||
// TODO this is wrong, as on line 209 and 249 it will be repeated
|
|
||||||
buffer = [lastFeaturePoint];
|
buffer = [lastFeaturePoint];
|
||||||
bufferDistance = 0;
|
bufferDistance = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -229,26 +213,23 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
|
|
||||||
// handle the remaining points in the buffer
|
// handle the remaining points in the buffer
|
||||||
if (typeof currentFeature === 'undefined') {
|
if (typeof currentFeature === 'undefined') {
|
||||||
|
// no feature was build so far
|
||||||
if (buffer.length > 1) {
|
if (buffer.length > 1) {
|
||||||
// TODO remove duplication
|
|
||||||
// building a feature with the few points on the route
|
// building a feature with the few points on the route
|
||||||
console.log('building a feature with the few points on the route');
|
console.log('building a feature with the few points on the route');
|
||||||
var altDelta = buffer[buffer.length - 1].alt - buffer[0].alt;
|
var currentGradient = self._calculateGradient(buffer);
|
||||||
// bufferDistance as already initialized in the main for loop
|
|
||||||
var currentGradientPercentage = (altDelta * 100) / bufferDistance; // never division by 0
|
|
||||||
var currentGradient = self._mapGradient(currentGradientPercentage);
|
|
||||||
|
|
||||||
currentFeature = self._buildFeature(buffer, currentGradient);
|
currentFeature = self._buildFeature(buffer, currentGradient);
|
||||||
features.push(currentFeature);
|
features.push(currentFeature);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// adding a few more points to the last feature
|
// adding the extra points to the last feature;
|
||||||
|
// 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
|
||||||
console.log('adding a few more points to the last feature; point count:', buffer.length);
|
console.log('adding a few more points to the last feature; point count:', buffer.length);
|
||||||
buffer.forEach(function(point) {
|
self._addPointsToFeature(currentFeature, buffer.slice(1));
|
||||||
var coordinate = [point.lng, point.lat, point.alt];
|
|
||||||
currentFeature.geometry.coordinates.push(coordinate);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// each feature starts with the last point on the previous feature;
|
// each feature starts with the last point on the previous feature;
|
||||||
// this will also take care of inserting the firstmost point
|
// this will also take care of inserting the firstmost point
|
||||||
|
|
@ -261,7 +242,6 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
var altDelta = currentPoint.alt - previousPoint.alt;
|
var altDelta = currentPoint.alt - previousPoint.alt;
|
||||||
var currentGradientPercentage = (altDelta * 100) / dist;
|
var currentGradientPercentage = (altDelta * 100) / dist;
|
||||||
var currentGradient = dist == 0 ? 0 : this._mapGradient(currentGradientPercentage);
|
var currentGradient = dist == 0 ? 0 : this._mapGradient(currentGradientPercentage);
|
||||||
// TODO
|
|
||||||
console.log(
|
console.log(
|
||||||
'gradient %:',
|
'gradient %:',
|
||||||
currentGradientPercentage,
|
currentGradientPercentage,
|
||||||
|
|
@ -306,10 +286,35 @@ BR.Heightgraph = function(map, layersControl, routing, pois) {
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
|
||||||
_buildFeature: function(subsequentPoints, gradient) {
|
_calculateDistance: function(latLngs) {
|
||||||
|
var distance = 0;
|
||||||
|
for (var i = 1; i < latLngs.length; i++) {
|
||||||
|
distance += latLngs[i].distanceTo(latLngs[i - 1]); // never negative
|
||||||
|
}
|
||||||
|
return distance;
|
||||||
|
},
|
||||||
|
|
||||||
|
_calculateGradient: function(latLngs) {
|
||||||
|
// the array is guaranteed to have 2+ elements
|
||||||
|
var altDelta = latLngs[latLngs.length - 1].alt - latLngs[0].alt;
|
||||||
|
var distance = this._calculateDistance(latLngs);
|
||||||
|
|
||||||
|
var currentGradientPercentage = distance == 0 ? 0 : (altDelta * 100) / distance;
|
||||||
|
var currentGradient = this._mapGradient(currentGradientPercentage);
|
||||||
|
return currentGradient;
|
||||||
|
},
|
||||||
|
|
||||||
|
_addPointsToFeature: function(feature, latLngs) {
|
||||||
|
latLngs.forEach(function(point) {
|
||||||
|
var coordinate = [point.lng, point.lat, point.alt];
|
||||||
|
feature.geometry.coordinates.push(coordinate);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
_buildFeature: function(latLngs, gradient) {
|
||||||
var coordinates = [];
|
var coordinates = [];
|
||||||
subsequentPoints.forEach(function(point) {
|
latLngs.forEach(function(latLng) {
|
||||||
coordinates.push([point.lng, point.lat, point.alt]);
|
coordinates.push([latLng.lng, latLng.lat, latLng.alt]);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue