Improve navigation graph compression
This commit is contained in:
parent
474fc94a06
commit
a51a80a4b3
|
@ -70,7 +70,7 @@ import * as osm from "./sources/osm.js";
|
|||
*/
|
||||
|
||||
/**
|
||||
* Edge of the graph for out-of-route navigation between stops.
|
||||
* Edge of the graph for navigating between stops.
|
||||
* @typedef {Object} NavigationEdge
|
||||
* @property {string} type Always equal to "Feature".
|
||||
* @property {Object} properties
|
||||
|
@ -83,13 +83,18 @@ import * as osm from "./sources/osm.js";
|
|||
* Sequence of points forming this edge (as longitude/latitude pairs).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Graph for navigating between stops.
|
||||
* @typedef {Object.<string,Object.<string,NavigationEdge>>} Navigation
|
||||
*/
|
||||
|
||||
/**
|
||||
* Information about a public transport network.
|
||||
* @typedef {Object} Network
|
||||
* @property {Object.<string,Stop>} stops List of stops.
|
||||
* @property {Object.<string,Line>} lines List of lines.
|
||||
* @property {Object.<string,Segment>} segments List of segments.
|
||||
* @property {Object.<string,Object.<string,NavigationEdge>>} navigation
|
||||
* @property {Navigation} navigation
|
||||
* Graph for out-of-route navigation between stops.
|
||||
*/
|
||||
|
||||
|
@ -332,45 +337,20 @@ different sequence of nodes in two or more lines.`);
|
|||
};
|
||||
|
||||
/**
|
||||
* Create a graph for navigating between stops outside of
|
||||
* regular planned routes.
|
||||
* @property {Object.<string,Stop>} stops List of stops.
|
||||
* Create a graph for navigating between stops.
|
||||
* @param {Array.<Object>} elementsList List of nodes retrieved from OSM.
|
||||
* @param {Object.<string,Object>} elementsById OSM nodes indexed by their ID.
|
||||
* @return {Object.<string,Object.<string,NavigationEdge>} Resulting graph.
|
||||
*/
|
||||
const createNavigationGraph = (stops, elementsList, elementsById) => {
|
||||
// Graph of network stops and junctions
|
||||
const createNavigationGraph = (elementsList, elementsById) => {
|
||||
const navigation = {};
|
||||
|
||||
// Predecessors of each graph node
|
||||
const navigationReverse = {};
|
||||
|
||||
// Stops indexed by their OSM ID instead of their network ID
|
||||
const stopsReverse = Object.fromEntries(
|
||||
Object.entries(stops).map(([id, stop]) => [stop.properties.node, id])
|
||||
);
|
||||
|
||||
// Get the ID of a node in the navigation graph
|
||||
// (its network ID if it is a network object, otherwise its OSM id)
|
||||
const getNavigationId = objId => (
|
||||
objId in stopsReverse
|
||||
? stopsReverse[objId]
|
||||
: objId.toString()
|
||||
);
|
||||
|
||||
// Get the OSM ID of a navigation object
|
||||
const getOSMId = navId => (
|
||||
navId in stops
|
||||
? stops[navId].properties.node
|
||||
: navId
|
||||
);
|
||||
|
||||
// Create graph nodes from OSM nodes
|
||||
for (const obj of elementsList) {
|
||||
if (obj.type === "node") {
|
||||
navigation[getNavigationId(obj.id)] = {};
|
||||
navigationReverse[getNavigationId(obj.id)] = {};
|
||||
navigation[obj.id] = {};
|
||||
navigationReverse[obj.id] = {};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -378,95 +358,202 @@ const createNavigationGraph = (stops, elementsList, elementsById) => {
|
|||
for (const obj of elementsList) {
|
||||
if (obj.type === "way") {
|
||||
const oneWay = osm.isOneWay(obj);
|
||||
const pairs = obj.nodes.slice(0, -1).map(
|
||||
(node, i) => [
|
||||
getNavigationId(node),
|
||||
getNavigationId(obj.nodes[i + 1]),
|
||||
]
|
||||
);
|
||||
|
||||
for (const [from, to] of pairs) {
|
||||
navigation[from][to] = [from, to];
|
||||
for (let i = 0; i + 1 < obj.nodes.length; ++i) {
|
||||
const from = obj.nodes[i];
|
||||
let to = obj.nodes[i + 1];
|
||||
let path = [from.toString(), to.toString()];
|
||||
|
||||
// Make sure we can’t jump between rails at railway crossings
|
||||
if (i + 2 < obj.nodes.length
|
||||
&& osm.isRailwayCrossing(elementsById[to])) {
|
||||
const next = obj.nodes[i + 2];
|
||||
path = [from.toString(), to.toString(), next.toString()];
|
||||
to = next;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
navigation[from][to] = path;
|
||||
navigationReverse[to][from] = true;
|
||||
|
||||
if (!oneWay) {
|
||||
navigation[to][from] = [to, from];
|
||||
const reversePath = [...path];
|
||||
reversePath.reverse();
|
||||
navigation[to][from] = reversePath;
|
||||
navigationReverse[from][to] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark nodes of the graph to be kept
|
||||
const nodesToKeep = {};
|
||||
return { navigation, navigationReverse };
|
||||
};
|
||||
|
||||
for (const nodeId in navigation) {
|
||||
if (nodeId in stops) {
|
||||
// Keep stop nodes
|
||||
nodesToKeep[nodeId] = true;
|
||||
continue;
|
||||
}
|
||||
/**
|
||||
* Remove and relink nodes that connect only two nodes or less.
|
||||
* @param {Object.<string,Stop>} stops List of stops.
|
||||
* @param {Navigation} navigation Input navigation graph.
|
||||
* @param {Object.<string,Object.<string,boolean>>} navigationReverse
|
||||
* Backward edges of the navigation graph.
|
||||
*/
|
||||
const compressNavigationGraph = (stops, navigation, navigationReverse) => {
|
||||
const stopsReverse = Object.fromEntries(
|
||||
Object.entries(stops).map(([id, stop]) => [stop.properties.node, id])
|
||||
);
|
||||
|
||||
const entries = new Set(Object.keys(navigationReverse[nodeId]));
|
||||
const exits = new Set(Object.keys(navigation[nodeId]));
|
||||
let removedDeadEnds = true;
|
||||
const nodesToCompress = {};
|
||||
|
||||
// Keep split nodes, i.e. nodes with at least two exit nodes
|
||||
// and one entry node that are all distinct from each other
|
||||
if (entries.size >= 1) {
|
||||
if (exits.size >= 3) {
|
||||
nodesToKeep[nodeId] = true;
|
||||
while (removedDeadEnds) {
|
||||
// Identify nodes to be compressed
|
||||
for (const nodeId in navigation) {
|
||||
if (nodeId in stopsReverse) {
|
||||
// Keep stop nodes
|
||||
continue;
|
||||
}
|
||||
|
||||
if (exits.size === 2) {
|
||||
for (const entry of entries) {
|
||||
if (!exits.has(entry)) {
|
||||
nodesToKeep[nodeId] = true;
|
||||
continue;
|
||||
const entries = new Set(Object.keys(navigationReverse[nodeId]));
|
||||
const exits = new Set(Object.keys(navigation[nodeId]));
|
||||
|
||||
// Keep split nodes, i.e. nodes with at least two exit nodes
|
||||
// and one entry node that are all distinct from each other
|
||||
if (entries.size >= 1) {
|
||||
if (exits.size >= 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let isSplit = false;
|
||||
|
||||
if (exits.size === 2) {
|
||||
for (const entry of entries) {
|
||||
if (!exits.has(entry)) {
|
||||
isSplit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isSplit) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Keep junction nodes, i.e. nodes with at least two entry nodes
|
||||
// and one exit node that are all distinct from each other
|
||||
if (exits.size >= 1) {
|
||||
if (entries.size >= 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let isJunction = false;
|
||||
|
||||
if (entries.size === 2) {
|
||||
for (const exit of exits) {
|
||||
if (!entries.has(exit)) {
|
||||
isJunction = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isJunction) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Compress all other nodes
|
||||
nodesToCompress[nodeId] = true;
|
||||
}
|
||||
|
||||
// Find nodes that cannot be used to directly link up two kept nodes
|
||||
const usedNodes = {};
|
||||
|
||||
for (const beginId in navigation) {
|
||||
if (beginId in nodesToCompress) {
|
||||
continue;
|
||||
}
|
||||
|
||||
usedNodes[beginId] = true;
|
||||
|
||||
// Start a DFS from each node to be kept
|
||||
const begin = navigation[beginId];
|
||||
const stack = [];
|
||||
const parent = {[beginId]: beginId};
|
||||
|
||||
for (const succId in begin) {
|
||||
if (succId in nodesToCompress) {
|
||||
stack.push(succId);
|
||||
parent[succId] = beginId;
|
||||
}
|
||||
}
|
||||
|
||||
while (stack.length > 0) {
|
||||
const endId = stack.pop();
|
||||
const end = navigation[endId];
|
||||
|
||||
if (!(endId in nodesToCompress)) {
|
||||
let trackback = parent[endId];
|
||||
|
||||
while (trackback !== beginId) {
|
||||
usedNodes[trackback] = true;
|
||||
trackback = parent[trackback];
|
||||
}
|
||||
} else {
|
||||
for (const succId in end) {
|
||||
if (succId !== parent[endId]) {
|
||||
parent[succId] = endId;
|
||||
stack.push(succId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep junction nodes, i.e. nodes with at least two entry nodes
|
||||
// and one exit node that are all distinct from each other
|
||||
if (exits.size >= 1) {
|
||||
if (entries.size >= 3) {
|
||||
nodesToKeep[nodeId] = true;
|
||||
continue;
|
||||
}
|
||||
// Remove dead-end nodes
|
||||
removedDeadEnds = false;
|
||||
|
||||
if (entries.size === 2) {
|
||||
for (const exit of exits) {
|
||||
if (!entries.has(exit)) {
|
||||
nodesToKeep[nodeId] = true;
|
||||
continue;
|
||||
}
|
||||
for (const beginId in navigation) {
|
||||
if (!(beginId in usedNodes)) {
|
||||
for (const neighborId in navigation[beginId]) {
|
||||
delete navigationReverse[neighborId][beginId];
|
||||
}
|
||||
|
||||
for (const neighborId in navigationReverse[beginId]) {
|
||||
delete navigation[neighborId][beginId];
|
||||
}
|
||||
|
||||
delete navigation[beginId];
|
||||
delete navigationReverse[beginId];
|
||||
removedDeadEnds = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compress edges between nodes of interest
|
||||
for (const beginId in nodesToKeep) {
|
||||
// Perform node compression
|
||||
for (const beginId in navigation) {
|
||||
if (beginId in nodesToCompress) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start a DFS from each node to be kept
|
||||
const begin = navigation[beginId];
|
||||
const stack = [];
|
||||
const parent = {[beginId]: beginId};
|
||||
|
||||
for (const succId in begin) {
|
||||
stack.push(succId);
|
||||
parent[succId] = beginId;
|
||||
if (succId in nodesToCompress) {
|
||||
stack.push(succId);
|
||||
parent[succId] = beginId;
|
||||
}
|
||||
}
|
||||
|
||||
while (stack.length > 0) {
|
||||
const endId = stack.pop();
|
||||
const end = navigation[endId];
|
||||
|
||||
if (endId in nodesToKeep) {
|
||||
if (endId in begin) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(endId in nodesToCompress)) {
|
||||
// Found another kept node
|
||||
// Collect and remove intermediate nodes
|
||||
const reversePath = [endId];
|
||||
let trackback = parent[endId];
|
||||
let oneWay = !(trackback in end);
|
||||
|
@ -476,6 +563,7 @@ const createNavigationGraph = (stops, elementsList, elementsById) => {
|
|||
oneWay = oneWay || !(parent[trackback] in navigation[trackback]);
|
||||
|
||||
delete navigation[trackback];
|
||||
delete navigationReverse[trackback];
|
||||
trackback = parent[trackback];
|
||||
}
|
||||
|
||||
|
@ -483,20 +571,24 @@ const createNavigationGraph = (stops, elementsList, elementsById) => {
|
|||
const forwardPath = [...reversePath];
|
||||
forwardPath.reverse();
|
||||
|
||||
// Create edges to link both nodes directly
|
||||
delete begin[forwardPath[1]];
|
||||
delete navigationReverse[endId][reversePath[1]];
|
||||
|
||||
delete end[reversePath[1]];
|
||||
delete navigationReverse[beginId][forwardPath[1]];
|
||||
|
||||
if (!(endId in begin)) {
|
||||
begin[endId] = forwardPath;
|
||||
navigationReverse[endId][beginId] = true;
|
||||
}
|
||||
|
||||
if (!oneWay) {
|
||||
delete end[reversePath[1]];
|
||||
|
||||
if (!(beginId in end)) {
|
||||
end[beginId] = reversePath;
|
||||
}
|
||||
if (!oneWay && !(beginId in end)) {
|
||||
end[beginId] = reversePath;
|
||||
navigationReverse[beginId][endId] = true;
|
||||
}
|
||||
} else {
|
||||
// Continue the traversal down unused nodes
|
||||
let isFirst = true;
|
||||
|
||||
for (const succId in end) {
|
||||
|
@ -511,30 +603,23 @@ non-junction node ${endId}`);
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isFirst) {
|
||||
// Reached a dead-end: remove the path
|
||||
let trackback = endId;
|
||||
|
||||
while (parent[trackback] !== beginId) {
|
||||
delete navigation[trackback];
|
||||
trackback = parent[trackback];
|
||||
}
|
||||
|
||||
delete navigation[trackback];
|
||||
delete begin[trackback];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Convert graph edges to GeoJSON line strings
|
||||
/**
|
||||
* Transform navigation graph edges into GeoJSON segments.
|
||||
* @param {Navigation} navigation Input navigation graph.
|
||||
* @param {Object.<string,Object>} elementsById OSM nodes indexed by their ID.
|
||||
*/
|
||||
const makeNavigationSegments = (navigation, elementsById) => {
|
||||
for (const [beginId, begin] of Object.entries(navigation)) {
|
||||
for (const endId in begin) {
|
||||
begin[endId] = turfHelpers.lineString(begin[endId].map(
|
||||
nodeId => [
|
||||
elementsById[getOSMId(nodeId)].lon,
|
||||
elementsById[getOSMId(nodeId)].lat
|
||||
elementsById[nodeId].lon,
|
||||
elementsById[nodeId].lat
|
||||
]
|
||||
), {
|
||||
begin: beginId,
|
||||
|
@ -544,8 +629,6 @@ non-junction node ${endId}`);
|
|||
begin[endId].properties.length = 1000 * turfLength(begin[endId]);
|
||||
}
|
||||
}
|
||||
|
||||
return navigation;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -561,7 +644,12 @@ export const fetch = async lineRefs => {
|
|||
}, {});
|
||||
|
||||
const { lines, stops, segments } = processRoutes(elementsList, elementsById);
|
||||
const navigation = createNavigationGraph(stops, elementsList, elementsById);
|
||||
const { navigation, navigationReverse } = createNavigationGraph(
|
||||
elementsList, elementsById
|
||||
);
|
||||
|
||||
compressNavigationGraph(stops, navigation, navigationReverse);
|
||||
makeNavigationSegments(navigation, elementsById);
|
||||
|
||||
return { navigation, lines, stops, segments };
|
||||
};
|
||||
|
|
44304
src/tam/network.json
44304
src/tam/network.json
File diff suppressed because it is too large
Load Diff
|
@ -48,6 +48,19 @@ export const isOneWay = obj => (
|
|||
obj.tags.highway === "motorway")
|
||||
);
|
||||
|
||||
/**
|
||||
* Determine if a node is a railway crossing or not.
|
||||
*
|
||||
* See <https://wiki.osm.org/Tag:railway=railway_crossing> for details.
|
||||
* @param {Object} obj OSM node object.
|
||||
* @return {boolean} Whether the node is a railway crossing.
|
||||
*/
|
||||
export const isRailwayCrossing = obj => (
|
||||
obj.type === "node" &&
|
||||
isObject(obj.tags) &&
|
||||
(obj.tags.railway === "railway_crossing")
|
||||
);
|
||||
|
||||
/**
|
||||
* Determine if an OSM object is a public transport line (route master).
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue