2020-07-16 23:41:05 +00:00
|
|
|
|
/**
|
2020-07-25 16:05:43 +00:00
|
|
|
|
* @fileoverview
|
2020-07-17 10:13:25 +00:00
|
|
|
|
*
|
2020-07-16 23:41:05 +00:00
|
|
|
|
* Extract static information about the TaM network from OpenStreetMap (OSM):
|
|
|
|
|
* tram and bus lines, stops and routes.
|
|
|
|
|
*
|
2021-05-11 13:35:03 +00:00
|
|
|
|
* Functions in this file also report inconsistencies in OSM data.
|
2020-07-17 10:13:25 +00:00
|
|
|
|
*
|
|
|
|
|
* Because of the static nature of this data, it is cached in a
|
|
|
|
|
* version-controlled file `network.json` next to this file. To update it, use
|
|
|
|
|
* the `script/update-network` script.
|
2020-07-16 23:41:05 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const turfHelpers = require("@turf/helpers");
|
|
|
|
|
const turfLength = require("@turf/length").default;
|
|
|
|
|
const util = require("../util");
|
|
|
|
|
const osm = require("./sources/osm");
|
2020-01-14 23:19:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-07-16 23:41:05 +00:00
|
|
|
|
* Fetch stops and lines of the network.
|
2020-07-25 16:05:43 +00:00
|
|
|
|
* @param {string[]} lineRefs List of lines to fetch.
|
|
|
|
|
* @returns {{stops: Object, lines: Object, segments: Object}} Set of stops,
|
|
|
|
|
* segments and lines.
|
2020-01-14 23:19:26 +00:00
|
|
|
|
*/
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const fetch = async lineRefs => {
|
2020-01-14 13:08:08 +00:00
|
|
|
|
// Retrieve routes, ways and stops from OpenStreetMap
|
2020-07-17 10:13:25 +00:00
|
|
|
|
const rawData = await osm.runQuery(`[out:json];
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
// Find the public transport line bearing the requested reference
|
2020-07-25 16:05:43 +00:00
|
|
|
|
relation[network="TaM"][type="route_master"][ref~"^(${lineRefs.join("|")})$"];
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
// Recursively fetch routes, ways and stops inside the line
|
|
|
|
|
(._; >>;);
|
|
|
|
|
|
|
|
|
|
out body qt;
|
2020-01-14 23:19:26 +00:00
|
|
|
|
`);
|
|
|
|
|
|
2020-01-14 13:08:08 +00:00
|
|
|
|
// List of retrieved objects
|
2020-07-16 22:16:54 +00:00
|
|
|
|
const elementsList = rawData.elements;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
// List of retrieved lines
|
2020-07-17 10:13:25 +00:00
|
|
|
|
const routeMasters = elementsList.filter(osm.isTransportLine);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
// Retrieved objects indexed by ID
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const elements = elementsList.reduce((prev, elt) => {
|
2020-01-14 13:08:08 +00:00
|
|
|
|
prev[elt.id] = elt;
|
|
|
|
|
return prev;
|
|
|
|
|
}, {});
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// All stops in the network
|
2020-01-14 13:08:08 +00:00
|
|
|
|
const stops = {};
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// All transport lines of the network
|
2020-01-14 13:08:08 +00:00
|
|
|
|
const lines = {};
|
|
|
|
|
|
2020-07-17 21:48:32 +00:00
|
|
|
|
// All segments leading from one stop to another
|
|
|
|
|
const segments = {};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const routeMaster of routeMasters) {
|
2020-01-14 13:08:08 +00:00
|
|
|
|
const lineRef = routeMaster.tags.ref;
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const color = routeMaster.tags.colour || "#000000";
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
// Extract all routes for the given line
|
2020-07-22 21:10:43 +00:00
|
|
|
|
const routes = [];
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const [routeRef, data] of routeMaster.members.entries()) {
|
|
|
|
|
const routeId = data.ref;
|
2020-07-22 21:10:43 +00:00
|
|
|
|
const route = elements[routeId];
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const { from, via, to, name } = route.tags;
|
|
|
|
|
const state = route.tags.state || "normal";
|
2020-07-22 21:10:43 +00:00
|
|
|
|
|
|
|
|
|
// Add missing stops to the global stops object
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const { ref, role } of route.members) {
|
|
|
|
|
if (role === "stop") {
|
2020-01-14 13:08:08 +00:00
|
|
|
|
const stop = elements[ref];
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (!("ref" in stop.tags)) {
|
2020-07-22 21:10:43 +00:00
|
|
|
|
throw new Error(`Stop ${stop.id}
|
|
|
|
|
(${osm.viewNode(stop.id)}) on line ${route.tags.name} is missing
|
|
|
|
|
a “ref” tag`);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (!(stop.tags.ref in stops)) {
|
2020-07-25 14:28:51 +00:00
|
|
|
|
stops[stop.tags.ref] = turfHelpers.point([
|
|
|
|
|
stop.lon,
|
|
|
|
|
stop.lat
|
|
|
|
|
], {
|
2020-01-14 13:08:08 +00:00
|
|
|
|
name: stop.tags.name,
|
2020-07-27 19:01:21 +00:00
|
|
|
|
routes: [[lineRef, routeRef]],
|
2020-07-27 19:21:07 +00:00
|
|
|
|
successors: []
|
2020-07-25 14:28:51 +00:00
|
|
|
|
});
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
2020-07-25 14:28:51 +00:00
|
|
|
|
stops[stop.tags.ref].properties.routes.push([
|
|
|
|
|
lineRef,
|
|
|
|
|
routeRef
|
|
|
|
|
]);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// Check that the route consists of a block of stops and platforms
|
|
|
|
|
// followed by a block of routes as dictated by PTv2
|
|
|
|
|
const relationPivot = route.members.findIndex(
|
2020-07-25 16:05:43 +00:00
|
|
|
|
({ role }) => role === ""
|
2020-07-16 20:56:39 +00:00
|
|
|
|
);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
if (!route.members.slice(0, relationPivot).every(
|
2020-07-25 16:05:43 +00:00
|
|
|
|
({ role }) => role === "stop" || role === "platform"
|
|
|
|
|
)) {
|
2020-07-16 20:56:39 +00:00
|
|
|
|
throw new Error(`Members with invalid roles in between stops
|
2020-07-17 10:13:25 +00:00
|
|
|
|
of ${name}`);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
if (!route.members.slice(relationPivot).every(
|
2020-07-25 16:05:43 +00:00
|
|
|
|
({ role }) => role === ""
|
|
|
|
|
)) {
|
2020-07-16 20:56:39 +00:00
|
|
|
|
throw new Error(`Members with invalid roles inside the path
|
2020-07-17 10:13:25 +00:00
|
|
|
|
of ${name}`);
|
2020-07-16 20:56:39 +00:00
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// List of stops in the route, expected to be in the timetable
|
|
|
|
|
// order as per PTv2 and to be traversed in order by the sequence
|
|
|
|
|
// of ways extracted below
|
2020-07-22 21:10:43 +00:00
|
|
|
|
const lineStops = route.members.slice(0, relationPivot)
|
2020-07-25 16:05:43 +00:00
|
|
|
|
.filter(({ role }) => role === "stop")
|
|
|
|
|
.map(({ ref }) => ref);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// List of ways making up the route’s path through its stops
|
|
|
|
|
// with each way connected to the next through a single point
|
|
|
|
|
const ways = route.members.slice(relationPivot)
|
2020-07-25 16:05:43 +00:00
|
|
|
|
.map(({ ref }) => ref);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// Merge all used ways in a single path
|
|
|
|
|
let path = [];
|
2020-07-22 21:10:43 +00:00
|
|
|
|
let currentNode = lineStops[0];
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (let wayIndex = 0; wayIndex < ways.length; wayIndex += 1) {
|
2020-07-17 10:13:25 +00:00
|
|
|
|
const way = elements[ways[wayIndex]];
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const { nodes: wayNodes } = way;
|
2020-07-16 20:56:39 +00:00
|
|
|
|
const wayNodesSet = new Set(wayNodes);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
const curNodeIndex = wayNodes.indexOf(currentNode);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// If not the last way, find a connection point to the next way
|
|
|
|
|
// (there should be exactly one)
|
|
|
|
|
let nextNode = null;
|
|
|
|
|
let nextNodeIndex = null;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (wayIndex + 1 < ways.length) {
|
2020-07-16 20:56:39 +00:00
|
|
|
|
const nextNodeCandidates = elements[ways[wayIndex + 1]]
|
|
|
|
|
.nodes.filter(node => wayNodesSet.has(node));
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (nextNodeCandidates.length !== 1) {
|
2020-07-16 20:56:39 +00:00
|
|
|
|
throw new Error(`There should be exactly one point
|
2020-07-17 10:13:25 +00:00
|
|
|
|
connecting way n°${wayIndex} and way n°${wayIndex + 1} in ${name},
|
2020-07-16 20:56:39 +00:00
|
|
|
|
but there are ${nextNodeCandidates.length}`);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
2020-07-16 20:56:39 +00:00
|
|
|
|
|
|
|
|
|
nextNode = nextNodeCandidates[0];
|
|
|
|
|
nextNodeIndex = wayNodes.indexOf(nextNode);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
2020-07-16 20:56:39 +00:00
|
|
|
|
nextNodeIndex = wayNodes.length;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (curNodeIndex < nextNodeIndex) {
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// Use the way in its normal direction
|
|
|
|
|
path = path.concat(
|
|
|
|
|
wayNodes.slice(curNodeIndex, nextNodeIndex)
|
|
|
|
|
);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// Use the way in the reverse direction
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (osm.isOneWay(way)) {
|
2020-07-16 20:56:39 +00:00
|
|
|
|
throw new Error(`Way n°${wayIndex} in
|
2020-07-17 10:13:25 +00:00
|
|
|
|
${name} is one-way and cannot be used in reverse.`);
|
2020-07-16 20:56:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path = path.concat(
|
|
|
|
|
wayNodes.slice(nextNodeIndex + 1, curNodeIndex + 1)
|
2020-07-16 22:16:54 +00:00
|
|
|
|
.reverse()
|
2020-07-16 20:56:39 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentNode = nextNode;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// Split the path into segments between stops
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (let stopIdx = 0; stopIdx + 1 < lineStops.length; ++stopIdx) {
|
2020-07-22 21:10:43 +00:00
|
|
|
|
const begin = elements[lineStops[stopIdx]].tags.ref;
|
2020-07-24 23:16:36 +00:00
|
|
|
|
const beginIdx = path.indexOf(lineStops[stopIdx]);
|
2020-07-22 21:10:43 +00:00
|
|
|
|
const end = elements[lineStops[stopIdx + 1]].tags.ref;
|
2020-07-24 23:16:36 +00:00
|
|
|
|
const endIdx = path.indexOf(
|
|
|
|
|
lineStops[stopIdx + 1],
|
|
|
|
|
beginIdx
|
|
|
|
|
) + 1;
|
2020-07-17 21:48:32 +00:00
|
|
|
|
|
|
|
|
|
const id = `${begin}-${end}`;
|
2020-07-24 23:16:36 +00:00
|
|
|
|
const nodesIds = path.slice(beginIdx, endIdx);
|
2020-07-17 21:48:32 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (id in segments) {
|
2020-07-25 14:28:51 +00:00
|
|
|
|
if (!util.arraysEqual(
|
|
|
|
|
nodesIds,
|
|
|
|
|
segments[id].properties.nodesIds
|
2020-07-25 16:05:43 +00:00
|
|
|
|
)) {
|
2020-07-17 21:48:32 +00:00
|
|
|
|
throw new Error(`Segment ${id} is defined as a
|
|
|
|
|
different sequence of nodes in two or more lines.`);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 14:28:51 +00:00
|
|
|
|
segments[id].properties.routes.push([lineRef, routeRef]);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
segments[id] = turfHelpers.lineString(nodesIds.map(
|
|
|
|
|
nodeId => [
|
|
|
|
|
elements[nodeId].lon,
|
|
|
|
|
elements[nodeId].lat
|
|
|
|
|
]
|
|
|
|
|
), {
|
|
|
|
|
|
2020-07-17 21:48:32 +00:00
|
|
|
|
// Keep track of the original sequence of nodes to
|
|
|
|
|
// compare with duplicates
|
2020-07-24 23:02:07 +00:00
|
|
|
|
nodesIds,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
routes: [[lineRef, routeRef]]
|
2020-07-25 14:28:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-27 19:01:21 +00:00
|
|
|
|
stops[begin].properties.successors.push(end);
|
2020-07-25 14:28:51 +00:00
|
|
|
|
segments[id].properties.length = (
|
|
|
|
|
1000 * turfLength(segments[id]));
|
2020-07-17 21:48:32 +00:00
|
|
|
|
}
|
2020-07-16 20:56:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routes.push({
|
2020-07-25 16:05:43 +00:00
|
|
|
|
from,
|
|
|
|
|
via,
|
|
|
|
|
to,
|
|
|
|
|
name,
|
|
|
|
|
state
|
2020-07-16 20:56:39 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
lines[lineRef] = {
|
|
|
|
|
color,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
routes
|
2020-01-14 13:08:08 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 21:48:32 +00:00
|
|
|
|
// Remove OSM nodes from segments that were only used for checking validity
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const segment of Object.values(segments)) {
|
2020-07-25 14:28:51 +00:00
|
|
|
|
delete segment.properties.nodesIds;
|
2020-07-17 21:48:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
return { stops, lines, segments };
|
2020-01-14 23:19:26 +00:00
|
|
|
|
};
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
exports.fetch = fetch;
|