test/static/js/ol/layersStyles.js
2025-03-23 16:10:45 +07:00

185 lines
5.7 KiB
JavaScript

const getGTOMarkerText = function (feature) {
const statemum = feature.get('garagenum')
if (statemum) {
return statemum;
}
return feature.get('name')
};
const createGTOStateNumTextStyle = function (feature, resolution) {
const font = 'bold' + ' ' + '12px' + '/' + '1' + ' ' + 'Arial';
return new ol.style.Text({
textAlign: 'center',
textBaseline: 'middle',
font: font,
text: getGTOMarkerText(feature, resolution),
fill: new ol.style.Fill({color: '#000'}),
offsetX: 0,
offsetY: -25,
placement: 'point',
maxAngle: '0.7853981633974483',
overflow: 'false',
rotation: parseFloat('0'),
backgroundFill: new ol.style.Fill({color: '#fff'}),
backgroundStroke: new ol.style.Stroke({color: '#000', width: 1}),
padding: [3, 3, 3, 3],
});
};
const createGTOZoneNameTextStyle = function (feature, resolution) {
const font = 'bold' + ' ' + '9px' + '/' + '1' + ' ' + 'Arial';
return new ol.style.Text({
textAlign: 'center',
textBaseline: 'middle',
font: font,
text: getGTOMarkerText(feature, resolution),
fill: new ol.style.Fill({color: '#000'}),
offsetX: 0,
offsetY: -25,
placement: 'point',
maxAngle: '0.7853981633974483',
overflow: 'false',
rotation: parseFloat('0'),
padding: [3, 3, 3, 3],
});
};
function GTOStyleFunction(feature, resolution) {
return new ol.style.Style({
image: new ol.style.Icon({
src: feature.get('icon') ? feature.get('icon') : "/static/img/belaz.png",
width: 30,
height: 20
}),
text: createGTOStateNumTextStyle(feature, resolution)
})
}
function createHatchPattern() {
// Создаем элемент canvas для шаблона штриховки
const canvas = document.createElement('canvas');
canvas.width = 10; // Ширина шаблона
canvas.height = 10; // Высота шаблона
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'rgba(125, 105, 49, 0.1)'; // Цвет штриховки
ctx.lineWidth = 2;
// Рисуем диагональные линии для штриховки
ctx.beginPath();
ctx.moveTo(0, 10);
ctx.lineTo(10, 0);
ctx.stroke();
// Создаем паттерн из канваса
return ctx.createPattern(canvas, 'repeat');
}
function VlocationStyleFunction(feature, resolution) {
return new ol.style.Style({
image: new ol.style.Icon({
src: '/static/img/worker.png',
width: 25,
height: 30
})
})
}
function SPStyleFunction(feature, resolution) {
return new ol.style.Style({
image: new ol.style.Icon({
src: '/static/img/earthquake_yellow.png',
width: 25,
height: 30
})
})
}
function polygonStyleFunction(feature, resolution) {
return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgb(0,117,217)',
width: 2,
}),
fill: new ol.style.Fill({
color: createHatchPattern(),
}),
});
}
function polygonBrownStyleFunction(feature, resolution) {
return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgb(199,136,10)',
width: 2,
}),
fill: new ol.style.Fill({
color: 'rgba(199,136,10, 0.6)',
}),
text: createGTOZoneNameTextStyle(feature, resolution)
});
}
const createStyleFromFeature = function (feature, resolution) {
// Получаем стили из feature
const styleProperties = feature.get("style");
// Если стили заданы, создаем стиль на их основе
if (styleProperties) {
console.log(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 text = styleProperties["text-value"]
? 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"] || "white"
}),
offsetX: styleProperties["text-offset-x"] || 0,
offsetY: styleProperties["text-offset-y"] || -20,
textAlign: styleProperties["text-align"] || "center",
textBaseline: styleProperties["text-baseline"] || "middle"
})
: 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'
})
});
};