From 0fd13b0e8fc9dd06bf8d8745b0083075cc48c37f Mon Sep 17 00:00:00 2001 From: alexcojocaru Date: Mon, 2 Nov 2020 22:42:27 -0800 Subject: [PATCH] Testing... --- js/plugin/Heightgraph.js | 88 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 7 deletions(-) diff --git a/js/plugin/Heightgraph.js b/js/plugin/Heightgraph.js index cbc5052..7114c5c 100644 --- a/js/plugin/Heightgraph.js +++ b/js/plugin/Heightgraph.js @@ -152,6 +152,22 @@ BR.Heightgraph = function(map, layersControl, routing, pois) { // TODO set the alt to undefined on all between first and last points // TODO set the alt to undefined on all between first and middle point, and on all between middle and last point +var mockLatLngs = []; +for (var i = 0; i < 15; i++) { + var p = latLngs[i]; + var alt = i >= 2 && i < 13 ? p.alt : undefined; + mockLatLngs.push(L.latLng(p.lng, p.lat, alt)); + console.log('' + mockLatLngs[i].lng + ', ' + mockLatLngs[i].lat + ', ' + mockLatLngs[i].alt); +} +latLngs = mockLatLngs; +/* +var j = '['; +for (var i = 0; i < latLngs.length; i++) j+=(i>0?',':'')+'{lng:'+latLngs[i].lng+',lat:'+latLngs[i].lat+',alt:'+latLngs[i].alt+'}'; +j+=']'; +console.log(j); +*/ + + // since the altitude coordinate on points is not very reliable, let's normalize it // by taking into account only the altitude on points at a given min distance @@ -162,7 +178,9 @@ BR.Heightgraph = function(map, layersControl, routing, pois) { // for long routes, we can afford to normalized over a longer distance, // hence increasing the accuracy var totalDistance = self._calculateDistance(latLngs); +console.log('totalDistance:', totalDistance); var bufferMinDistance = Math.max(totalDistance / 200, 200); +console.log('bufferMinDistance:', bufferMinDistance); var segments = self._partitionByMinDistance(latLngs, bufferMinDistance); @@ -176,27 +194,32 @@ BR.Heightgraph = function(map, layersControl, routing, pois) { segments.forEach(function(segment) { var currentGradient = self._calculateGradient(segment); +console.log('currentGradient: ' + currentGradient); if (typeof currentGradient === 'undefined') { // not enough points on the segment to calculate the gradient currentFeature = self._buildFeature(segment, currentGradient); +console.log('new currentFeature for no gradient: ' + currentFeature); features.push(currentFeature); +console.log('features so far for no gradient: ' + features); } 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)); +console.log('existing currentFeature: ' + currentFeature); } else { // the gradient has changed; create a new feature currentFeature = self._buildFeature(segment, currentGradient); +console.log('new currentFeature: ' + currentFeature); features.push(currentFeature); +console.log('features so far for no gradient: ' + features); } // reset to prepare for the next iteration previousGradient = currentGradient; }); - // TODO at the end of 3rd render (breakpoint on line 203), feature 37 has undefined points; - // test with previous working version for errors in console +console.log('final features: ' + features); // TODO when elevation profile is open, the toggle button should be blue, not gray @@ -237,8 +260,10 @@ BR.Heightgraph = function(map, layersControl, routing, pois) { break; } } +console.log('buffer init: ' + buffer); var bufferDistance = this._calculateDistance(buffer); +console.log('buffer distance init: ' + bufferDistance); for (; index < latLngs.length; index++) { var latLng = latLngs[index]; buffer.push(latLng); // the buffer contains at least 2 points by now @@ -246,16 +271,23 @@ BR.Heightgraph = function(map, layersControl, routing, pois) { bufferDistance + // never negative buffer[buffer.length - 1].distanceTo(buffer[buffer.length - 2]); +console.log('buffer distance: ' + bufferDistance); +console.log('latLng.alt: ' + latLng.alt); // 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') { +console.log('wrapping up'); segments.push(buffer); +console.log('segments so far: ' + segments); // re-init the buffer with the last point from the previous buffer buffer = [buffer[buffer.length - 1]]; +console.log('buffer re-init: ' + buffer); bufferDistance = 0; } } +console.log('complete segments: ' + segments); +console.log('remaining buffer: ' + buffer); // if the buffer is not empty, add all points from it into the last segment if (segments.length === 0) { @@ -266,6 +298,7 @@ BR.Heightgraph = function(map, layersControl, routing, pois) { lastSegment.push(p); }); } +console.log('final segments: ' + segments); return segments; }, @@ -284,16 +317,57 @@ BR.Heightgraph = function(map, layersControl, routing, pois) { /** * Calculate the gradient between the first and last point in the LatLng array, * and map it to a gradient level. - * The array must have at least 2 elements. + * If less than 2 points have an altitude coordinate, the 0 gradient is returned. */ _calculateGradient: function(latLngs) { - // TODO what if .alt is undefined on the heading or trailing points - // the array is guaranteed to have 2+ elements - var altDelta = latLngs[latLngs.length - 1].alt - latLngs[0].alt; - var distance = this._calculateDistance(latLngs); +console.log('calculating gradient for latLngs: ' + latLngs); + if (latLngs.length < 2) { +console.log('too few points'); + return this._mapGradient(0); + } + + // find the index of the first point with a valid altitude + var firstIndex = -1; + for (var i = 0; i < latLngs.length; i++) { + if (typeof latLngs[i].alt !== 'undefined') { + firstIndex = i; + break; + } + } +console.log('firstIndex:', firstIndex); + // if no point with a valid altitude was found, there's not much to do here + if (firstIndex == -1) { + return this._mapGradient(0); + } + + // find the index of the last point with a valid altitude + var lastIndex = latLngs.length; + for (var i = latLngs.length - 1; i > firstIndex; i--) { + if (typeof latLngs[i].alt !== 'undefined') { + lastIndex = i; + break; + } + } +console.log('lastIndex:', lastIndex); + // if no point with a valid altitude was found between firstIndex and end of array, + // there's not much else to do + if (lastIndex == latLngs.length) { + return this._mapGradient(0); + } + + var altDelta = latLngs[lastIndex].alt - latLngs[firstIndex].alt; +console.log('altDelta:', altDelta); + + // calculate the distance only from firstIndex to lastIndex; + // points before or after don't have a valid altitude, + // hence they are not included in the gradient calculation + var distance = this._calculateDistance(latLngs.slice(firstIndex, lastIndex + 1)); +console.log('distance', distance); var currentGradientPercentage = distance == 0 ? 0 : (altDelta * 100) / distance; +console.log('currentGradientPercentage:', currentGradientPercentage); var currentGradient = this._mapGradient(currentGradientPercentage); +console.log('currentGradient:', currentGradient); return currentGradient; },