130 lines
4.7 KiB
JavaScript
130 lines
4.7 KiB
JavaScript
class CreatedLayersManager {
|
|
constructor(options) {
|
|
this.url = options.url
|
|
this.mapCore = options.mapCore
|
|
|
|
}
|
|
|
|
async fetchLayers() {
|
|
|
|
const response = await fetch(this.url)
|
|
const data = await response.json()
|
|
data["base_layers"].forEach(layer => {
|
|
const newLayer = this.createLayer(layer);
|
|
this.mapCore.addLayer(newLayer, "base", 'start')
|
|
})
|
|
data["static_layers"].forEach(layer => {
|
|
const newLayer = this.createLayer(layer);
|
|
this.mapCore.addLayer(newLayer, "static", 'start')
|
|
})
|
|
|
|
data["dynamic_layers"].forEach(layer => {
|
|
const newLayer = this.createLayer(layer);
|
|
this.mapCore.addLayer(newLayer, "dynamic")
|
|
})
|
|
|
|
|
|
}
|
|
|
|
createLayer(layer) {
|
|
let newLayer;
|
|
const createStyleFromFeature = function (feature, resolution) {
|
|
// Получаем стили из feature
|
|
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: stroke,
|
|
fill: fill,
|
|
text: 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({
|
|
// text: 'Hello, OpenLayers!',
|
|
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
|
|
}),
|
|
extent: ol.proj.transformExtent(layer.extent,
|
|
'EPSG:4326', 'EPSG:3857'
|
|
)
|
|
});
|
|
} else if (layer.layer_type === 'BaseTile') {
|
|
newLayer = new ol.layer.Tile({
|
|
source: new ol.source.XYZ({
|
|
url: layer.url,
|
|
}),
|
|
extent: ol.proj.transformExtent(layer.extent,
|
|
'EPSG:4326', 'EPSG:3857'
|
|
),
|
|
visible: false,
|
|
baseLayer: true
|
|
});
|
|
} 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
|
|
}
|
|
|
|
}
|