From c970f24ec6dcdb0ced6387951a2266d51daff728 Mon Sep 17 00:00:00 2001 From: Thomas Muguet Date: Mon, 4 Dec 2017 00:06:59 +0100 Subject: [PATCH 1/2] Fixed stats on some imports --- src/js/cache.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/js/cache.js b/src/js/cache.js index 956774d..c6c911a 100644 --- a/src/js/cache.js +++ b/src/js/cache.js @@ -5,11 +5,15 @@ $.Cache = {}; const getKey = function (coords) { - return coords.lng + '/' + coords.lat; + return getKeyLatLng(coords.lat, coords.lng); + }; + + const getKeyLatLng = function (lat, lng) { + return Math.roundE8(lng) + '/' + Math.roundE8(lat); }; $.Cache.addAltitude = function (lat, lng, z) { - _altitudes[lng + '/' + lat] = z; + _altitudes[getKeyLatLng(lat, lng)] = z; }; $.Cache.getAltitude = function (coords) { @@ -22,7 +26,7 @@ }; $.Cache.addSlope = function (lat, lng, slope) { - _slopes[lng + '/' + lat] = slope; + _slopes[getKeyLatLng(lat, lng)] = slope; }; $.Cache.getSlope = function (coords) { From 3261a3bea9983c38b1c62b2b88248c9ef0e4b85b Mon Sep 17 00:00:00 2001 From: Thomas Muguet Date: Sun, 3 Dec 2017 21:56:13 +0100 Subject: [PATCH 2/2] Enhancing interpolation algorithm --- src/js/L.Polyline.interpolate.js | 47 ++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/js/L.Polyline.interpolate.js b/src/js/L.Polyline.interpolate.js index 51746eb..0c85580 100644 --- a/src/js/L.Polyline.interpolate.js +++ b/src/js/L.Polyline.interpolate.js @@ -1,6 +1,7 @@ const _interpolateTrackData = function (deferred, coords, coordsLeft, depth) { + // Avoid interpolating when too long if (coords.length > 500) { return $.Deferred(function () { const _this = this; @@ -15,13 +16,11 @@ const _interpolateTrackData = function (deferred, coords, coordsLeft, depth) { const l = new L.Polyline(coords); - if (coords.length < 100) { + if (coords.length <= 4) { + // We'll not be able to interpolate (came down to too few samples); just find a straight line and use it const straight = L.polyline_findStraight(coords[0], coords[coords.length - 1]); - - if (coords.length <= 4 || l.distanceTo(straight) < 10) { - deferred.notify({ line: straight, count: coords.length - 1 }); - return $.Deferred(function () { this.resolve({ line: straight, mode: 'straight', coordsLeft }); }); - } + deferred.notify({ line: straight, count: coords.length - 1 }); + return $.Deferred(function () { this.resolve({ line: straight, mode: 'straight', coordsLeft, count: coords.length }); }); } return $.Deferred(function () { @@ -29,13 +28,30 @@ const _interpolateTrackData = function (deferred, coords, coordsLeft, depth) { L.polyline_findAuto(coords[0], coords[coords.length - 1]) .done(function (geojson) { - const d = l.distanceTo(geojson); - if (d < 10) { + var found = false; + const haversineDistance = coords[0].distanceTo(coords[coords.length - 1]); + const threshold = Math.max(10, 2 * haversineDistance / 100); + + if (l.distanceTo(geojson) < threshold) { + // Found it deferred.notify({ line: geojson, count: coords.length - 1 }); - _this.resolve({ line: geojson, mode: 'auto', coordsLeft }); - } else { + _this.resolve({ line: geojson, mode: 'auto', coordsLeft, count: coords.length }); + found = true; + } else if (coords.length < 100) { + // Test if straight line is better + const straight = L.polyline_findStraight(coords[0], coords[coords.length - 1]); + if (l.distanceTo(straight) < threshold) { + // Found it + deferred.notify({ line: straight, count: coords.length - 1 }); + _this.resolve({ line: straight, mode: 'straight', coordsLeft, count: coords.length }); + found = true; + } + } + + if (!found) { + // Could not find; interpolate on half of the track const coords1 = coords.slice(0, Math.floor(coords.length / 2) + 1); - const coords2 = coords.slice(Math.floor(coords.length / 2)); + const coords2 = coords.slice(Math.floor(coords.length / 2)); // and concatenate rest of the track to the pending coordinates _interpolateTrackData(deferred, coords1, coords2.concat(coordsLeft.slice(1)), depth + 1) .done(_this.resolve) @@ -63,7 +79,14 @@ L.polyline_interpolate = function (coords) { lines.push(line); if (line.coordsLeft.length > 0) { - _interpolateTrackData(_this, line.coordsLeft, [], 0) + // Still some paths to interpolate. + + // Don't try to interpolate the whole line.coordsLeft thing (usually won't work), use previously path found + const sizeToInterpolate = Math.min(line.count * 3, line.coordsLeft.length); + const coords1 = line.coordsLeft.slice(0, sizeToInterpolate + 1); + const coords2 = line.coordsLeft.slice(sizeToInterpolate); + + _interpolateTrackData(_this, coords1, coords2, 0) .done(onDone) .fail(_this.reject); } else {