Update routing to new navigation format

This commit is contained in:
Mattéo Delabre 2021-05-23 15:19:45 +02:00
parent e696761f8e
commit 05d7faa725
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
2 changed files with 27 additions and 13 deletions

View File

@ -37,6 +37,11 @@ const parseTime = (time, reference) => {
return result; 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. */ /** Guess whether a stop comes directly before another one. */
const isPenultimateStop = (stop, finalStop) => { const isPenultimateStop = (stop, finalStop) => {
// If there is a standard segment linking both stops, its certainly // If there is a standard segment linking both stops, its certainly
@ -53,8 +58,8 @@ const isPenultimateStop = (stop, finalStop) => {
} }
// If there is another stop in the way, it cant be either // If there is another stop in the way, it cant be either
for (const node of route.slice(1, -1)) { for (const nodeId of route.slice(1, -1)) {
if (node in network.stops) { if (stopsSet.has(nodeId)) {
return false; return false;
} }
} }

View File

@ -16,14 +16,23 @@ for (const [beginId, begin] of Object.entries(network.navigation)) {
} }
/** /**
* Find the shortest path of nodes linking two nodes or stops. * Get the OSM ID of a stop.
* @param {string} from ID of the starting stop or node. * @param {string} stopId Network ID of the stop.
* @param {string} to ID of the ending stop or node. * @return {string} OSM ID of the stop.
* @return {Array.<string>} If possible, list of nodes joining `from` to `to`.
*/ */
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.<string>} If it exists, a path of nodes joining the two stops.
*/
export const findPath = (fromStop, toStop) => {
try { try {
return dijkstra.find_path(graph, from, to); return dijkstra.find_path(
graph, stopToOSM(fromStop), stopToOSM(toStop)
);
} catch (err) { } catch (err) {
return null; return null;
} }
@ -31,12 +40,12 @@ export const findPath = (from, to) => {
/** /**
* Find the shortest segment linking two nodes or stops. * Find the shortest segment linking two nodes or stops.
* @param {string} from ID of the starting stop or node. * @param {string} fromStop Network ID of the starting stop.
* @param {string} to ID of the ending stop or node. * @param {string} toStop Network ID of the ending stop.
* @return {LineString?} If it exists, a segment linking the two nodes. * @return {LineString?} If it exists, a segment joining the two stops.
*/ */
export const findSegment = (from, to) => { export const findSegment = (fromStop, toStop) => {
const path = findPath(from, to); const path = findPath(fromStop, toStop);
if (path === null) { if (path === null) {
return null; return null;