Interpolate coordinates in Mercator space for better accuracy

This commit is contained in:
Mattéo Delabre 2020-07-23 19:19:35 +02:00
parent d688bff813
commit f080bb808e
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
3 changed files with 1509 additions and 44 deletions

1508
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@turf/turf": "^5.1.6",
"axios": "^0.19.2", "axios": "^0.19.2",
"color": "^3.1.2", "color": "^3.1.2",
"csv-parse": "^4.8.3", "csv-parse": "^4.8.3",

View File

@ -1,4 +1,5 @@
const axios = require('axios'); const axios = require('axios');
const turf = require('@turf/turf');
const network = require('./network.json'); const network = require('./network.json');
const server = 'http://localhost:4321'; const server = 'http://localhost:4321';
@ -189,38 +190,46 @@ const updatePositions = (courses, time) =>
const previousNode = segment.points[nextNodeIndex - 1]; const previousNode = segment.points[nextNodeIndex - 1];
const nextNode = segment.points[nextNodeIndex]; const nextNode = segment.points[nextNodeIndex];
const previousPoint = turf.toMercator([
previousNode.lon,
previousNode.lat
]);
const nextPoint = turf.toMercator([
nextNode.lon,
nextNode.lat
]);
const curLength = course.traveledDistance const curLength = course.traveledDistance
- previousNode.distance; - previousNode.distance;
const totalLength = nextNode.distance const totalLength = nextNode.distance
- previousNode.distance; - previousNode.distance;
const progression = curLength / totalLength; const t = curLength / totalLength;
course.position = { course.position = [
lat: progression * nextNode.lat t * nextPoint[0] + (1 - t) * previousPoint[0],
+ (1 - progression) * previousNode.lat, t * nextPoint[1] + (1 - t) * previousPoint[1],
lon: progression * nextNode.lon ];
+ (1 - progression) * previousNode.lon,
};
} }
} }
else else
{ {
const currentStop = network.stops[course.currentStop]; const currentNode = network.stops[course.currentStop];
course.position = { course.position = turf.toMercator([
lat: currentStop.lat, currentNode.lon,
lon: currentStop.lon, currentNode.lat
}; ]);
} }
} }
}; };
const run = callback => const start = () =>
{ {
const courses = {}; const courses = {};
let lastFrame = null; let lastFrame = null;
let lastUpdate = null; let lastUpdate = null;
const loop = () => const update = () =>
{ {
const now = Date.now(); const now = Date.now();
@ -233,12 +242,9 @@ const run = callback =>
const time = lastFrame === null ? 0 : now - lastFrame; const time = lastFrame === null ? 0 : now - lastFrame;
lastFrame = now; lastFrame = now;
updatePositions(courses, time); updatePositions(courses, time);
callback(courses);
}; };
const interval = setInterval(loop, 24); return {courses, update};
return () => clearInterval(interval);
}; };
exports.run = run; exports.start = start;