From d04e3824cc2e08d1325d14fa54817ad0ff5dcaff Mon Sep 17 00:00:00 2001 From: Thomas Muguet Date: Tue, 20 Feb 2018 00:53:18 +0100 Subject: [PATCH 1/3] Fixed fetching elevation/slope data while path is snaking in (fixes #7) --- src/js/stats.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/js/stats.js b/src/js/stats.js index 7c75636..b39549c 100644 --- a/src/js/stats.js +++ b/src/js/stats.js @@ -103,9 +103,11 @@ L.Layer.include({ computeStats: function () { const _this = this; + const latlngs = _this.getLatLngsFlatten(); + return $.Deferred(function () { const deferred = this; // jscs:ignore safeContextKeyword - const promises = _this._fetchAltitude().concat(_this._fetchSlope()); + const promises = _this._fetchAltitude(latlngs).concat(_this._fetchSlope(latlngs)); const total = promises.length; deferred.notify({ start: true, total: total, status: 'Récupération des données géographiques...' }); @@ -118,17 +120,29 @@ L.Layer.include({ $.when.apply($, promises) .fail(deferred.reject) - .then(function () { - _this._computeStats(); + .done(function () { + // Sanity checks + $.each(latlngs, function (j, coords) { + if (!$.Cache.hasAltitude(coords)) { + console.log('Could not find altitude for coordinates', coords); + deferred.rejectWith({ error: 'Impossible d\'obtenir les données d\'altitude' }); + } + if (!$.Cache.hasSlope(coords)) { + console.log('Could not find slope for coordinates', coords); + deferred.rejectWith({ error: 'Impossible d\'obtenir les données de pente' }); + } + }); + + _this._computeStats(latlngs); deferred.resolve(); }); }); }, - _computeStats: function () { + _computeStats: function (latlngs) { const elevations = []; - $.each(this.getLatLngsFlatten(), function (j, coords) { + $.each(latlngs, function (j, coords) { const values = $.extend({}, { lat: coords.lat, lng: coords.lng }, $.Cache.getInfos(coords)); elevations.push(values); }); @@ -178,11 +192,11 @@ L.Layer.include({ return true; }, - _fetchAltitude: function () { + _fetchAltitude: function (latlngs) { var geometry = []; // Batch const promises = []; - $.each(this.getLatLngsFlatten(), function (j, coords) { + $.each(latlngs, function (j, coords) { if (!$.Cache.hasAltitude(coords)) { // Skip already cached values geometry.push({ lon: coords.lng, @@ -204,12 +218,12 @@ L.Layer.include({ return promises; }, - _fetchSlope: function () { + _fetchSlope: function (latlngs) { const tiles = {}; const promises = []; const map = (this._map || this._mapToAdd); - $.each(this.getLatLngsFlatten(), function (j, coords) { + $.each(latlngs, function (j, coords) { if (!$.Cache.hasSlope(coords)) { // Skip already cached values const { tile, tilePixel } = coords.toTilePixel(map.options.crs, 16, 256, map.getPixelOrigin()); From bb0934b788e30953a83d88278e954871dbada0f6 Mon Sep 17 00:00:00 2001 From: Thomas Muguet Date: Tue, 20 Feb 2018 00:54:42 +0100 Subject: [PATCH 2/3] Nicer error handling if elevation or slope could not be fetched --- index.html | 5 +++-- src/js/progress.js | 6 ++++++ src/js/queue.js | 6 ++++++ src/js/script.js | 5 +++++ src/js/track.js | 7 ++++--- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index d5efb76..51b05a2 100644 --- a/index.html +++ b/index.html @@ -163,8 +163,9 @@


Rien à montre Commencez à tracer votre itinéraire pour voir les données le concernant.
-


Tracé invalide !

- Le tracé est invalide: l'itinéraire n'a pas pu être calculé avec le dernier marqueur. Déplacez le marqueur translucide pour ré-essayer un nouvel itinéraire. +


Oups

+ L'itinéraire n'a pas pu être calculé... Vous pouvez ré-essayer en déplacer le marqueur.
+
diff --git a/src/js/progress.js b/src/js/progress.js index fdb79f1..c2fff14 100644 --- a/src/js/progress.js +++ b/src/js/progress.js @@ -57,6 +57,12 @@ this.stop(); }, + failed: function (error) { + this.$status.text('Une erreur est survenue'); + $('
' + error + '
').insertAfter(this.$h2).fadeOut(10000, function () {$(this).remove();}); + this._trigger('failed', null, { error }); + }, + update: function (progress) { if (Array.isArray(progress)) { const _this = this; diff --git a/src/js/queue.js b/src/js/queue.js index 08cd2bf..74c9af3 100644 --- a/src/js/queue.js +++ b/src/js/queue.js @@ -51,6 +51,12 @@ this.progress('update', progress); }); }, + + failed: function (error) { + $.each(listeners, function () { + this.progress('failed', error); + }); + }, }; })(jQuery); diff --git a/src/js/script.js b/src/js/script.js index 3f696f5..14bae0c 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -15,13 +15,18 @@ window.onload = function () { $('#data-computing').progress().on('progressstatechanged', (e, data) => { if (data.started) { + $('#data-invalid').fadeOut(); $('#data-computing').fadeIn(); } else { $('#data').data('map2gpx-chart').replot($map.map('getTrack').computeStats()); $('#data-computing').fadeOut(); } + }).on('progressfailed', (e, data) => { + $('#data-invalid-status').text(data.error); + $('#data-invalid').fadeIn(); }); $.Queue.bindTo($('#data-computing')); + $.BlockingQueue.bindTo({ start: () => $('#pending').fadeIn(), stop: () => $('#pending').fadeOut(), diff --git a/src/js/track.js b/src/js/track.js index 54e9379..8232ba9 100644 --- a/src/js/track.js +++ b/src/js/track.js @@ -205,7 +205,7 @@ $.when.apply($, promises).done(() => { _this.fire('markerschanged'); deferred.resolve(); - }).fail(deferred.fail); + }).fail(deferred.reject); }); }, @@ -229,7 +229,7 @@ $.when.apply($, promises).done(() => { _this.fire('markerschanged'); deferred.resolve(); - }).fail(deferred.fail); + }).fail(deferred.reject); }); }, @@ -704,7 +704,8 @@ _this.attachRouteFrom(to, geojson, mode); $(_this).startCompute((next) => { - geojson.computeStats().progress($.Queue.notify).then(deferred.resolve).fail(function () { + geojson.computeStats().progress($.Queue.notify).done(deferred.resolve).fail(function () { + $.Queue.failed('Impossible d\'obtenir les données de la route'); deferred.rejectWith({ error: 'Impossible d\'obtenir les données de la route' }); }).always(() => $(_this).endCompute(next)); From 5315afa42021607a63c7778287dcfa2bba49b7d0 Mon Sep 17 00:00:00 2001 From: Thomas Muguet Date: Mon, 16 Apr 2018 23:02:22 +0200 Subject: [PATCH 3/3] Fixed a corner case with snaking --- src/js/stats.js | 1 + src/js/track.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/js/stats.js b/src/js/stats.js index b39549c..d0c8634 100644 --- a/src/js/stats.js +++ b/src/js/stats.js @@ -127,6 +127,7 @@ L.Layer.include({ console.log('Could not find altitude for coordinates', coords); deferred.rejectWith({ error: 'Impossible d\'obtenir les données d\'altitude' }); } + if (!$.Cache.hasSlope(coords)) { console.log('Could not find slope for coordinates', coords); deferred.rejectWith({ error: 'Impossible d\'obtenir les données de pente' }); diff --git a/src/js/track.js b/src/js/track.js index 8232ba9..f5e0db9 100644 --- a/src/js/track.js +++ b/src/js/track.js @@ -725,7 +725,13 @@ }); }); - geojson.snakeIn(); + try { + geojson.snakeIn(); + } catch (e) { + // With some weird tracks, snakeIn can fail (don't know why) + geojson._snakeEnd(); + } + _this.setOpacity(1); to.setOpacity(1); });