2020-07-16 23:41:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* @file
|
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.
|
|
|
|
|
*
|
|
|
|
|
* Functions in this file also report and offer to correct errors that may
|
|
|
|
|
* occur 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-01-14 23:19:26 +00:00
|
|
|
|
const {choosePlural, joinSentence} = require('../util');
|
2020-07-17 10:13:25 +00:00
|
|
|
|
const osm = require('./sources/osm');
|
|
|
|
|
const tam = require('./sources/tam');
|
2020-01-14 23:19:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use theoretical passings data to guess which lines use which stops in which
|
|
|
|
|
* direction.
|
|
|
|
|
*
|
|
|
|
|
* This is used for suggesting possible stop IDs for stops that don’t have
|
|
|
|
|
* one in OSM.
|
|
|
|
|
*
|
|
|
|
|
* @return Map containing for each stop its abbreviated name, the lines that
|
|
|
|
|
* use it and in which directions it is used.
|
|
|
|
|
*/
|
|
|
|
|
const fetchStopsRefAssociations = () => new Promise((res, rej) =>
|
|
|
|
|
{
|
|
|
|
|
const stops = {};
|
|
|
|
|
|
2020-07-17 10:13:25 +00:00
|
|
|
|
tam.fetchTheoretical((err, row) =>
|
2020-01-14 23:19:26 +00:00
|
|
|
|
{
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
rej(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-01-14 23:19:26 +00:00
|
|
|
|
if (!row)
|
|
|
|
|
{
|
|
|
|
|
res(stops);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 17:17:49 +00:00
|
|
|
|
let line = row.routeShortName;
|
|
|
|
|
|
|
|
|
|
if (line === '4')
|
|
|
|
|
{
|
|
|
|
|
line += row.directionId === '0' ? 'A' : 'B';
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 23:19:26 +00:00
|
|
|
|
if (!(row.stopId in stops))
|
|
|
|
|
{
|
|
|
|
|
stops[row.stopId] = {
|
|
|
|
|
name: row.stopName,
|
2020-01-15 17:17:49 +00:00
|
|
|
|
lines: new Set([line]),
|
2020-01-14 23:19:26 +00:00
|
|
|
|
directions: new Set([row.tripHeadsign]),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const stop = stops[row.stopId];
|
|
|
|
|
|
|
|
|
|
if (stop.name !== row.stopName)
|
|
|
|
|
{
|
|
|
|
|
console.warn(`Stop ${row.stopId} has multiple names: \
|
|
|
|
|
“${row.stopName}” and “${stop.name}”. Only the first one will be considered.`);
|
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-01-15 17:17:49 +00:00
|
|
|
|
stop.lines.add(line);
|
2020-01-14 23:19:26 +00:00
|
|
|
|
stop.directions.add(row.tripHeadsign);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Mapping for abbreviations used in stop names
|
|
|
|
|
const stopAbbreviations = {
|
2020-01-15 23:34:47 +00:00
|
|
|
|
st: 'saint',
|
2020-01-14 23:19:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a stop name to a canonical representation suitable for
|
|
|
|
|
* comparing two names.
|
|
|
|
|
*
|
|
|
|
|
* @param stopName Original stop name.
|
|
|
|
|
* @return List of normalized tokens in the name.
|
|
|
|
|
*/
|
|
|
|
|
const canonicalizeStopName = stopName => stopName
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
|
|
|
|
|
// Remove diacritics
|
2020-01-15 23:34:47 +00:00
|
|
|
|
.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
|
2020-01-14 23:19:26 +00:00
|
|
|
|
|
|
|
|
|
// Only keep alpha-numeric characters
|
|
|
|
|
.replace(/[^a-z0-9]/g, ' ')
|
|
|
|
|
|
|
|
|
|
// Split in tokens longer than two characters
|
|
|
|
|
.split(/\s+/g).filter(part => part.length >= 2)
|
|
|
|
|
|
|
|
|
|
// Resolve well-known abbreviations
|
|
|
|
|
.map(part => part in stopAbbreviations ? stopAbbreviations[part] : part);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Compute a matching score between two stop names.
|
|
|
|
|
*
|
|
|
|
|
* @param fullName Stop name in full.
|
|
|
|
|
* @param abbrName Abbreviated stop name.
|
|
|
|
|
* @return Matching score (number of common tokens).
|
|
|
|
|
*/
|
|
|
|
|
const matchStopNames = (fullName, abbrName) =>
|
|
|
|
|
{
|
|
|
|
|
const canonicalFullName = canonicalizeStopName(fullName);
|
|
|
|
|
const canonicalAbbrName = canonicalizeStopName(abbrName);
|
|
|
|
|
|
|
|
|
|
return canonicalFullName.filter(part =>
|
|
|
|
|
canonicalAbbrName.findIndex(abbrPart =>
|
|
|
|
|
part.startsWith(abbrPart)
|
|
|
|
|
) !== -1
|
|
|
|
|
).length;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-16 23:41:05 +00:00
|
|
|
|
* Fetch stops and lines of the network.
|
2020-01-14 23:19:26 +00:00
|
|
|
|
*
|
|
|
|
|
* @param lineRefs List of lines to fetch.
|
|
|
|
|
* @return Object with a set of stops, segments and lines.
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
relation[network="TaM"][type="route_master"][ref~"^(${lineRefs.join('|')})$"];
|
|
|
|
|
|
|
|
|
|
// Recursively fetch routes, ways and stops inside the line
|
|
|
|
|
(._; >>;);
|
|
|
|
|
|
|
|
|
|
out body qt;
|
2020-01-14 23:19:26 +00:00
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
// Retrieve stop associations from TaM
|
|
|
|
|
const associations = await fetchStopsRefAssociations();
|
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
|
|
|
|
|
const elements = elementsList.reduce((prev, elt) =>
|
|
|
|
|
{
|
|
|
|
|
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 = {};
|
|
|
|
|
|
|
|
|
|
for (let routeMaster of routeMasters)
|
|
|
|
|
{
|
|
|
|
|
const lineRef = routeMaster.tags.ref;
|
|
|
|
|
const color = routeMaster.tags.colour || '#000000';
|
|
|
|
|
|
|
|
|
|
// Extract all routes for the given line
|
|
|
|
|
const rawRoutes = routeMaster.members.map(({ref}) => elements[ref]);
|
|
|
|
|
|
|
|
|
|
// Add missing stops to the result object
|
|
|
|
|
for (let route of rawRoutes)
|
|
|
|
|
{
|
|
|
|
|
for (let {ref, role} of route.members)
|
|
|
|
|
{
|
|
|
|
|
if (role === 'stop')
|
|
|
|
|
{
|
|
|
|
|
const stop = elements[ref];
|
|
|
|
|
|
|
|
|
|
if (!('ref' in stop.tags))
|
|
|
|
|
{
|
2020-01-14 23:19:26 +00:00
|
|
|
|
console.warn(`Stop ${stop.id} is missing a “ref” tag
|
|
|
|
|
Name: ${stop.tags.name}
|
2020-01-15 17:17:49 +00:00
|
|
|
|
Part of line: ${route.tags.name}
|
2020-07-17 10:13:25 +00:00
|
|
|
|
URI: ${osm.viewNode(stop.id)}
|
2020-01-14 23:19:26 +00:00
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
// Try to identify stops matching this stop in the
|
|
|
|
|
// TaM-provided data, using the stop name, line number
|
|
|
|
|
// and trip direction
|
|
|
|
|
const candidates = Object.entries(associations).filter(
|
2020-01-15 23:34:47 +00:00
|
|
|
|
([, {lines}]) => lines.has(route.tags.ref)
|
2020-01-14 23:19:26 +00:00
|
|
|
|
).map(([stopRef, {name, lines, directions}]) => ({
|
|
|
|
|
stopRef,
|
|
|
|
|
lines,
|
|
|
|
|
|
|
|
|
|
name,
|
|
|
|
|
nameScore: matchStopNames(stop.tags.name, name),
|
|
|
|
|
|
|
|
|
|
directions,
|
|
|
|
|
directionScore: Math.max(
|
|
|
|
|
...Array.from(directions).map(direction =>
|
|
|
|
|
matchStopNames(route.tags.to, direction)
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
}))
|
|
|
|
|
// Only keep non-zero scores for both criteria
|
|
|
|
|
.filter(({nameScore, directionScore}) =>
|
|
|
|
|
nameScore && directionScore
|
|
|
|
|
)
|
|
|
|
|
// Sort by best name score then best direction
|
|
|
|
|
.sort(({
|
|
|
|
|
nameScore: nameScore1,
|
2020-01-15 23:34:47 +00:00
|
|
|
|
directionScore: directionScore1,
|
2020-01-14 23:19:26 +00:00
|
|
|
|
}, {
|
|
|
|
|
nameScore: nameScore2,
|
2020-01-15 23:34:47 +00:00
|
|
|
|
directionScore: directionScore2,
|
2020-01-14 23:19:26 +00:00
|
|
|
|
}) =>
|
|
|
|
|
(nameScore2 - nameScore1)
|
|
|
|
|
|| (directionScore2 - directionScore1)
|
|
|
|
|
)
|
|
|
|
|
.slice(0, 4);
|
|
|
|
|
|
|
|
|
|
if (candidates.length === 0)
|
|
|
|
|
{
|
2020-07-16 20:56:39 +00:00
|
|
|
|
console.warn('No candidate found in TaM data.');
|
2020-01-14 23:19:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
console.warn('Candidates:');
|
|
|
|
|
|
|
|
|
|
for (let candidate of candidates)
|
|
|
|
|
{
|
|
|
|
|
console.warn(`\
|
|
|
|
|
— Stop ${candidate.stopRef} with name “${candidate.name}” used by \
|
|
|
|
|
${choosePlural(candidate.lines.length, 'line', '.s')} \
|
|
|
|
|
${joinSentence(Array.from(candidate.lines), ', ', ' and ')} going to \
|
|
|
|
|
${joinSentence(Array.from(candidate.directions), ', ', ' or ')}
|
2020-07-17 10:13:25 +00:00
|
|
|
|
Apply in JOSM: ${osm.addTagsToNode(stop.id, ['ref=' + candidate.stopRef])}
|
2020-01-14 23:19:26 +00:00
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.warn('');
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(stop.tags.ref in stops))
|
|
|
|
|
{
|
|
|
|
|
stops[stop.tags.ref] = {
|
|
|
|
|
lat: stop.lat,
|
|
|
|
|
lon: stop.lon,
|
|
|
|
|
name: stop.tags.name,
|
2020-01-16 18:10:30 +00:00
|
|
|
|
lines: new Set([lineRef]),
|
2020-01-14 13:08:08 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-16 18:10:30 +00:00
|
|
|
|
stops[stop.tags.ref].lines.add(lineRef);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
// Reconstruct the line’s route from stop to stop
|
|
|
|
|
const routes = [];
|
|
|
|
|
|
2020-01-14 13:08:08 +00:00
|
|
|
|
for (let route of rawRoutes)
|
|
|
|
|
{
|
2020-07-17 10:13:25 +00:00
|
|
|
|
const {from, to, name} = route.tags;
|
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(
|
|
|
|
|
({role}) => role === ''
|
|
|
|
|
);
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
if (!route.members.slice(0, relationPivot).every(
|
|
|
|
|
({role}) => role === 'stop' || role === 'platform'
|
|
|
|
|
))
|
2020-01-14 13:08:08 +00:00
|
|
|
|
{
|
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(
|
|
|
|
|
({role}) => role === ''
|
|
|
|
|
))
|
2020-01-14 13:08:08 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
const stops = route.members.slice(0, relationPivot)
|
|
|
|
|
.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)
|
|
|
|
|
.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 = [];
|
|
|
|
|
let currentNode = stops[0];
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
for (let wayIndex = 0; wayIndex < ways.length; wayIndex += 1)
|
|
|
|
|
{
|
2020-07-17 10:13:25 +00:00
|
|
|
|
const way = elements[ways[wayIndex]];
|
|
|
|
|
const {nodes: wayNodes, tags: wayTags} = 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-16 20:56:39 +00:00
|
|
|
|
if (wayIndex + 1 < ways.length)
|
|
|
|
|
{
|
|
|
|
|
const nextNodeCandidates = elements[ways[wayIndex + 1]]
|
|
|
|
|
.nodes.filter(node => wayNodesSet.has(node));
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
if (nextNodeCandidates.length !== 1)
|
2020-01-14 13:08:08 +00:00
|
|
|
|
{
|
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-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-16 20:56:39 +00:00
|
|
|
|
nextNodeIndex = wayNodes.length;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
if (curNodeIndex < nextNodeIndex)
|
|
|
|
|
{
|
|
|
|
|
// Use the way in its normal direction
|
|
|
|
|
path = path.concat(
|
|
|
|
|
wayNodes.slice(curNodeIndex, nextNodeIndex)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Use the way in the reverse direction
|
2020-07-17 10:13:25 +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
|
|
|
|
|
const segments = [];
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
for (let stopIndex = 0; stopIndex + 1 < stops.length; ++stopIndex)
|
|
|
|
|
{
|
|
|
|
|
segments.push(path.slice(
|
|
|
|
|
path.indexOf(stops[stopIndex]),
|
2020-07-16 23:41:05 +00:00
|
|
|
|
path.indexOf(stops[stopIndex + 1]) + 1,
|
2020-07-16 20:56:39 +00:00
|
|
|
|
).map(id => ({
|
|
|
|
|
lat: elements[id].lat,
|
2020-07-16 22:16:54 +00:00
|
|
|
|
lon: elements[id].lon
|
2020-07-16 20:56:39 +00:00
|
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routes.push({
|
2020-07-17 10:13:25 +00:00
|
|
|
|
from, to, name,
|
2020-07-16 20:56:39 +00:00
|
|
|
|
segments,
|
|
|
|
|
stops: stops.map(id => elements[id].tags.ref),
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
lines[lineRef] = {
|
|
|
|
|
color,
|
2020-01-15 23:34:47 +00:00
|
|
|
|
routes,
|
2020-01-14 13:08:08 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 20:56:39 +00:00
|
|
|
|
return {stops, lines};
|
2020-01-14 23:19:26 +00:00
|
|
|
|
};
|
2020-01-14 13:08:08 +00:00
|
|
|
|
|
|
|
|
|
exports.fetch = fetch;
|