Skip to content

Commit

Permalink
Merge branch 'devel' and bump version to 1.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tmuguet committed Dec 3, 2017
2 parents e73af08 + 3261a3b commit b6a1547
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 50 deletions.
61 changes: 44 additions & 17 deletions js/map2gpx.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ L.Polyline.include({

var _interpolateTrackData = function _interpolateTrackData(deferred, coords, coordsLeft, depth) {

// Avoid interpolating when too long
if (coords.length > 500) {
return $.Deferred(function () {
var _this = this;
Expand All @@ -395,28 +396,43 @@ var _interpolateTrackData = function _interpolateTrackData(deferred, coords, coo

var 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
var 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: coordsLeft });
});
}
deferred.notify({ line: straight, count: coords.length - 1 });
return $.Deferred(function () {
this.resolve({ line: straight, mode: 'straight', coordsLeft: coordsLeft, count: coords.length });
});
}

return $.Deferred(function () {
var _this = this;

L.polyline_findAuto(coords[0], coords[coords.length - 1]).done(function (geojson) {
var d = l.distanceTo(geojson);
if (d < 10) {
var found = false;
var haversineDistance = coords[0].distanceTo(coords[coords.length - 1]);
var 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: coordsLeft });
} else {
_this.resolve({ line: geojson, mode: 'auto', coordsLeft: coordsLeft, count: coords.length });
found = true;
} else if (coords.length < 100) {
// Test if straight line is better
var _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: coordsLeft, count: coords.length });
found = true;
}
}

if (!found) {
// Could not find; interpolate on half of the track
var coords1 = coords.slice(0, Math.floor(coords.length / 2) + 1);
var coords2 = coords.slice(Math.floor(coords.length / 2));
var 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).fail(_this.reject);
}
Expand All @@ -439,7 +455,14 @@ L.polyline_interpolate = function (coords) {
lines.push(line);

if (line.coordsLeft.length > 0) {
_interpolateTrackData(_this, line.coordsLeft, [], 0).done(onDone).fail(_this.reject);
// Still some paths to interpolate.

// Don't try to interpolate the whole line.coordsLeft thing (usually won't work), use previously path found
var sizeToInterpolate = Math.min(line.count * 3, line.coordsLeft.length);
var coords1 = line.coordsLeft.slice(0, sizeToInterpolate + 1);
var coords2 = line.coordsLeft.slice(sizeToInterpolate);

_interpolateTrackData(_this, coords1, coords2, 0).done(onDone).fail(_this.reject);
} else {
_this.resolve(lines);
}
Expand Down Expand Up @@ -667,11 +690,15 @@ L.polyline_interpolate = function (coords) {
$.Cache = {};

var getKey = function getKey(coords) {
return coords.lng + '/' + coords.lat;
return getKeyLatLng(coords.lat, coords.lng);
};

var getKeyLatLng = function getKeyLatLng(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) {
Expand All @@ -684,7 +711,7 @@ L.polyline_interpolate = function (coords) {
};

$.Cache.addSlope = function (lat, lng, slope) {
_slopes[lng + '/' + lat] = slope;
_slopes[getKeyLatLng(lat, lng)] = slope;
};

$.Cache.getSlope = function (coords) {
Expand Down
2 changes: 1 addition & 1 deletion js/map2gpx.min.js

Large diffs are not rendered by default.

57 changes: 42 additions & 15 deletions js/map2gpx.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ L.Polyline.include({

const _interpolateTrackData = function (deferred, coords, coordsLeft, depth) {

// Avoid interpolating when too long
if (coords.length > 500) {
return $.Deferred(function () {
const _this = this;
Expand All @@ -420,27 +421,42 @@ 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 () {
const _this = this;

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)
Expand Down Expand Up @@ -468,7 +484,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 {
Expand Down Expand Up @@ -700,11 +723,15 @@ L.polyline_interpolate = function (coords) {
$.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) {
Expand All @@ -717,7 +744,7 @@ L.polyline_interpolate = function (coords) {
};

$.Cache.addSlope = function (lat, lng, slope) {
_slopes[lng + '/' + lat] = slope;
_slopes[getKeyLatLng(lat, lng)] = slope;
};

$.Cache.getSlope = function (coords) {
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.1",
"version": "1.3.2",
"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
47 changes: 35 additions & 12 deletions src/js/L.Polyline.interpolate.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,27 +16,42 @@ 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 () {
const _this = this;

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)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions src/js/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -22,7 +26,7 @@
};

$.Cache.addSlope = function (lat, lng, slope) {
_slopes[lng + '/' + lat] = slope;
_slopes[getKeyLatLng(lat, lng)] = slope;
};

$.Cache.getSlope = function (coords) {
Expand Down

0 comments on commit b6a1547

Please sign in to comment.