2020-07-25 16:05:43 +00:00
|
|
|
|
require("ol/ol.css");
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const { Map, View } = require("ol");
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const GeoJSON = require("ol/format/GeoJSON").default;
|
|
|
|
|
const reader = new GeoJSON({ featureProjection: "EPSG:3857" });
|
2020-07-25 14:28:51 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const TileLayer = require("ol/layer/Tile").default;
|
|
|
|
|
const XYZSource = require("ol/source/XYZ").default;
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const VectorLayer = require("ol/layer/Vector").default;
|
|
|
|
|
const VectorSource = require("ol/source/Vector").default;
|
|
|
|
|
const { getVectorContext } = require("ol/render");
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const Point = require("ol/geom/Point").default;
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const proj = require("ol/proj");
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const { Style, Fill, Stroke, Circle, Icon } = require("ol/style");
|
|
|
|
|
const colorModule = require("color");
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const mapboxToken = "pk.eyJ1IjoibWF0dGVvZGVsYWJyZSIsImEiOiJja2NxaTUyMmUwcmFhMn\
|
|
|
|
|
h0NmFsdzQ3emxqIn0.cyxF0h36emIMTk3cc4VqUw";
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const simulation = require("../tam/simulation");
|
|
|
|
|
const network = require("../tam/network.json");
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const lineFeaturesOrder = (feature1, feature2) => {
|
|
|
|
|
const lines1 = feature1.get("lines");
|
2020-07-25 11:46:45 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (lines1.length === 0) {
|
2020-07-25 11:46:45 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const lines2 = feature2.get("lines");
|
2020-07-25 11:46:45 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (lines2.length === 0) {
|
2020-07-25 11:46:45 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Math.min(...lines1) - Math.min(...lines2);
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const makeDataSources = () => {
|
2020-07-17 17:59:34 +00:00
|
|
|
|
const segmentsSource = new VectorSource();
|
|
|
|
|
const stopsSource = new VectorSource();
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const readFeatures = hash => Object.values(hash).map(json => {
|
2020-07-25 14:28:51 +00:00
|
|
|
|
json.properties.lines = json.properties.routes.filter(
|
2020-07-25 16:05:43 +00:00
|
|
|
|
|
2020-07-25 14:28:51 +00:00
|
|
|
|
// Only consider normal routes (excluding alternate routes)
|
|
|
|
|
([lineRef, routeRef]) =>
|
2020-07-25 16:05:43 +00:00
|
|
|
|
network.lines[lineRef].routes[routeRef].state === "normal"
|
2020-07-25 14:28:51 +00:00
|
|
|
|
).map(([lineRef]) => lineRef);
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (json.properties.lines.length >= 1) {
|
2020-07-25 14:28:51 +00:00
|
|
|
|
json.properties.colors = json.properties.lines.map(
|
2020-07-25 16:05:43 +00:00
|
|
|
|
lineRef => network.lines[lineRef].color
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
json.properties.colors = ["#FFFFFF"];
|
2020-07-25 14:28:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reader.readFeature(json);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
segmentsSource.addFeatures(readFeatures(network.segments));
|
|
|
|
|
stopsSource.addFeatures(readFeatures(network.stops));
|
2020-07-25 16:05:43 +00:00
|
|
|
|
return { segmentsSource, stopsSource };
|
2020-07-17 17:59:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const makeBorderColor = mainColor => {
|
|
|
|
|
const hsl = colorModule(mainColor).hsl();
|
|
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
|
hsl.color = Math.max(0, hsl.color[2] -= 20);
|
|
|
|
|
return hsl.hex();
|
2020-07-17 17:17:06 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const makeCourseColor = mainColor => {
|
|
|
|
|
const hsl = colorModule(mainColor).hsl();
|
|
|
|
|
|
2020-07-23 21:09:05 +00:00
|
|
|
|
hsl.color = Math.max(0, hsl.color[2] += 10);
|
|
|
|
|
return hsl.hex();
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-23 23:32:39 +00:00
|
|
|
|
const sizes = {
|
|
|
|
|
segmentOuter: 8,
|
|
|
|
|
segmentInner: 6,
|
|
|
|
|
stopRadius: 6,
|
|
|
|
|
stopBorder: 1.5,
|
|
|
|
|
courseSize: 15,
|
|
|
|
|
courseOuterBorder: 13,
|
|
|
|
|
courseBorder: 10,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
courseInnerBorder: 7
|
2020-07-23 23:32:39 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-07-23 18:49:01 +00:00
|
|
|
|
const segmentBorderStyle = feature => new Style({
|
2020-07-17 17:59:34 +00:00
|
|
|
|
stroke: new Stroke({
|
2020-07-25 16:05:43 +00:00
|
|
|
|
color: makeBorderColor(feature.get("colors")[0]),
|
|
|
|
|
width: sizes.segmentOuter
|
|
|
|
|
})
|
2020-07-17 17:59:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-23 18:49:01 +00:00
|
|
|
|
const segmentInnerStyle = feature => new Style({
|
2020-07-17 17:59:34 +00:00
|
|
|
|
stroke: new Stroke({
|
2020-07-25 16:05:43 +00:00
|
|
|
|
color: feature.get("colors")[0],
|
|
|
|
|
width: sizes.segmentInner
|
|
|
|
|
})
|
2020-07-17 17:59:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-23 18:49:01 +00:00
|
|
|
|
const stopStyle = feature => new Style({
|
2020-07-17 17:59:34 +00:00
|
|
|
|
image: new Circle({
|
|
|
|
|
fill: new Fill({
|
2020-07-25 16:05:43 +00:00
|
|
|
|
color: feature.get("colors")[0]
|
2020-07-17 17:59:34 +00:00
|
|
|
|
}),
|
|
|
|
|
stroke: new Stroke({
|
2020-07-25 16:05:43 +00:00
|
|
|
|
color: makeBorderColor(feature.get("colors")[0]),
|
|
|
|
|
width: sizes.stopBorder
|
2020-07-17 17:59:34 +00:00
|
|
|
|
}),
|
2020-07-25 16:05:43 +00:00
|
|
|
|
radius: sizes.stopRadius
|
|
|
|
|
})
|
2020-07-17 17:59:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-23 21:09:05 +00:00
|
|
|
|
const courseStyles = {};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const getCourseStyle = lineColor => {
|
|
|
|
|
if (!(lineColor in courseStyles)) {
|
|
|
|
|
const icon = window.document.createElement("canvas");
|
2020-07-23 21:09:05 +00:00
|
|
|
|
|
2020-07-23 23:32:39 +00:00
|
|
|
|
const shapeSize = sizes.courseSize;
|
|
|
|
|
const iconSize = sizes.courseSize + sizes.courseOuterBorder;
|
2020-07-23 21:09:05 +00:00
|
|
|
|
|
|
|
|
|
icon.width = iconSize;
|
2020-07-23 23:32:39 +00:00
|
|
|
|
icon.height = iconSize;
|
2020-07-23 21:09:05 +00:00
|
|
|
|
|
|
|
|
|
const cx = icon.width / 2;
|
|
|
|
|
const cy = icon.height / 2;
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const iconCtx = icon.getContext("2d");
|
2020-07-23 21:09:05 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const [color, size] of [
|
|
|
|
|
[makeBorderColor(lineColor), sizes.courseOuterBorder],
|
|
|
|
|
[lineColor, sizes.courseBorder],
|
|
|
|
|
[makeCourseColor(lineColor), sizes.courseInnerBorder]
|
|
|
|
|
]) {
|
2020-07-23 21:09:05 +00:00
|
|
|
|
iconCtx.fillStyle = color;
|
|
|
|
|
iconCtx.strokeStyle = color;
|
|
|
|
|
iconCtx.lineWidth = size;
|
2020-07-25 16:05:43 +00:00
|
|
|
|
iconCtx.lineJoin = "round";
|
2020-07-23 21:09:05 +00:00
|
|
|
|
iconCtx.miterLimit = 200000;
|
|
|
|
|
|
|
|
|
|
iconCtx.beginPath();
|
|
|
|
|
iconCtx.moveTo(cx - 0.5 * shapeSize, cy - 0.3 * shapeSize);
|
|
|
|
|
iconCtx.lineTo(cx + 0.5 * shapeSize, cy);
|
|
|
|
|
iconCtx.lineTo(cx - 0.5 * shapeSize, cy + 0.3 * shapeSize);
|
|
|
|
|
iconCtx.closePath();
|
|
|
|
|
iconCtx.stroke();
|
|
|
|
|
iconCtx.fill();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
courseStyles[lineColor] = new Style({
|
2020-07-23 21:09:05 +00:00
|
|
|
|
image: new Icon({
|
|
|
|
|
img: icon,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
imgSize: [icon.width, icon.height]
|
|
|
|
|
})
|
2020-07-23 21:09:05 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
return courseStyles[lineColor];
|
2020-07-23 21:09:05 +00:00
|
|
|
|
};
|
2020-07-23 18:49:01 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const createMap = target => {
|
|
|
|
|
|
2020-07-17 17:17:06 +00:00
|
|
|
|
// Map background
|
|
|
|
|
const backgroundSource = new XYZSource({
|
2020-07-25 16:05:43 +00:00
|
|
|
|
url: `https://api.mapbox.com/${[
|
|
|
|
|
"styles", "v1", "mapbox", "streets-v11",
|
|
|
|
|
"tiles", "512", "{z}", "{x}", "{y}"
|
|
|
|
|
].join("/")}?access_token=${mapboxToken}`,
|
|
|
|
|
tileSize: [512, 512]
|
2020-07-17 17:17:06 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const backgroundLayer = new TileLayer({
|
2020-07-25 16:05:43 +00:00
|
|
|
|
source: backgroundSource
|
2020-07-17 17:17:06 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-23 15:29:35 +00:00
|
|
|
|
// Static data overlay
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const { segmentsSource, stopsSource } = makeDataSources();
|
2020-07-17 17:59:34 +00:00
|
|
|
|
|
|
|
|
|
const segmentsBorderLayer = new VectorLayer({
|
|
|
|
|
source: segmentsSource,
|
2020-07-25 11:46:45 +00:00
|
|
|
|
renderOrder: lineFeaturesOrder,
|
2020-07-23 18:49:01 +00:00
|
|
|
|
style: segmentBorderStyle,
|
2020-07-17 17:59:34 +00:00
|
|
|
|
|
|
|
|
|
updateWhileInteracting: true,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
updateWhileAnimating: true
|
2020-07-17 17:59:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const segmentsInnerLayer = new VectorLayer({
|
|
|
|
|
source: segmentsSource,
|
2020-07-25 11:46:45 +00:00
|
|
|
|
renderOrder: lineFeaturesOrder,
|
2020-07-23 18:49:01 +00:00
|
|
|
|
style: segmentInnerStyle,
|
2020-07-17 17:59:34 +00:00
|
|
|
|
|
|
|
|
|
updateWhileInteracting: true,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
updateWhileAnimating: true
|
2020-07-17 17:59:34 +00:00
|
|
|
|
});
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
|
const stopsLayer = new VectorLayer({
|
|
|
|
|
source: stopsSource,
|
2020-07-25 11:46:45 +00:00
|
|
|
|
renderOrder: lineFeaturesOrder,
|
2020-07-23 18:49:01 +00:00
|
|
|
|
style: stopStyle,
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
|
minZoom: 13,
|
2020-07-17 17:17:06 +00:00
|
|
|
|
updateWhileInteracting: true,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
updateWhileAnimating: true
|
2020-07-17 17:17:06 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-23 18:49:01 +00:00
|
|
|
|
// Setup map
|
|
|
|
|
const view = new View({
|
|
|
|
|
center: proj.fromLonLat([3.88, 43.605]),
|
2020-07-23 21:09:05 +00:00
|
|
|
|
zoom: 14,
|
2020-07-23 18:49:01 +00:00
|
|
|
|
maxZoom: 22,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
constrainResolution: true
|
2020-07-23 15:29:35 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-17 17:17:06 +00:00
|
|
|
|
const map = new Map({
|
|
|
|
|
target,
|
|
|
|
|
layers: [
|
|
|
|
|
backgroundLayer,
|
2020-07-17 17:59:34 +00:00
|
|
|
|
segmentsBorderLayer,
|
|
|
|
|
segmentsInnerLayer,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
stopsLayer
|
2020-07-17 17:17:06 +00:00
|
|
|
|
],
|
2020-07-25 16:05:43 +00:00
|
|
|
|
view
|
2020-07-23 18:49:01 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-23 23:32:39 +00:00
|
|
|
|
// Run courses simulation
|
2020-07-23 18:49:01 +00:00
|
|
|
|
const simulInstance = simulation.start();
|
|
|
|
|
|
2020-07-23 23:32:39 +00:00
|
|
|
|
// Course on which the view is currently focused
|
|
|
|
|
let focusedCourse = null;
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const startFocus = courseId => {
|
|
|
|
|
if (courseId in simulInstance.courses) {
|
2020-07-23 23:32:39 +00:00
|
|
|
|
const course = simulInstance.courses[courseId];
|
2020-07-25 16:05:43 +00:00
|
|
|
|
|
2020-07-23 23:32:39 +00:00
|
|
|
|
view.animate({
|
|
|
|
|
center: course.position,
|
2020-07-25 16:05:43 +00:00
|
|
|
|
duration: 500
|
|
|
|
|
}, () => {
|
|
|
|
|
focusedCourse = courseId;
|
|
|
|
|
});
|
2020-07-23 23:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const stopFocus = () => {
|
2020-07-23 23:32:39 +00:00
|
|
|
|
focusedCourse = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Draw courses directly on the map
|
2020-07-25 16:05:43 +00:00
|
|
|
|
map.on("postcompose", ev => {
|
2020-07-23 18:49:01 +00:00
|
|
|
|
simulInstance.update();
|
|
|
|
|
|
|
|
|
|
// The normal way to access a layer’s vector context is through the
|
|
|
|
|
// `postrender` event of that layer. However, `postrender` is not
|
|
|
|
|
// triggered when no feature of the layer is inside the current
|
|
|
|
|
// bounding box, but we want to draw vehicles in between stops even
|
|
|
|
|
// if no stop is visible. This hack listens to the global `postcompose`
|
|
|
|
|
// event, which is always triggered at every frame, and reconstructs
|
|
|
|
|
// the stops layer’s vector context from internal variables
|
2020-07-25 16:05:43 +00:00
|
|
|
|
/* eslint-disable no-underscore-dangle */
|
|
|
|
|
if (stopsLayer.renderer_) {
|
2020-07-23 18:49:01 +00:00
|
|
|
|
ev.context = stopsLayer.renderer_.context;
|
2020-07-25 16:05:43 +00:00
|
|
|
|
ev.inversePixelTransform =
|
|
|
|
|
stopsLayer.renderer_.inversePixelTransform;
|
|
|
|
|
/* eslint-enable no-underscore-dangle */
|
2020-07-23 18:49:01 +00:00
|
|
|
|
|
|
|
|
|
const ctx = getVectorContext(ev);
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const course of Object.values(simulInstance.courses)) {
|
2020-07-23 21:09:05 +00:00
|
|
|
|
const color = network.lines[course.line].color;
|
|
|
|
|
const style = getCourseStyle(color);
|
|
|
|
|
|
2020-07-23 22:18:30 +00:00
|
|
|
|
style.getImage().setRotation(course.angle);
|
2020-07-23 21:09:05 +00:00
|
|
|
|
ctx.setStyle(style);
|
|
|
|
|
|
2020-07-23 18:49:01 +00:00
|
|
|
|
const point = new Point(course.position);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
|
2020-07-23 18:49:01 +00:00
|
|
|
|
ctx.drawGeometry(point);
|
2020-07-23 23:32:39 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (course.id === focusedCourse) {
|
2020-07-23 23:32:39 +00:00
|
|
|
|
view.setCenter(course.position);
|
|
|
|
|
}
|
2020-07-23 18:49:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
map.render();
|
2020-07-17 17:17:06 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-23 18:49:01 +00:00
|
|
|
|
map.render();
|
2020-07-23 23:32:39 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
map.on("singleclick", ev => {
|
2020-07-23 23:32:39 +00:00
|
|
|
|
const mousePixel = map.getPixelFromCoordinate(ev.coordinate);
|
|
|
|
|
const maxDistance = sizes.courseSize + sizes.courseOuterBorder;
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const course of Object.values(simulInstance.courses)) {
|
2020-07-23 23:32:39 +00:00
|
|
|
|
const coursePixel = map.getPixelFromCoordinate(course.position);
|
|
|
|
|
const dx = mousePixel[0] - coursePixel[0];
|
|
|
|
|
const dy = mousePixel[1] - coursePixel[1];
|
|
|
|
|
const distance = dx * dx + dy * dy;
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (distance <= maxDistance * maxDistance) {
|
2020-07-23 23:32:39 +00:00
|
|
|
|
startFocus(course.id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clicking anywhere else resets focus
|
|
|
|
|
stopFocus();
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-17 17:17:06 +00:00
|
|
|
|
return map;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.createMap = createMap;
|