1
0
forked from MTSR/mapserver
mapserver/public/js/map/overlays/TrainlineOverlay.js

133 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-06-11 15:01:16 +02:00
import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js';
import { getMapObjects } from '../../api.js';
2019-04-02 15:00:42 +02:00
2019-06-11 15:01:16 +02:00
export default AbstractGeoJsonOverlay.extend({
2019-09-18 11:53:58 +02:00
initialize: function() {
AbstractGeoJsonOverlay.prototype.initialize.call(this, "train");
this.cache = {
lines: {}, // { "A1":[] }
lineColors: {}, // { "A1": "red" }
lineFeat: []
};
this.pendingQueries = [];
this.lastLayer = L.geoJSON([], {
2019-04-02 15:00:42 +02:00
onEachFeature: function(feature, layer){
if (feature.properties && feature.properties.popupContent) {
layer.bindPopup(feature.properties.popupContent);
}
},
pointToLayer: function (feature, latlng) {
2019-04-03 21:31:36 +02:00
var geojsonMarkerOptions = {
radius: 8,
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
2019-04-04 10:49:55 +02:00
2019-04-02 15:00:42 +02:00
return L.circleMarker(latlng, geojsonMarkerOptions);
2019-06-08 14:31:04 +02:00
},
style: function(feature) {
if (feature.properties && feature.properties.color){
return { color: feature.properties.color };
}
2019-04-02 15:00:42 +02:00
}
});
},
createGeoJson: function(objects){
var self = this;
2019-04-02 15:00:42 +02:00
// which unique lines do objects belong to?
var lines = [];
2019-04-02 15:00:42 +02:00
objects.forEach(function(obj){
if (obj.attributes.line && lines.indexOf(obj.attributes.line) == -1) {
lines.push(obj.attributes.line);
2019-04-02 15:00:42 +02:00
}
});
// query for each line, add to cache
lines.forEach(function(linename){
if (!self.cache.lines[linename]){
// only request if not in cache.
// if someone changed the train lines, the user has to reload. sorry.
self.pendingQueries.push(linename);
getMapObjects({
type: self.type,
attributelike: {
key: "line",
value: linename
}
})
.then(function(objects){
objects.sort(function(a,b){
return parseInt(a.attributes.index) - parseInt(b.attributes.index);
});
2019-04-02 15:00:42 +02:00
self.cache.lines[linename] = objects;
// already sorted, determine color
self.cache.lineColors[linename] = "#ff7800";
for (var i = objects.length-1; i >= 0; i--) {
// find the last element specifying a color
// as was previous behaviour, but be more efficient
if (objects[i].attributes.color){
self.cache.lineColors[linename] = objects[i].attributes.color;
break;
}
}
var feat = {
coords: [],
stations: [],
feature: null
};
//Add stations
objects.forEach(function(entry){
feat.coords.push([entry.x, entry.z]);
if (entry.attributes.station) {
feat.stations.push({
"type": "Feature",
"properties": {
"name": entry.attributes.station,
"color": self.cache.lineColors[linename],
"popupContent": "<b>Train-station (Line " + entry.attributes.line + ")</b><hr>" +
entry.attributes.station
},
"geometry": {
"type": "Point",
"coordinates": [entry.x, entry.z]
}
});
2019-04-02 15:00:42 +02:00
}
});
feat.feature = {
"type":"Feature",
"geometry": {
"type":"LineString",
"coordinates": feat.coords
},
"properties":{
"name": linename,
"color": self.cache.lineColors[linename],
"popupContent": "<b>Train-line (" + linename + ")</b>"
}
};
2019-04-02 15:00:42 +02:00
self.cache.lineFeat[linename] = feat;
2019-04-02 15:00:42 +02:00
//line-points
self.lastLayer.addData(feat.feature);
2019-04-02 15:00:42 +02:00
//stations
feat.stations.forEach(function(stationfeature){
self.lastLayer.addData(stationfeature);
});
});
}
2019-04-02 15:00:42 +02:00
});
return self.lastLayer;
},
2019-04-02 15:00:42 +02:00
});