2020-07-17 17:17:06 +00:00
|
|
|
require('ol/ol.css');
|
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
const axios = require('axios');
|
2020-07-17 17:17:06 +00:00
|
|
|
const {Map, View} = require('ol');
|
|
|
|
|
|
|
|
const TileLayer = require('ol/layer/Tile').default;
|
|
|
|
const XYZSource = require('ol/source/XYZ').default;
|
|
|
|
|
|
|
|
const VectorLayer = require('ol/layer/Vector').default;
|
|
|
|
const VectorSource = require('ol/source/Vector').default;
|
|
|
|
|
|
|
|
const Feature = require('ol/Feature').default;
|
|
|
|
const Point = require('ol/geom/Point').default;
|
|
|
|
const LineString = require('ol/geom/LineString').default;
|
|
|
|
|
|
|
|
const proj = require('ol/proj');
|
|
|
|
|
|
|
|
const {Style, Fill, Stroke, Circle} = require('ol/style');
|
|
|
|
const color = require('color');
|
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
const mapboxToken = `pk.eyJ1IjoibWF0dGVvZGVsYWJyZSIsImEiOiJja2NxaTUyMmUwcmFhMn\
|
|
|
|
h0NmFsdzQ3emxqIn0.cyxF0h36emIMTk3cc4VqUw`;
|
2020-07-17 17:17:06 +00:00
|
|
|
|
2020-07-17 18:10:52 +00:00
|
|
|
const network = require('../back/data/network.json');
|
2020-07-17 17:17:06 +00:00
|
|
|
|
2020-07-17 18:10:52 +00:00
|
|
|
const makeDataSources = async () =>
|
2020-07-17 17:17:06 +00:00
|
|
|
{
|
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-17 17:59:34 +00:00
|
|
|
segmentsSource.addFeatures(
|
2020-07-17 21:48:32 +00:00
|
|
|
Object.values(network.segments).map(({lines, points}) =>
|
|
|
|
new Feature({
|
|
|
|
colors: lines.map(line => network.lines[line].color),
|
|
|
|
geometry: new LineString(points.map(
|
|
|
|
({lat, lon}) => proj.fromLonLat([lon, lat])
|
|
|
|
)),
|
|
|
|
})
|
2020-07-17 17:59:34 +00:00
|
|
|
)
|
|
|
|
);
|
2020-07-17 17:17:06 +00:00
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
stopsSource.addFeatures(
|
2020-07-17 21:48:32 +00:00
|
|
|
Object.values(network.stops).map(({lines, lon, lat}) =>
|
2020-07-17 17:59:34 +00:00
|
|
|
new Feature({
|
2020-07-17 21:48:32 +00:00
|
|
|
colors: lines.map(line => network.lines[line].color),
|
|
|
|
geometry: new Point(proj.fromLonLat([lon, lat])),
|
2020-07-17 17:59:34 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2020-07-17 17:17:06 +00:00
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
return {segmentsSource, stopsSource};
|
|
|
|
};
|
|
|
|
|
|
|
|
const makeBorderColor = mainColor =>
|
|
|
|
{
|
|
|
|
const hsl = color(mainColor).hsl();
|
|
|
|
hsl.color = Math.max(0, hsl.color[2] -= 20);
|
|
|
|
return hsl.hex();
|
2020-07-17 17:17:06 +00:00
|
|
|
};
|
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
const segmentsBorderStyle = feature => new Style({
|
|
|
|
stroke: new Stroke({
|
2020-07-17 21:48:32 +00:00
|
|
|
color: makeBorderColor(feature.get('colors')[0]),
|
2020-07-17 17:59:34 +00:00
|
|
|
width: 8,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
const segmentsInnerStyle = feature => new Style({
|
|
|
|
stroke: new Stroke({
|
2020-07-17 21:48:32 +00:00
|
|
|
color: feature.get('colors')[0],
|
2020-07-17 17:59:34 +00:00
|
|
|
width: 6,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
const stopsStyle = feature => new Style({
|
|
|
|
image: new Circle({
|
|
|
|
fill: new Fill({
|
2020-07-17 21:48:32 +00:00
|
|
|
color: feature.get('colors')[0],
|
2020-07-17 17:59:34 +00:00
|
|
|
}),
|
|
|
|
stroke: new Stroke({
|
2020-07-17 21:48:32 +00:00
|
|
|
color: makeBorderColor(feature.get('colors')[0]),
|
2020-07-17 17:59:34 +00:00
|
|
|
width: 1.5,
|
|
|
|
}),
|
|
|
|
radius: 6,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2020-07-17 17:17:06 +00:00
|
|
|
const createMap = async (target) =>
|
|
|
|
{
|
|
|
|
// Map background
|
|
|
|
const backgroundSource = new XYZSource({
|
|
|
|
url: 'https://api.mapbox.com/' + [
|
|
|
|
'styles', 'v1', 'mapbox', 'streets-v11',
|
|
|
|
'tiles', '512', '{z}', '{x}', '{y}',
|
|
|
|
].join('/') + `?access_token=${mapboxToken}`,
|
|
|
|
tileSize: [512, 512],
|
|
|
|
});
|
|
|
|
|
|
|
|
const backgroundLayer = new TileLayer({
|
|
|
|
source: backgroundSource,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Data overlay
|
2020-07-17 18:10:52 +00:00
|
|
|
const {segmentsSource, stopsSource} = await makeDataSources();
|
2020-07-17 17:59:34 +00:00
|
|
|
|
|
|
|
const segmentsBorderLayer = new VectorLayer({
|
|
|
|
source: segmentsSource,
|
|
|
|
style: segmentsBorderStyle,
|
|
|
|
|
|
|
|
updateWhileInteracting: true,
|
|
|
|
updateWhileAnimating: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const segmentsInnerLayer = new VectorLayer({
|
|
|
|
source: segmentsSource,
|
|
|
|
style: segmentsInnerStyle,
|
|
|
|
|
|
|
|
updateWhileInteracting: true,
|
|
|
|
updateWhileAnimating: true,
|
|
|
|
});
|
2020-07-17 17:17:06 +00:00
|
|
|
|
2020-07-17 17:59:34 +00:00
|
|
|
const stopsLayer = new VectorLayer({
|
|
|
|
source: stopsSource,
|
|
|
|
style: stopsStyle,
|
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,
|
|
|
|
updateWhileAnimating: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setup map
|
|
|
|
const map = new Map({
|
|
|
|
target,
|
|
|
|
layers: [
|
|
|
|
backgroundLayer,
|
2020-07-17 17:59:34 +00:00
|
|
|
segmentsBorderLayer,
|
|
|
|
segmentsInnerLayer,
|
|
|
|
stopsLayer,
|
2020-07-17 17:17:06 +00:00
|
|
|
],
|
|
|
|
view: new View({
|
|
|
|
center: proj.fromLonLat([3.88, 43.605]),
|
|
|
|
zoom: 13,
|
|
|
|
maxZoom: 22,
|
|
|
|
constrainResolution: true,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
return map;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createMap = createMap;
|