Skip to content

Commit

Permalink
Merge branch 'fix/fix-computation' (fixes #7) and bump version to 1.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
tmuguet committed Apr 16, 2018
2 parents f63c19e + 5315afa commit f1d36c0
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 44 deletions.
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ <h2><i class="fa fa-area-chart fa-3x" aria-hidden="true"></i><br/>Rien à montre
Commencez à tracer votre itinéraire pour voir les données le concernant.
</div>
<div id="data-invalid" class="data">
<h2><i class="fa fa-exclamation-triangle fa-3x" aria-hidden="true"></i><br/>Tracé invalide !</h2>
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.
<h2><i class="fa fa-exclamation-triangle fa-3x" aria-hidden="true"></i><br/>Oups</h2>
L'itinéraire n'a pas pu être calculé... Vous pouvez ré-essayer en déplacer le marqueur.<br/>
<span id="data-invalid-status"></span>
</div>
<div id="data-computing" class="data"></div>
<div id="loading" style="display: block; position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; background-color: #FFFFFF; z-index: 5000; text-align: center;">
Expand Down
67 changes: 54 additions & 13 deletions js/map2gpx.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,12 @@ L.polyline_interpolate = function (coords) {
$.each(listeners, function () {
this.progress('update', progress);
});
},

failed: function failed(error) {
$.each(listeners, function () {
this.progress('failed', error);
});
}
};
})(jQuery);
Expand Down Expand Up @@ -639,6 +645,14 @@ L.polyline_interpolate = function (coords) {
if (_started) this.start();else this.stop();
},

failed: function failed(error) {
this.$status.text('Une erreur est survenue');
$('<div><small>' + error + '</small></div>').insertAfter(this.$h2).fadeOut(10000, function () {
$(this).remove();
});
this._trigger('failed', null, { error: error });
},

update: function update(progress) {
if (Array.isArray(progress)) {
var _this = this;
Expand Down Expand Up @@ -916,9 +930,11 @@ L.Layer.include({

computeStats: function computeStats() {
var _this = this;
var latlngs = _this.getLatLngsFlatten();

return $.Deferred(function () {
var deferred = this; // jscs:ignore safeContextKeyword
var promises = _this._fetchAltitude().concat(_this._fetchSlope());
var promises = _this._fetchAltitude(latlngs).concat(_this._fetchSlope(latlngs));
var total = promises.length;

deferred.notify({ start: true, total: total, status: 'Récupération des données géographiques...' });
Expand All @@ -929,17 +945,30 @@ L.Layer.include({
});
});

$.when.apply($, promises).fail(deferred.reject).then(function () {
_this._computeStats();
$.when.apply($, promises).fail(deferred.reject).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() {
_computeStats: function _computeStats(latlngs) {
var elevations = [];

$.each(this.getLatLngsFlatten(), function (j, coords) {
$.each(latlngs, function (j, coords) {
var values = $.extend({}, { lat: coords.lat, lng: coords.lng }, $.Cache.getInfos(coords));
elevations.push(values);
});
Expand Down Expand Up @@ -982,11 +1011,11 @@ L.Layer.include({
return true;
},

_fetchAltitude: function _fetchAltitude() {
_fetchAltitude: function _fetchAltitude(latlngs) {
var geometry = []; // Batch
var promises = [];

$.each(this.getLatLngsFlatten(), function (j, coords) {
$.each(latlngs, function (j, coords) {
if (!$.Cache.hasAltitude(coords)) {
// Skip already cached values
geometry.push({
Expand All @@ -1009,12 +1038,12 @@ L.Layer.include({
return promises;
},

_fetchSlope: function _fetchSlope() {
_fetchSlope: function _fetchSlope(latlngs) {
var tiles = {};
var promises = [];
var map = this._map || this._mapToAdd;

$.each(this.getLatLngsFlatten(), function (j, coords) {
$.each(latlngs, function (j, coords) {
if (!$.Cache.hasSlope(coords)) {
// Skip already cached values
var _coords$toTilePixel = coords.toTilePixel(map.options.crs, 16, 256, map.getPixelOrigin()),
Expand Down Expand Up @@ -1589,7 +1618,7 @@ L.Layer.include({
$.when.apply($, promises).done(function () {
_this.fire('markerschanged');
deferred.resolve();
}).fail(deferred.fail);
}).fail(deferred.reject);
});
},

Expand All @@ -1609,7 +1638,7 @@ L.Layer.include({
$.when.apply($, promises).done(function () {
_this.fire('markerschanged');
deferred.resolve();
}).fail(deferred.fail);
}).fail(deferred.reject);
});
},

Expand Down Expand Up @@ -2086,7 +2115,8 @@ L.Layer.include({
_this.attachRouteFrom(to, geojson, mode);

$(_this).startCompute(function (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(function () {
return $(_this).endCompute(next);
Expand All @@ -2108,7 +2138,13 @@ L.Layer.include({
});
});

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);
});
Expand Down Expand Up @@ -2726,13 +2762,18 @@ window.onload = function () {

$('#data-computing').progress().on('progressstatechanged', function (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', function (e, data) {
$('#data-invalid-status').text(data.error);
$('#data-invalid').fadeIn();
});
$.Queue.bindTo($('#data-computing'));

$.BlockingQueue.bindTo({
start: function start() {
return $('#pending').fadeIn();
Expand Down
2 changes: 1 addition & 1 deletion js/map2gpx.min.js

Large diffs are not rendered by default.

65 changes: 52 additions & 13 deletions js/map2gpx.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@ L.polyline_interpolate = function (coords) {
this.progress('update', progress);
});
},

failed: function (error) {
$.each(listeners, function () {
this.progress('failed', error);
});
},
};

})(jQuery);
Expand Down Expand Up @@ -672,6 +678,12 @@ L.polyline_interpolate = function (coords) {
this.stop();
},

failed: function (error) {
this.$status.text('Une erreur est survenue');
$('<div><small>' + error + '</small></div>').insertAfter(this.$h2).fadeOut(10000, function () {$(this).remove();});
this._trigger('failed', null, { error });
},

update: function (progress) {
if (Array.isArray(progress)) {
const _this = this;
Expand Down Expand Up @@ -939,9 +951,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...' });
Expand All @@ -954,17 +968,30 @@ 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);
});
Expand Down Expand Up @@ -1014,11 +1041,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,
Expand All @@ -1040,12 +1067,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());

Expand Down Expand Up @@ -1655,7 +1682,7 @@ L.Layer.include({
$.when.apply($, promises).done(() => {
_this.fire('markerschanged');
deferred.resolve();
}).fail(deferred.fail);
}).fail(deferred.reject);
});
},

Expand All @@ -1679,7 +1706,7 @@ L.Layer.include({
$.when.apply($, promises).done(() => {
_this.fire('markerschanged');
deferred.resolve();
}).fail(deferred.fail);
}).fail(deferred.reject);
});
},

Expand Down Expand Up @@ -2154,7 +2181,8 @@ L.Layer.include({
_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));

Expand All @@ -2174,7 +2202,13 @@ L.Layer.include({
});
});

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);
});
Expand Down Expand Up @@ -2786,13 +2820,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(),
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "map2gpx",
"version": "1.3.4",
"version": "1.3.5",
"description": "map2gpx is a web application that lets you trace paths on maps (for hiking for instance) and export them to GPX or KML formats.\\n",
"main": "n/a",
"private": true,
Expand Down
6 changes: 6 additions & 0 deletions src/js/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
this.stop();
},

failed: function (error) {
this.$status.text('Une erreur est survenue');
$('<div><small>' + error + '</small></div>').insertAfter(this.$h2).fadeOut(10000, function () {$(this).remove();});
this._trigger('failed', null, { error });
},

update: function (progress) {
if (Array.isArray(progress)) {
const _this = this;
Expand Down
6 changes: 6 additions & 0 deletions src/js/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
this.progress('update', progress);
});
},

failed: function (error) {
$.each(listeners, function () {
this.progress('failed', error);
});
},
};

})(jQuery);
Loading

0 comments on commit f1d36c0

Please sign in to comment.