1
0
forked from MTSR/mapserver
mapserver/server/static/js/overlays/TrainlineOverlay.js

116 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-04-04 11:49:55 +03:00
/* exported TrainlineOverlay */
/* globals AbstractGeoJsonOverlay: true */
2019-04-02 16:00:42 +03:00
var TrainlineOverlay = AbstractGeoJsonOverlay.extend({
initialize: function(wsChannel, layerMgr) {
AbstractGeoJsonOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "train");
},
createGeoJson: function(objects){
var geoJsonLayer = L.geoJSON([], {
onEachFeature: function(feature, layer){
if (feature.properties && feature.properties.popupContent) {
layer.bindPopup(feature.properties.popupContent);
}
},
pointToLayer: function (feature, latlng) {
2019-04-03 22:31:36 +03:00
var geojsonMarkerOptions = {
radius: 8,
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
2019-04-04 11:49:55 +03:00
2019-04-02 16:00:42 +03:00
return L.circleMarker(latlng, geojsonMarkerOptions);
2019-06-08 15:31:04 +03:00
},
style: function(feature) {
if (feature.properties && feature.properties.color){
return { color: feature.properties.color };
}
2019-04-02 16:00:42 +03:00
}
});
var lines = {}; // { "A1":[] }
2019-06-09 19:32:36 +03:00
var lineColors = {}; // { "A1": "red" }
2019-04-02 16:00:42 +03:00
//Sort and add lines
objects.forEach(function(obj){
if (!obj.attributes.line)
2019-04-04 11:49:55 +03:00
return;
2019-04-02 16:00:42 +03:00
var line = lines[obj.attributes.line];
if (!line){
line = [];
lines[obj.attributes.line] = line;
2019-06-08 15:31:04 +03:00
//default or new color
2019-06-08 15:39:38 +03:00
lineColors[obj.attributes.line] = "#ff7800";
2019-06-08 15:31:04 +03:00
}
if (obj.attributes.color){
//new color
lineColors[obj.attributes.line] = obj.attributes.color;
2019-04-02 16:00:42 +03:00
}
line.push(obj);
});
//Order by index and display
Object.keys(lines).forEach(function(linename){
lines[linename].sort(function(a,b){
return parseInt(a.attributes.index) - parseInt(b.attributes.index);
});
var coords = [];
var stations = [];
//Add stations
lines[linename].forEach(function(entry){
coords.push([entry.x, entry.z]);
if (entry.attributes.station) {
stations.push({
"type": "Feature",
"properties": {
"name": entry.attributes.station,
2019-06-08 15:31:04 +03:00
"color": lineColors[linename],
2019-04-02 16:00:42 +03:00
"popupContent": "<b>Train-station (Line " + entry.attributes.line + ")</b><hr>" +
entry.attributes.station
},
"geometry": {
"type": "Point",
"coordinates": [entry.x, entry.z]
}
});
}
});
var feature = {
"type":"Feature",
"geometry": {
"type":"LineString",
"coordinates":coords
},
"properties":{
"name": linename,
2019-06-08 15:31:04 +03:00
"color": lineColors[linename],
2019-04-02 16:00:42 +03:00
"popupContent": "<b>Train-line (" + linename + ")</b>"
}
2019-04-04 11:49:55 +03:00
};
2019-04-02 16:00:42 +03:00
//line-points
geoJsonLayer.addData(feature);
//stations
stations.forEach(function(stationfeature){
geoJsonLayer.addData(stationfeature);
});
});
return geoJsonLayer;
}
});