test/static/js/ol/map/map-created-layer-manager.js

109 lines
3.8 KiB
JavaScript

class CreatedLayersManager {
constructor(options) {
this.url = options.url;
this.mapCore = options.mapCore;
}
fetchLayers() {
return fetch(this.url)
.then(response => response.json())
.then(data => {
data["static_layers"].forEach(layer => {
const newLayer = this.createLayer(layer);
this.mapCore.addLayer(newLayer);
});
data["dynamic_layers"].forEach(layer => {
const newLayer = this.createLayer(layer);
this.mapCore.addLayer(newLayer);
});
});
}
createLayer(layer) {
let newLayer;
const createStyleFromFeature = function (feature, resolution) {
const styleProperties = feature.get("style");
if (styleProperties) {
const stroke = styleProperties["stroke-color"] || styleProperties["stroke-width"]
? new ol.style.Stroke({
color: styleProperties["stroke-color"] || "blue",
width: styleProperties["stroke-width"] || 2,
lineDash: styleProperties['stroke-line-dash']
})
: null;
const fill = styleProperties["fill-color"]
? new ol.style.Fill({color: styleProperties["fill-color"]})
: null;
const zoom = window.Map_.getView().getZoom();
const text = zoom > 12
? new ol.style.Text({
text: styleProperties["text-value"],
font: styleProperties["text-font"] || "14px Calibri,sans-serif",
fill: new ol.style.Fill({
color: styleProperties["text-color"] || styleProperties["stroke-color"] || "white"
}),
scale: 1,
offsetX: styleProperties["text-offset-x"] || 0,
offsetY: styleProperties["text-offset-y"] || 0,
textAlign: styleProperties["text-align"] || "center",
textBaseline: styleProperties["text-baseline"] || "middle",
rotation: styleProperties['text-rotation'] || 0
})
: null;
return new ol.style.Style({
stroke,
fill,
text
});
}
return new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 0, 0, 0.5)'
}),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2
}),
text: new ol.style.Text({
font: '14px Calibri,sans-serif',
fill: new ol.style.Fill({color: 'white'}),
offsetX: 0,
offsetY: -20,
textAlign: 'center',
textBaseline: 'middle'
})
});
};
if (layer.layer_type === 'Tile') {
newLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: layer.url,
}),
visible: layer.is_visible,
});
} else if (layer.layer_type === 'Vector') {
newLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: layer.url,
format: new ol.format.GeoJSON(),
}),
visible: layer.is_visible,
style: createStyleFromFeature
});
}
newLayer.set('title', layer.name);
newLayer.set('name', layer.name);
return newLayer;
}
}