From 05d7faa72532365b0e2d4a3239a19a156cab4f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Sun, 23 May 2021 15:19:45 +0200 Subject: [PATCH] Update routing to new navigation format --- src/tam/courses.js | 9 +++++++-- src/tam/routing.js | 31 ++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/tam/courses.js b/src/tam/courses.js index 95d3715..0be1cd1 100644 --- a/src/tam/courses.js +++ b/src/tam/courses.js @@ -37,6 +37,11 @@ const parseTime = (time, reference) => { return result; }; +/** List of OSM nodes that are stops. */ +const stopsSet = new Set( + Object.values(network.stops).map(stop => stop.properties.node) +); + /** Guess whether a stop comes directly before another one. */ const isPenultimateStop = (stop, finalStop) => { // If there is a standard segment linking both stops, it’s certainly @@ -53,8 +58,8 @@ const isPenultimateStop = (stop, finalStop) => { } // If there is another stop in the way, it can’t be either - for (const node of route.slice(1, -1)) { - if (node in network.stops) { + for (const nodeId of route.slice(1, -1)) { + if (stopsSet.has(nodeId)) { return false; } } diff --git a/src/tam/routing.js b/src/tam/routing.js index 840065e..e4ac6f8 100644 --- a/src/tam/routing.js +++ b/src/tam/routing.js @@ -16,14 +16,23 @@ for (const [beginId, begin] of Object.entries(network.navigation)) { } /** - * Find the shortest path of nodes linking two nodes or stops. - * @param {string} from ID of the starting stop or node. - * @param {string} to ID of the ending stop or node. - * @return {Array.} If possible, list of nodes joining `from` to `to`. + * Get the OSM ID of a stop. + * @param {string} stopId Network ID of the stop. + * @return {string} OSM ID of the stop. */ -export const findPath = (from, to) => { +export const stopToOSM = stopId => network.stops[stopId].properties.node; + +/** + * Find the shortest path of nodes linking two stops. + * @param {string} fromStop Network ID of the starting stop. + * @param {string} toStop Network ID of the ending stop. + * @return {Array.} If it exists, a path of nodes joining the two stops. + */ +export const findPath = (fromStop, toStop) => { try { - return dijkstra.find_path(graph, from, to); + return dijkstra.find_path( + graph, stopToOSM(fromStop), stopToOSM(toStop) + ); } catch (err) { return null; } @@ -31,12 +40,12 @@ export const findPath = (from, to) => { /** * Find the shortest segment linking two nodes or stops. - * @param {string} from ID of the starting stop or node. - * @param {string} to ID of the ending stop or node. - * @return {LineString?} If it exists, a segment linking the two nodes. + * @param {string} fromStop Network ID of the starting stop. + * @param {string} toStop Network ID of the ending stop. + * @return {LineString?} If it exists, a segment joining the two stops. */ -export const findSegment = (from, to) => { - const path = findPath(from, to); +export const findSegment = (fromStop, toStop) => { + const path = findPath(fromStop, toStop); if (path === null) { return null;