Compare commits

...

4 Commits

21 changed files with 2208 additions and 9707 deletions

11007
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,38 +1,34 @@
{ {
"name": "tamview", "name": "tamview",
"version": "0.1.0", "version": "0.1.0",
"description": "", "type": "module",
"main": "index.js",
"scripts": { "scripts": {
"back": "node src/back", "back": "node src/back",
"front:dev": "parcel serve src/front/index.html", "front:dev": "vite src/front",
"front:prod": "npx parcel build src/front/index.html --no-source-maps --no-autoinstall", "front:prod": "vite build src/front",
"lint": "eslint ." "lint": "eslint ."
}, },
"engines": {
"node": ">=10.0.0"
},
"keywords": [],
"author": "Mattéo Delabre",
"license": "MIT",
"dependencies": { "dependencies": {
"@turf/along": "^6.0.1", "@turf/along": "^6.3.0",
"@turf/helpers": "^6.1.4", "@turf/helpers": "^6.3.0",
"@turf/length": "^6.0.2", "@turf/length": "^6.3.0",
"@turf/projection": "^6.0.1", "@turf/projection": "^6.3.0",
"@turf/turf": "^5.1.6", "@turf/turf": "^6.3.0",
"axios": "^0.19.2", "axios": "^0.21.1",
"color": "^3.1.2", "color": "^3.1.3",
"csv-parse": "^4.8.3", "csv-parse": "^4.15.4",
"express": "^4.17.1", "express": "^4.17.1",
"ol": "^6.1.1", "ol": "^6.5.0",
"unzip-stream": "^0.3.0" "unzip-stream": "^0.3.1",
"vue": "^3.0.5"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^6.8.0", "@vitejs/plugin-vue": "^1.2.2",
"eslint-config-eslint": "^6.0.0", "@vue/compiler-sfc": "^3.0.5",
"eslint-plugin-jsdoc": "^30.0.3", "eslint": "^7.26.0",
"eslint-config-eslint": "^7.0.0",
"eslint-plugin-jsdoc": "^34.0.1",
"eslint-plugin-node": "^11.1.0", "eslint-plugin-node": "^11.1.0",
"parcel-bundler": "^1.12.4" "vite": "^2.3.0"
} }
} }

View File

@ -1,10 +1,14 @@
#!/usr/bin/env node #!/usr/bin/env node
const courses = require('../src/tam/courses'); import * as courses from '../src/tam/courses.js';
const network = require('../src/tam/network.json'); import {displayTime} from '../src/util.js';
const {displayTime} = require('../src/util'); import process from 'process';
const process = require('process'); import path from 'path';
const path = require('path'); import { readFile } from 'fs/promises';
const network = JSON.parse(await readFile(
new URL('../src/tam/network.json', import.meta.url)
));
/** /**
* Convert stop ID to human-readable stop name. * Convert stop ID to human-readable stop name.

View File

@ -1,29 +0,0 @@
#!/usr/bin/env node
const network = require('../src/tam/network');
const path = require('path');
const fs = require('fs');
(async () =>
{
const lines = ['1', '2', '3', '4'];
const data = await network.fetch(lines);
fs.writeFileSync(
path.join(__dirname, '../src/tam/network.json'),
JSON.stringify(
data,
(_, value) =>
{
if (value instanceof Set)
{
// Convert sets to arrays for JSON representation
return Array.from(value.values());
}
return value;
},
4
)
);
})();

25
script/update-network.js Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env node
import * as network from '../src/tam/network.js';
import fs from 'fs/promises';
const lines = ['1', '2', '3', '4'];
const data = await network.fetch(lines);
await fs.writeFile(
new URL("../src/tam/network.json", import.meta.url),
JSON.stringify(
data,
(_, value) =>
{
if (value instanceof Set)
{
// Convert sets to arrays for JSON representation
return Array.from(value.values());
}
return value;
},
4
)
);

View File

@ -1,10 +1,10 @@
const express = require("express"); import express from "express";
const courses = require("../tam/courses"); import * as courses from "../tam/courses.js";
const app = express(); const app = express();
const port = 4321; const port = 4321;
app.get("/courses", async(req, res) => { app.get("/courses", async (_, res) => {
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
return res.json(await courses.fetch("realtime")); return res.json(await courses.fetch("realtime"));
}); });

13
src/front/Panel.vue Normal file
View File

@ -0,0 +1,13 @@
<template>
<div>Hello {{ name }}!</div>
</template>
<script>
export default {
data() {
return {
name: "Vue",
};
}
};
</script>

View File

@ -18,8 +18,8 @@
flex-direction: row; flex-direction: row;
} }
#informations { #panel {
flex: 0 0 600px; flex: 0 0 400px;
padding: 20px; padding: 20px;
overflow-y: auto; overflow-y: auto;
} }
@ -31,8 +31,8 @@
</style> </style>
</head> </head>
<body> <body>
<div id="informations"></div> <aside id="panel"></aside>
<div id="map"></div> <div id="map"></div>
<script src="index.js"></script> <script type="module" src="/index.js"></script>
</body> </body>
</html> </html>

View File

@ -1,21 +1,32 @@
// eslint-disable-next-line node/no-extraneous-require import network from "../tam/network.json";
require("regenerator-runtime/runtime"); import * as simulation from "../tam/simulation.js";
import * as map from "./map/index.js";
const network = require("../tam/network.json");
const simulation = require("../tam/simulation");
const map = require("./map/index.js");
// Run courses simulation // Run courses simulation
const coursesSimulation = simulation.start(); const coursesSimulation = simulation.start();
const informations = document.querySelector("#informations");
let courseId = null; let courseId = null;
// Create display panel
const panel = document.querySelector("#panel");
const displayTime = date => [ const displayTime = date => [
date.getHours(), date.getHours(),
date.getMinutes(), date.getMinutes(),
date.getSeconds() date.getSeconds()
].map(number => number.toString().padStart(2, "0")).join(":"); ].map(number => number.toString().padStart(2, "0")).join(":");
const timeToHTML = time => {
const delta = Math.ceil((time - Date.now()) / 1000);
if (delta <= 0) {
return `Imminent`;
} else if (delta < 60) {
return `${delta} s`;
} else {
return `${Math.floor(delta / 60)} min ${delta % 60} s`;
}
};
setInterval(() => { setInterval(() => {
let html = ` let html = `
<dl> <dl>
@ -27,8 +38,6 @@ setInterval(() => {
if (courseId !== null && courseId in coursesSimulation.courses) { if (courseId !== null && courseId in coursesSimulation.courses) {
const course = coursesSimulation.courses[courseId]; const course = coursesSimulation.courses[courseId];
const timeToHTML = time => Math.ceil((time - Date.now()) / 1000);
const stopToHTML = stopId => stopId in network.stops ? const stopToHTML = stopId => stopId in network.stops ?
network.stops[stopId].properties.name : network.stops[stopId].properties.name :
'<em>Arrêt inconnu</em>'; '<em>Arrêt inconnu</em>';
@ -40,6 +49,17 @@ setInterval(() => {
</tr> </tr>
`).join("\n"); `).join("\n");
const state = (
course.traveledDistance === 0 && course.speed === 0
? "stopped" : "moving"
);
let prevPassings = course.prevPassings;
if (state === "moving") {
prevPassings = prevPassings.concat([[course.departureStop, course.departureTime]]);
}
html += ` html += `
<dl> <dl>
<dt>ID</dt> <dt>ID</dt>
@ -52,14 +72,14 @@ setInterval(() => {
<dd>${stopToHTML(course.finalStop)}</dd> <dd>${stopToHTML(course.finalStop)}</dd>
<dt>État</dt> <dt>État</dt>
<dd>${course.state === "moving" <dd>${state === "moving"
? `Entre ${stopToHTML(course.departureStop)} ? `Entre ${stopToHTML(course.departureStop)}
et ${stopToHTML(course.arrivalStop)}` et ${stopToHTML(course.arrivalStop)}`
: `À larrêt ${stopToHTML(course.currentStop)}`}</dd> : `À larrêt ${stopToHTML(course.departureStop)}`}</dd>
${course.state === "moving" ? ` ${state === "moving" ? `
<dt>Arrivée dans</dt> <dt>Arrivée dans</dt>
<dd>${timeToHTML(course.arrivalTime)} s</dd> <dd>${timeToHTML(course.arrivalTime - 10000)}</dd>
<dt>Distance parcourue</dt> <dt>Distance parcourue</dt>
<dd>${Math.ceil(course.traveledDistance)} m</dd> <dd>${Math.ceil(course.traveledDistance)} m</dd>
@ -68,19 +88,19 @@ setInterval(() => {
<dd>${Math.ceil(course.speed * 3600)} km/h</dd> <dd>${Math.ceil(course.speed * 3600)} km/h</dd>
` : ` ` : `
<dt>Départ dans</dt> <dt>Départ dans</dt>
<dd>${timeToHTML(course.departureTime)} s</dd> <dd>${timeToHTML(course.departureTime)}</dd>
`} `}
</dl> </dl>
<h2>Arrêts précédents</h2> <h2>Arrêts précédents</h2>
<table>${passingsToHTML(course.prevPassings)}</table> <table>${passingsToHTML(prevPassings)}</table>
<h2>Arrêts suivants</h2> <h2>Arrêts suivants</h2>
<table>${passingsToHTML(course.nextPassings)}</table> <table>${passingsToHTML(course.nextPassings)}</table>
`; `;
} }
informations.innerHTML = html; panel.innerHTML = html;
}, 1000); }, 1000);
// Create the network and courses map // Create the network and courses map

View File

@ -1,34 +1,24 @@
const colorModule = require("color"); import color from "color";
/** /**
* Turn the main color of a line into a color suitable for using as a border. * Turn the main color of a line into a color suitable for using as a border.
* @param {string} mainColor Original color. * @param {string} mainColor Original color.
* @returns {string} Hexadecimal representation of the border color. * @returns {string} Hexadecimal representation of the border color.
*/ */
const makeBorderColor = mainColor => { export const makeBorderColor = mainColor => {
const hsl = colorModule(mainColor).hsl(); return color(mainColor).darken(0.2).hex();
hsl.color = Math.max(0, hsl.color[2] -= 20);
return hsl.hex();
}; };
exports.makeBorderColor = makeBorderColor;
/** /**
* Turn the main color of a line into a color suitable for using as a border. * Turn the main color of a line into a color suitable for using as a border.
* @param {string} mainColor Original color. * @param {string} mainColor Original color.
* @returns {string} Hexadecimal representation of the border color. * @returns {string} Hexadecimal representation of the border color.
*/ */
const makeCourseColor = mainColor => { export const makeCourseColor = mainColor => {
const hsl = colorModule(mainColor).hsl(); return color(mainColor).lighten(0.2).hex();
hsl.color = Math.max(0, hsl.color[2] += 10);
return hsl.hex();
}; };
exports.makeCourseColor = makeCourseColor; export const sizes = {
const sizes = {
segmentOuter: 8, segmentOuter: 8,
segmentInner: 6, segmentInner: 6,
stopRadius: 6, stopRadius: 6,
@ -38,5 +28,3 @@ const sizes = {
courseBorder: 10, courseBorder: 10,
courseInnerBorder: 7 courseInnerBorder: 7
}; };
exports.sizes = sizes;

View File

@ -1,14 +1,14 @@
require("ol/ol.css"); import "ol/ol.css";
const { Map, View } = require("ol"); import { Map, View } from "ol";
const { getVectorContext } = require("ol/render"); import { getVectorContext } from "ol/render";
const Point = require("ol/geom/Point").default; import Point from "ol/geom/Point";
const proj = require("ol/proj"); import * as proj from "ol/proj";
const { Style, Icon } = require("ol/style"); import { Style, Icon } from "ol/style";
const tilesLayers = require("./tiles"); import * as tilesLayers from "./tiles";
const networkLayers = require("./network"); import * as networkLayers from "./network";
const { sizes, makeBorderColor, makeCourseColor } = require("./common"); import { sizes, makeBorderColor, makeCourseColor } from "./common";
const network = require("../../tam/network.json"); import network from "../../tam/network.json";
const courseStyles = {}; const courseStyles = {};
@ -58,7 +58,7 @@ const getCourseStyle = lineColor => {
return courseStyles[lineColor]; return courseStyles[lineColor];
}; };
const create = (target, coursesSimulation, onClick) => { export const create = (target, coursesSimulation, onClick) => {
const view = new View({ const view = new View({
center: proj.fromLonLat([3.88, 43.605]), center: proj.fromLonLat([3.88, 43.605]),
zoom: 14, zoom: 14,
@ -138,5 +138,3 @@ const create = (target, coursesSimulation, onClick) => {
return map; return map;
}; };
exports.create = create;

View File

@ -1,11 +1,11 @@
const network = require("../../tam/network.json"); import network from "../../tam/network.json";
const { makeBorderColor, sizes } = require("./common"); import { makeBorderColor, sizes } from "./common";
const GeoJSON = require("ol/format/GeoJSON").default; import GeoJSON from "ol/format/GeoJSON";
const VectorLayer = require("ol/layer/Vector").default; import VectorLayer from "ol/layer/Vector";
const VectorSource = require("ol/source/Vector").default; import VectorSource from "ol/source/Vector";
const { Style, Fill, Stroke, Circle } = require("ol/style"); import { Style, Fill, Stroke, Circle } from "ol/style";
const geojsonReader = new GeoJSON({ featureProjection: "EPSG:3857" }); const geojsonReader = new GeoJSON({ featureProjection: "EPSG:3857" });
@ -68,7 +68,7 @@ const lineFeaturesOrder = (feature1, feature2) => {
* Create the list of layers for displaying the transit network. * Create the list of layers for displaying the transit network.
* @returns {Array.<Layer>} List of map layers. * @returns {Array.<Layer>} List of map layers.
*/ */
const getLayers = () => { export const getLayers = () => {
const segmentsSource = new VectorSource(); const segmentsSource = new VectorSource();
const stopsSource = new VectorSource(); const stopsSource = new VectorSource();
@ -128,5 +128,3 @@ const getLayers = () => {
return [segmentsBorderLayer, segmentsInnerLayer, stopsLayer]; return [segmentsBorderLayer, segmentsInnerLayer, stopsLayer];
}; };
exports.getLayers = getLayers;

View File

@ -1,5 +1,5 @@
const TileLayer = require("ol/layer/Tile").default; import TileLayer from "ol/layer/Tile";
const XYZSource = require("ol/source/XYZ").default; import XYZSource from "ol/source/XYZ";
const mapboxToken = "pk.eyJ1IjoibWF0dGVvZGVsYWJyZSIsImEiOiJja2NxaTUyMmUwcmFhMn\ const mapboxToken = "pk.eyJ1IjoibWF0dGVvZGVsYWJyZSIsImEiOiJja2NxaTUyMmUwcmFhMn\
h0NmFsdzQ3emxqIn0.cyxF0h36emIMTk3cc4VqUw"; h0NmFsdzQ3emxqIn0.cyxF0h36emIMTk3cc4VqUw";
@ -8,7 +8,7 @@ h0NmFsdzQ3emxqIn0.cyxF0h36emIMTk3cc4VqUw";
* Create the list of layers for displaying the background map. * Create the list of layers for displaying the background map.
* @returns {Array.<Layer>} List of map layers. * @returns {Array.<Layer>} List of map layers.
*/ */
const getLayers = () => { export const getLayers = () => {
const backgroundSource = new XYZSource({ const backgroundSource = new XYZSource({
url: `https://api.mapbox.com/${[ url: `https://api.mapbox.com/${[
"styles", "v1", "mapbox", "streets-v11", "styles", "v1", "mapbox", "streets-v11",
@ -21,5 +21,3 @@ const getLayers = () => {
source: backgroundSource source: backgroundSource
})]; })];
}; };
exports.getLayers = getLayers;

14
src/front/vite.config.js Normal file
View File

@ -0,0 +1,14 @@
import path from 'path';
const root = path.join(
path.dirname(new URL(import.meta.url).pathname),
"..", ".."
);
export default {
server: {
fsServe: {
root
}
}
};

View File

@ -5,8 +5,12 @@
* from the official endpoints. * from the official endpoints.
*/ */
const tam = require("./sources/tam"); import * as tam from "./sources/tam.js";
const network = require("./network.json"); import { readFile } from 'fs/promises';
const network = JSON.parse(await readFile(
new URL('./network.json', import.meta.url)
));
/** /**
* Information about the course of a vehicle. * Information about the course of a vehicle.
@ -40,12 +44,13 @@ const parseTime = (time, reference) =>
/** /**
* Fetch information about courses in the TaM network. * Fetch information about courses in the TaM network.
* *
* @async
* @param {string} kind Pass 'realtime' to get real-time information, * @param {string} kind Pass 'realtime' to get real-time information,
* or 'theoretical' to get planned courses for the day. * or 'theoretical' to get planned courses for the day.
* @returns {Object.<string,Course>} Mapping from active course IDs to * @returns {Object.<string,Course>} Mapping from active course IDs to
* information about each course. * information about each course.
*/ */
const fetch = async (kind = 'realtime') => { export const fetch = async (kind = 'realtime') => {
const courses = {}; const courses = {};
const passings = ( const passings = (
kind === 'realtime' kind === 'realtime'
@ -122,11 +127,9 @@ const fetch = async (kind = 'realtime') => {
if (course.finalStopId === undefined) { if (course.finalStopId === undefined) {
course.finalStopId = lastPassing[0]; course.finalStopId = lastPassing[0];
} else if (course.finalStopId !== lastPassing[0]) { } else if (course.finalStopId !== lastPassing[0]) {
course.passings.push([course.finalStopId, lastPassing[1] + 30000]); course.passings.push([course.finalStopId, lastPassing[1] + 60000]);
} }
} }
return courses; return courses;
}; };
exports.fetch = fetch;

View File

@ -11,10 +11,10 @@
* the `script/update-network` script. * the `script/update-network` script.
*/ */
const turfHelpers = require("@turf/helpers"); import * as turfHelpers from "@turf/helpers";
const turfLength = require("@turf/length").default; import turfLength from "@turf/length";
const util = require("../util"); import * as util from "../util.js";
const osm = require("./sources/osm"); import * as osm from "./sources/osm.js";
/** /**
* Fetch stops and lines of the network. * Fetch stops and lines of the network.
@ -22,7 +22,7 @@ const osm = require("./sources/osm");
* @returns {{stops: Object, lines: Object, segments: Object}} Set of stops, * @returns {{stops: Object, lines: Object, segments: Object}} Set of stops,
* segments and lines. * segments and lines.
*/ */
const fetch = async lineRefs => { export const fetch = async lineRefs => {
// Retrieve routes, ways and stops from OpenStreetMap // Retrieve routes, ways and stops from OpenStreetMap
const rawData = await osm.runQuery(`[out:json]; const rawData = await osm.runQuery(`[out:json];
@ -250,5 +250,3 @@ different sequence of nodes in two or more lines.`);
return { stops, lines, segments }; return { stops, lines, segments };
}; };
exports.fetch = fetch;

View File

@ -3405,7 +3405,29 @@
] ]
} }
}, },
"43159": { "43161": {
"type": "Feature",
"properties": {
"name": "Cougourlude",
"routes": [
[
"3",
2
]
],
"successors": [
"43163"
]
},
"geometry": {
"type": "Point",
"coordinates": [
3.9139627,
43.5713482
]
}
},
"43163": {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "Lattes Centre", "name": "Lattes Centre",
@ -3425,28 +3447,6 @@
] ]
} }
}, },
"43161": {
"type": "Feature",
"properties": {
"name": "Cougourlude",
"routes": [
[
"3",
2
]
],
"successors": [
"43159"
]
},
"geometry": {
"type": "Point",
"coordinates": [
3.9139627,
43.5713482
]
}
},
"43201": { "43201": {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
@ -22712,7 +22712,7 @@
] ]
} }
}, },
"43161-43159": { "43161-43163": {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"routes": [ "routes": [

View File

@ -1,204 +1,250 @@
const axios = require("axios"); import axios from "axios";
const turfAlong = require("@turf/along").default; import turfAlong from "@turf/along";
const turfProjection = require("@turf/projection"); import turfLength from "@turf/length";
const network = require("./network.json"); import * as turfHelpers from "@turf/helpers";
import * as turfProjection from "@turf/projection";
import network from "./network.json";
const server = "http://localhost:4321"; const server = "http://localhost:4321";
const findRoute = (from, to) => { // Number of milliseconds to stay at each stop
const queue = [[from, []]]; const stopTime = 10000;
while (queue.length) { // Step used to compute the vehicle angle in meters
const [head, path] = queue.shift(); const angleStep = 10;
for (const successor of network.stops[head].properties.successors) { // Maximum speed of a vehicle
if (successor === to) { const maxSpeed = 60 / 3600;
return path.concat([head, successor]);
}
if (!path.includes(successor)) { // Minimum speed of a vehicle
queue.push([successor, path.concat([head])]); const minSpeed = 10 / 3600;
}
}
}
return null; // Normal speed of a vehicle
}; const normSpeed = (2 * maxSpeed + minSpeed) / 3;
/** Simulate the evolution of a vehicle course in the network. */
class Course { class Course {
constructor(data) { constructor(id) {
this.id = data.id; // Unique identifier of this course
this.prevPassings = []; this.id = id;
this.nextPassings = [];
this.state = null;
// Attributes for the `stopped` state // Line on which this vehicle operates
this.currentStop = null; this.line = null;
// Line direction of this course
this.direction = null;
// Stop to which this course is headed
this.finalStop = null;
// Previous stops that this course left (with timestamps)
this.prevPassings = [];
// Next stops that this course will leave (with timestamps)
this.nextPassings = [];
// Stop that this course just left or will leave
this.departureStop = null;
// Time at which the last stop was left or will be left
this.departureTime = 0; this.departureTime = 0;
// Attributes for the `moving` state // Next stop that this course will reach
this.departureStop = null; // (if equal to departureStop, the course has reached its last stop)
this.arrivalStop = null; this.arrivalStop = null;
// Time at which the next stop will be left
this.arrivalTime = 0; this.arrivalTime = 0;
// Segment of points between the current departure and arrival
this.segment = null;
// Number of meters travelled between the two stops
this.traveledDistance = 0; this.traveledDistance = 0;
// Current vehicle speed in meters per millisecond
this.speed = 0; this.speed = 0;
// Current vehicle latitude and longitude
this.position = [0, 0]; this.position = [0, 0];
// Current vehicle bearing
this.angle = 0; this.angle = 0;
this.history = [];
} }
get currentSegment() { /** Retrieve information about the current segment used by the vehicle. */
if (this.state !== "moving") { updateSegment() {
return null; if (this.departureStop === null || this.arrivalStop === null) {
this.segment = null;
return;
} }
return network.segments[`${this.departureStop}-${this.arrivalStop}`]; const name = `${this.departureStop}-${this.arrivalStop}`;
if (name in network.segments) {
this.segment = network.segments[name];
return;
} }
updateData(data) { if (!(this.departureStop in network.stops)) {
console.warn(`Unknown stop: ${this.departureStop}`);
this.segment = null;
return;
}
if (!(this.arrivalStop in network.stops)) {
console.warn(`Unknown stop: ${this.arrivalStop}`);
this.segment = null;
return;
}
this.segment = turfHelpers.lineString([
network.stops[this.departureStop].geometry.coordinates,
network.stops[this.arrivalStop].geometry.coordinates,
]);
this.segment.properties.length = turfLength(this.segment);
}
/** Merge data received from the server. */
receiveData(data) {
this.line = data.line; this.line = data.line;
this.direction = data.direction; this.direction = data.direction;
this.finalStop = data.finalStopId; this.finalStop = data.finalStopId;
this.nextPassings = data.passings;
const passings = Object.assign(
Object.fromEntries(this.nextPassings),
Object.fromEntries(data.passings),
);
// Remove older passings from next passings
for (let [stop, time] of this.prevPassings) {
delete passings[stop];
}
// Update departure time if still announced
if (this.departureStop !== null) {
if (this.departureStop in passings) {
this.departureTime = passings[this.departureStop];
delete passings[this.departureStop];
}
}
// Update arrival time
if (this.arrivalStop !== null) {
if (this.arrivalStop in passings) {
// Use announced time if available
this.arrivalTime = passings[this.arrivalStop];
delete passings[this.arrivalStop];
} else {
// Otherwise, arrive using a normal speed from current position
const segment = this.segment;
const distance = segment.properties.length - this.traveledDistance;
const time = Math.floor(distance / normSpeed);
this.arrivalTime = Date.now() + time;
}
}
this.nextPassings = Object.entries(passings).sort(
([, time1], [, time2]) => time1 - time2
);
}
/** Update the vehicle state. */
update() {
const now = Date.now(); const now = Date.now();
if (this.state === null) { // When initializing, use the first available passing as start
// Initialize the course on the first available segment if (this.departureStop === null) {
const index = this.nextPassings.findIndex( if (this.nextPassings.length > 0) {
([, time]) => time >= now const [stopId, time] = this.nextPassings.shift();
); this.departureStop = stopId;
this.departureTime = time;
if (index === -1) { this.updateSegment();
return false;
}
if (index === 0) {
this.arriveToStop(this.nextPassings[index][0]);
} else {
this.arriveToStop(this.nextPassings[index - 1][0]);
this.moveToStop(...this.nextPassings[index]);
}
} else if (this.state === "moving") {
const index = this.nextPassings.findIndex(
([stop]) => stop === this.arrivalStop
);
if (index === -1 || this.nextPassings[index][1] <= now) {
// Next stop is not announced or in the past,
// move towards it as fast as possible
this.arrivalTime = now;
} else {
// On the right track, update the arrival time
this.arrivalTime = this.nextPassings[index][1];
}
} else {
// (this.state === 'stopped')
// Try moving to the next stop
const index = this.nextPassings.findIndex(
([stop]) => stop === this.currentStop
);
if (index !== -1) {
if (this.nextPassings[index][1] <= now) {
// Current stop is still announced but in the past
if (index + 1 < this.nextPassings.length) {
// Move to next stop
this.moveToStop(...this.nextPassings[index + 1]);
} else {
// No next stop announced, end of course
return false;
}
} else {
// Cannot move yet, departure is in the future
this.departureTime = this.nextPassings[index][1];
}
} else {
// Current stop is not announced, find the first stop
// announced in the future to which is connection is
// possible
let found = false;
for (
let nextIndex = 0;
nextIndex < this.nextPassings.length;
++nextIndex
) {
const [stop, arrivalTime] = this.nextPassings[nextIndex];
if (arrivalTime > now) {
const route = findRoute(this.currentStop, stop);
if (route !== null) {
// Move to the first intermediate stop, guess the
// arrival time based on the final arrival time and
// the relative distance of the stops
const midDistance = network.segments[
`${route[0]}-${route[1]}`
].properties.length;
let totalDistance = midDistance;
for (
let midIndex = 1;
midIndex + 1 < route.length;
++midIndex
) {
totalDistance += network.segments[
`${route[midIndex]}-${route[midIndex + 1]}`
].properties.length;
}
const midTime = now + (arrivalTime - now) *
midDistance / totalDistance;
this.moveToStop(route[1], midTime);
found = true;
break;
}
} }
} }
if (!found) { // …and the second one as the arrival
// No valid next stop available if (this.arrivalStop === null) {
return false; if (this.nextPassings.length > 0) {
} const [stopId, time] = this.nextPassings.shift();
this.arrivalStop = stopId;
this.arrivalTime = time;
this.updateSegment();
} }
} }
if (this.state === "moving") { if (this.segment !== null) {
const segment = this.currentSegment; const segment = this.segment;
const distance = segment.properties.length - this.traveledDistance; const distance = segment.properties.length - this.traveledDistance;
const duration = this.arrivalTime - Date.now(); const duration = this.arrivalTime - stopTime - now;
// Arrive to the next stop
if (distance === 0) {
this.prevPassings.push([this.departureStop, this.departureTime]);
this.departureStop = this.arrivalStop;
this.departureTime = this.arrivalTime;
if (this.nextPassings.length > 0) {
const [stopId, time] = this.nextPassings.shift();
this.arrivalStop = stopId;
this.arrivalTime = time;
} else {
this.arrivalStop = null;
this.arrivalTime = 0;
}
this.traveledDistance = 0;
this.updateSegment();
}
if (this.departureTime > now) {
// Wait for departure
this.speed = 0;
} else {
if (this.traveledDistance === 0 && this.speed === 0) {
// Were late, record the actual departure time
this.departureTime = now;
}
// Update current speed to arrive on time if possible
this.speed = Course.computeSpeed(distance, duration); this.speed = Course.computeSpeed(distance, duration);
} }
}
return true; return true;
} }
tick(time) { /** Integrate the current vehicle speed and update distance. */
if (this.state === "moving") { move(time) {
// Integrate current speed in travelled distance if (this.segment === null) {
this.traveledDistance += this.speed * time;
const segment = this.currentSegment;
if (this.traveledDistance >= segment.properties.length) {
this.arriveToStop(this.arrivalStop);
return; return;
} }
if (this.speed > 0) {
this.traveledDistance = Math.min(
this.traveledDistance + this.speed * time,
this.segment.properties.length,
);
}
// Compute updated position and angle based on a small step // Compute updated position and angle based on a small step
const step = 10; // In meters let positionBehind;
let positionInFront;
if (this.traveledDistance < angleStep / 2) {
positionBehind = this.traveledDistance;
positionInFront = angleStep;
} else {
positionBehind = this.traveledDistance - angleStep / 2;
positionInFront = this.traveledDistance + angleStep / 2;
}
const positions = [ const positions = [
Math.max(0, this.traveledDistance - step / 2), positionBehind,
this.traveledDistance, this.traveledDistance,
this.traveledDistance + step / 2 positionInFront,
].map(distance => turfProjection.toMercator(turfAlong( ].map(distance => turfProjection.toMercator(turfAlong(
segment, this.segment,
distance / 1000 distance / 1000
)).geometry.coordinates); )).geometry.coordinates);
@ -209,69 +255,21 @@ class Course {
this.position = positions[1]; this.position = positions[1];
} }
}
/**
* Transition this course to a state where it has arrived to a stop.
* @param {string} stop Identifier for the stop to which
* the course arrives.
* @returns {undefined}
*/
arriveToStop(stop) {
this.state = "stopped";
this.currentStop = stop;
this.departureTime = Date.now();
this.prevPassings.push([stop, Date.now()]);
this.position = (
turfProjection.toMercator(network.stops[stop])
.geometry.coordinates
);
}
/**
* Transition this course to a state where it is moving to a stop.
* @param {string} stop Next stop for this course.
* @param {number} arrivalTime Planned arrival time to that stop.
* @returns {undefined}
*/
moveToStop(stop, arrivalTime) {
const segmentId = `${this.currentStop}-${stop}`;
if (!(segmentId in network.segments)) {
console.warn(`Course ${this.id} cannot go from stop
${this.currentStop} to stop ${stop}. Teleporting to ${stop}`);
this.arriveToStop(stop);
return;
}
const distance = network.segments[segmentId].properties.length;
const duration = arrivalTime - Date.now();
if (Course.computeSpeed(distance, duration) === 0) {
// Speed would be too low, better wait for some time
return;
}
this.state = "moving";
this.departureStop = this.currentStop;
this.arrivalStop = stop;
this.arrivalTime = arrivalTime;
this.traveledDistance = 0;
this.speed = 0;
}
static computeSpeed(distance, duration) { static computeSpeed(distance, duration) {
if (duration <= 0) { if (duration <= 0) {
// Late: go to maximum speed // Late: go to maximum speed
return 50 / 3600; return maxSpeed;
} }
if (distance / duration <= 10 / 3600) { const speed = distance / duration;
if (speed < minSpeed) {
// Too slow: pause until speed is sufficient // Too slow: pause until speed is sufficient
return 0; return 0;
} }
return distance / duration; return Math.min(maxSpeed, speed);
} }
} }
@ -281,33 +279,23 @@ const updateData = async courses => {
// Update or create new courses // Update or create new courses
for (const [id, data] of Object.entries(dataset)) { for (const [id, data] of Object.entries(dataset)) {
if (id in courses) { if (id in courses) {
if (!courses[id].updateData(data)) { courses[id].receiveData(data);
delete courses[id];
}
} else { } else {
const newCourse = new Course(data); const newCourse = new Course(data.id);
newCourse.receiveData(data);
if (newCourse.updateData(data)) {
courses[id] = newCourse; courses[id] = newCourse;
} }
} }
}
// Remove stale courses
for (const id of Object.keys(courses)) {
if (!(id in dataset)) {
delete courses[id];
}
}
}; };
const tick = (courses, time) => { const tick = (courses, time) => {
for (const course of Object.values(courses)) { for (const course of Object.values(courses)) {
course.tick(time); course.update();
course.move(time);
} }
}; };
const start = () => { export const start = () => {
const courses = {}; const courses = {};
let lastFrame = null; let lastFrame = null;
let lastUpdate = null; let lastUpdate = null;
@ -326,7 +314,7 @@ const start = () => {
tick(courses, time); tick(courses, time);
}; };
window.__courses = courses;
return { courses, update }; return { courses, update };
}; };
exports.start = start;

View File

@ -4,8 +4,8 @@
* Interface with the OpenStreetMap collaborative mapping database. * Interface with the OpenStreetMap collaborative mapping database.
*/ */
const axios = require("axios"); import axios from "axios";
const { isObject } = require("../../util"); import { isObject } from "../../util.js";
/** /**
* Submit a query to an Overpass endpoint. * Submit a query to an Overpass endpoint.
@ -19,7 +19,7 @@ const { isObject } = require("../../util");
* is requested in the query, the result will automatically be parsed into * is requested in the query, the result will automatically be parsed into
* a JS object. * a JS object.
*/ */
const runQuery = ( export const runQuery = (
query, query,
endpoint = "https://lz4.overpass-api.de/api/interpreter" endpoint = "https://lz4.overpass-api.de/api/interpreter"
) => ( ) => (
@ -27,16 +27,12 @@ const runQuery = (
.then(res => res.data) .then(res => res.data)
); );
exports.runQuery = runQuery;
/** /**
* Create a link to view a node. * Create a link to view a node.
* @param {string|number} id Identifier for the node to view. * @param {string|number} id Identifier for the node to view.
* @returns {string} Link to view this node on the OSM website. * @returns {string} Link to view this node on the OSM website.
*/ */
const viewNode = id => `https://www.osm.org/node/${id}`; export const viewNode = id => `https://www.osm.org/node/${id}`;
exports.viewNode = viewNode;
/** /**
* Determine if an OSM way is one-way or not. * Determine if an OSM way is one-way or not.
@ -45,15 +41,13 @@ exports.viewNode = viewNode;
* @param {Object} obj OSM way object. * @param {Object} obj OSM way object.
* @returns {boolean} Whether the way is one-way. * @returns {boolean} Whether the way is one-way.
*/ */
const isOneWay = obj => ( export const isOneWay = obj => (
obj.type === "way" && obj.type === "way" &&
isObject(obj.tags) && isObject(obj.tags) &&
(obj.tags.oneway === "yes" || obj.tags.junction === "roundabout" || (obj.tags.oneway === "yes" || obj.tags.junction === "roundabout" ||
obj.tags.highway === "motorway") obj.tags.highway === "motorway")
); );
exports.isOneWay = isOneWay;
/** /**
* Determine if an OSM object is a public transport line (route master). * Determine if an OSM object is a public transport line (route master).
* *
@ -62,10 +56,8 @@ exports.isOneWay = isOneWay;
* @param {Object} obj OSM relation object. * @param {Object} obj OSM relation object.
* @returns {boolean} Whether the relation is a public transport line. * @returns {boolean} Whether the relation is a public transport line.
*/ */
const isTransportLine = obj => ( export const isTransportLine = obj => (
obj.type === "relation" && obj.type === "relation" &&
isObject(obj.tags) && isObject(obj.tags) &&
obj.tags.type === "route_master" obj.tags.type === "route_master"
); );
exports.isTransportLine = isTransportLine;

View File

@ -1,8 +1,8 @@
const csv = require("csv-parse"); import csv from "csv-parse";
const axios = require("axios"); import axios from "axios";
const path = require("path"); import path from "path";
const fs = require("fs").promises; import fs from "fs/promises";
const { snakeToCamelCase, unzipFile } = require("../../util"); import { snakeToCamelCase, unzipFile } from "../../util.js";
/** /**
* Data available for each passing of a vehicle at a station. * Data available for each passing of a vehicle at a station.
@ -70,14 +70,14 @@ const makeCached = (func, cachePath) => {
yield passing; yield passing;
} }
fs.writeFile(cachePath, JSON.stringify(newCache)); await fs.writeFile(cachePath, JSON.stringify(newCache));
}; };
}; };
const cacheDir = path.join(__dirname, "..", "..", "..", "cache"); const cacheDir = new URL("../../../cache/", import.meta.url);
const realtimeEndpoint = "http://data.montpellier3m.fr/node/10732/download"; const realtimeEndpoint = "http://data.montpellier3m.fr/node/10732/download";
const realtimeCachePath = path.join(cacheDir, "realtime.json"); const realtimeCachePath = new URL("./realtime.json", cacheDir);
/** /**
* Fetch real time passings of vehicles across the network. * Fetch real time passings of vehicles across the network.
@ -86,7 +86,7 @@ const realtimeCachePath = path.join(cacheDir, "realtime.json");
* update of this information. Next values are informations about each vehicle * update of this information. Next values are informations about each vehicle
* passing. * passing.
*/ */
const fetchRealtime = async function *() { const fetchRealtimeRaw = async function *() {
const res = await axios.get(realtimeEndpoint, { const res = await axios.get(realtimeEndpoint, {
responseType: "stream" responseType: "stream"
}); });
@ -106,10 +106,10 @@ const fetchRealtime = async function *() {
} }
}; };
exports.fetchRealtime = makeCached(fetchRealtime, realtimeCachePath); export const fetchRealtime = makeCached(fetchRealtimeRaw, realtimeCachePath);
const theoreticalEndpoint = "http://data.montpellier3m.fr/node/10731/download"; const theoreticalEndpoint = "http://data.montpellier3m.fr/node/10731/download";
const theoreticalCachePath = path.join(cacheDir, "theoretical.json"); const theoreticalCachePath = new URL("./theoretical.json", cacheDir);
/** /**
* Fetch theoretical passings for the current day across the network. * Fetch theoretical passings for the current day across the network.
@ -118,7 +118,7 @@ const theoreticalCachePath = path.join(cacheDir, "theoretical.json");
* update of this information. Next values are informations about each vehicle * update of this information. Next values are informations about each vehicle
* passing. * passing.
*/ */
const fetchTheoretical = async function *() { const fetchTheoreticalRaw = async function *() {
const res = await axios.get(theoreticalEndpoint, { const res = await axios.get(theoreticalEndpoint, {
responseType: "stream" responseType: "stream"
}); });
@ -154,4 +154,5 @@ const fetchTheoretical = async function *() {
} }
}; };
exports.fetchTheoretical = makeCached(fetchTheoretical, theoreticalCachePath); export const fetchTheoretical = makeCached(
fetchTheoreticalRaw, theoreticalCachePath);

View File

@ -1,23 +1,19 @@
const unzip = require("unzip-stream"); import unzip from "unzip-stream";
/** /**
* Convert a snake-cased string to a camel-cased one. * Convert a snake-cased string to a camel-cased one.
* @param {string} str Original string. * @param {string} str Original string.
* @returns {string} Transformed string. * @returns {string} Transformed string.
*/ */
const snakeToCamelCase = str => str.replace(/([-_][a-z])/gu, group => export const snakeToCamelCase = str => str.replace(/([-_][a-z])/gu, group =>
group.toUpperCase().replace("-", "").replace("_", "")); group.toUpperCase().replace("-", "").replace("_", ""));
exports.snakeToCamelCase = snakeToCamelCase;
/** /**
* Check if a value is a JS object. * Check if a value is a JS object.
* @param {*} value Value to check. * @param {*} value Value to check.
* @returns {boolean} Whether `value` is a JS object. * @returns {boolean} Whether `value` is a JS object.
*/ */
const isObject = value => value !== null && typeof value === "object"; export const isObject = value => value !== null && typeof value === "object";
exports.isObject = isObject;
/** /**
* Check if two arrays are equal in a shallow manner. * Check if two arrays are equal in a shallow manner.
@ -25,20 +21,18 @@ exports.isObject = isObject;
* @param {Array} array2 Second array. * @param {Array} array2 Second array.
* @returns {boolean} Whether the two arrays are equal. * @returns {boolean} Whether the two arrays are equal.
*/ */
const arraysEqual = (array1, array2) => ( export const arraysEqual = (array1, array2) => (
array1.length === array2.length && array1.length === array2.length &&
array1.every((elt1, index) => elt1 === array2[index]) array1.every((elt1, index) => elt1 === array2[index])
); );
exports.arraysEqual = arraysEqual;
/** /**
* Find a file in a zipped stream and unzip it. * Find a file in a zipped stream and unzip it.
* @param {stream.Readable} data Input zipped stream. * @param {stream.Readable} data Input zipped stream.
* @param {string} fileName Name of the file to find. * @param {string} fileName Name of the file to find.
* @returns {Promise.<stream.Readable>} Stream of the unzipped file. * @returns {Promise.<stream.Readable>} Stream of the unzipped file.
*/ */
const unzipFile = (data, fileName) => new Promise((res, rej) => { export const unzipFile = (data, fileName) => new Promise((res, rej) => {
// eslint-disable-next-line new-cap // eslint-disable-next-line new-cap
const stream = data.pipe(unzip.Parse()); const stream = data.pipe(unzip.Parse());
let found = false; let found = false;
@ -62,12 +56,13 @@ const unzipFile = (data, fileName) => new Promise((res, rej) => {
stream.on("error", err => rej(err)); stream.on("error", err => rej(err));
}); });
exports.unzipFile = unzipFile; /**
* Display the time component of a date.
const displayTime = date => [ * @param {Date} date Date to display.
* @returns {string} Formatted date as 'HH:MM:SS'.
*/
export const displayTime = date => [
date.getHours(), date.getHours(),
date.getMinutes(), date.getMinutes(),
date.getSeconds() date.getSeconds()
].map(number => number.toString().padStart(2, "0")).join(":"); ].map(number => number.toString().padStart(2, "0")).join(":");
exports.displayTime = displayTime;