Files
frontend/packages/common/business_components/src/ui/map/lib/gis.style.js
T

402 lines
12 KiB
JavaScript

import GisApp from './gis.core.js';
import Style from 'ol/style/Style.js';
import Fill from 'ol/style/Fill.js';
import Icon from 'ol/style/Icon.js';
import Circle from 'ol/style/Circle.js';
import Stroke from 'ol/style/Stroke.js';
import Text from 'ol/style/Text.js';
import Point from 'ol/geom/Point.js';
import RegularShape from 'ol/style/RegularShape.js';
import Overlay from 'ol/Overlay';
import { createPortal } from 'react-dom';
import ReactDOM from 'react-dom/client';
import React from 'react';
const GisOlStyle = function () {
this.initialize();
};
GisOlStyle.prototype = {
overlays: {},
hiddenOverlays: {},
initialize: function () {
this.layericonPath = GisApp.CONTEXT_LAYER_PATH;
},
getRGBAToString: function (r, g, b, a) {
let rgba = 'rgba({r}, {g}, {b}, {a})';
return rgba.replace('{r}', r).replace('{g}', g).replace('{b}', b).replace('{a}', a);
},
getFillColor: function (layer) {
let r = layer.get('fillColorR');
let g = layer.get('fillColorG');
let b = layer.get('fillColorB');
let a = layer.get('fillColorA');
return this.getRGBAToString(r, g, b, a);
},
getFontLineColor: function (layer) {
let r = layer.get('fontOutlineColorR');
let g = layer.get('fontOutlineColorG');
let b = layer.get('fontOutlineColorB');
let a = layer.get('fontOutlineColorA');
return this.getRGBAToString(r, g, b, a);
},
getFontFillColor: function (layer) {
let r = layer.get('fontColorR');
let g = layer.get('fontColorG');
let b = layer.get('fontColorB');
let a = layer.get('fontColorA');
return this.getRGBAToString(r, g, b, a);
},
point: function (layer, options, feature, resolution) {
let symbol = layer.get('iconNm');
let maticKey = layer.get('maticKey');
let maticVal1 = layer.get('maticVal1');
let image;
let style = new Style({
fill: new Fill(),
});
let text = this.getText(layer, feature, resolution);
style.setText(text);
if (symbol) {
let rotation = 0;
if (feature) {
let angDir = feature.get('angDir');
if (angDir) {
rotation = (angDir * Math.PI) / 180;
}
}
image = new Icon({
crossOrigin: 'anonymous',
scale: 1.2,
src: this.layericonPath + symbol,
rotation: rotation,
});
} else {
image = new Circle({
fill: new Fill({
color: '#4848B4',
}),
radius: 5,
});
}
const overlayHtml = feature.get('html');
if (overlayHtml) {
this.setOverlay(layer, feature, overlayHtml);
}
let baseResolution = 2;
let minScale = 0.8;
let maxScale = 1.5;
let scale = baseResolution / resolution > maxScale ? maxScale : baseResolution / resolution;
scale = Math.max(minScale, Math.min(maxScale, scale));
image.setScale(scale);
style.setImage(image);
Object.assign(style, options);
return style;
},
line: function (layer, options, feature, resolution) {
let linewidth = layer.get('lnWdth');
let lineColor = layer.get('lnColor');
let style = new Style({
stroke: new Stroke({
color: lineColor,
width: linewidth,
}),
text: new Text(),
});
if (GisApp.Module.toolbar && GisApp.Module.toolbar.defaults.blockDiagramVisible) {
let blockDiagramLayerId = GisApp.Module.toolbar.getBlockDiagramLayerId(layer.get('lyrId'));
if (blockDiagramLayerId) {
style = this.getArrowStyle(feature, lineColor, linewidth);
}
}
return style;
},
poligon: function (layer, options, feature, resolution) {
const style = new Style();
const color = layer.get('polgnColor');
const fill = new Fill({
color,
});
style.setFill(fill);
Object.assign(style, options);
return style;
},
lineLabelPoligon: function (layer, options, feature, resolution) {
let style = new Style();
const polgnColor = layer.get('polgnColor');
let fill = new Fill({
color: polgnColor,
});
let lnWdth = layer.get('lnWdth');
let lineColor = layer.get('lnColor');
let lineDash = layer.get('lineDash');
let stroke = new Stroke({
color: lineColor || '#000000',
width: lnWdth,
lineDash: lineDash ? [10, 5] : null,
});
let text = this.getText(layer, feature, resolution);
if (text) {
style.setText(text);
}
const overlayHtml = feature.get('html');
if (overlayHtml) {
this.setOverlay(layer, feature, overlayHtml);
}
style.setFill(fill);
style.setStroke(stroke);
Object.assign(style, options);
return style;
},
imagePoligon: function (layer, options, feature, resolution) {
let style = new Style();
let symbol = layer.get('iconNm');
let retGeom = feature.getGeometry().getInteriorPoint();
let image = new Icon({
crossOrigin: 'anonymous',
imgSize: [20, 20],
src: this.layericonPath + symbol,
});
let scale = 0.5 / resolution > 1 ? 1 : 0.5 / resolution;
image.setScale(1);
style.setGeometry(retGeom);
style.setImage(image);
Object.assign(style, options);
return style;
},
setOverlay: function (layer, feature, overlayHtml) {
const layerFeature = layer.getSource().getFeatures()[0];
const layerCoordinate = layerFeature ? layerFeature.getGeometry().getCoordinates() : null;
const properties = feature.getProperties();
const overLayId = feature?.get('overLayId');
const overLayMinZoom = layer.get('overLayMinZoom') || '10';
const overLayOffset = layer?.get('overLayOffset');
let findoOverlay = this.overlays[overLayId];
if (findoOverlay) {
const zoom = GisApp.Module.core.map.getView().getZoom();
if (Number(overLayMinZoom) < zoom) {
findoOverlay.options.element.style.display = 'block';
} else {
findoOverlay.options.element.style.display = 'none';
}
return findoOverlay;
}
let overlayContainer = document.createElement('div');
document.body.appendChild(overlayContainer);
if (typeof overlayHtml === 'string') {
//const sanitizedHtml = DOMPurify.sanitize(overlayHtml);
//overlayContainer.innerHTML = sanitizedHtml;
overlayContainer.innerHTML = overlayHtml;
}
// React 컴포넌트일 경우 createPortal() 사용
else if (React.isValidElement(overlayHtml)) {
ReactDOM.createRoot(overlayContainer).render(createPortal(overlayHtml, overlayContainer));
}
// 오버레이 생성
let overlay = new Overlay({
element: overlayContainer,
positioning: 'bottom-center',
stopEvent: false,
offset: overLayOffset ? overLayOffset : [80, 0], // 오프셋 설정 (위치 조정)
});
this.overlays[overLayId] = overlay;
// feature의 좌표 가져오기
let geometry = feature.getGeometry();
let coordinate;
// Point인 경우
if (geometry.getType() === 'Point') {
coordinate = geometry.getCoordinates();
}
// LineString 또는 MultiLineString인 경우
else if (geometry.getType() === 'LineString' || geometry.getType() === 'MultiLineString') {
coordinate = geometry.getFlatMidpoint(); // 선의 중간 좌표를 가져옴
}
// Polygon 또는 MultiPolygon인 경우
else if (geometry.getType() === 'Polygon' || geometry.getType() === 'MultiPolygon') {
coordinate = geometry.getExtent();
const center = [(coordinate[0] + coordinate[2]) / 2, (coordinate[1] + coordinate[3]) / 2];
coordinate = center;
} else {
console.error('Unsupported geometry type: ' + geometry.getType());
return;
}
overlay.setPosition(coordinate);
GisApp.Module.core.map.addOverlay(overlay);
const zoom = GisApp.Module.core.map.getView().getZoom();
if (Number(overLayMinZoom) < zoom) {
overlay.options.element.style.display = 'block';
} else {
overlay.options.element.style.display = 'none';
}
},
label: function (layer, options, feature, resolution) {
let textBaseline = layer.get('textBaseline') || 'bottom';
let textAlign = layer.get('textAlign') || 'center';
let fontExtnColor = layer.get('fontExtnColor');
let fontExtnWdth = Number(layer.get('fontExtnWdth') || '3.5');
let fontIntnColor = layer.get('fontIntnColor');
let style = new Style();
let prop = feature.getProperties();
let label = prop[layer.get('lblAtrbNm')];
let text = new Text({
stroke: new Stroke({
width: fontExtnWdth,
color: fontExtnColor,
}),
fill: new Fill({
color: fontIntnColor,
}),
font: layer.get('font'),
textBaseline: textBaseline,
textAlign: textAlign,
text: label,
});
style.setStroke(null);
style.setFill(null);
style.setText(text);
Object.assign(style, options);
return style;
},
getText: function (layer, feature, resolution) {
let textBaseline = layer.get('textBaseline') || 'middle';
let textAlign = layer.get('textAlign') || 'center';
let fontExtnColor = layer.get('fontExtnColor');
let fontExtnWdth = Number(layer.get('fontExtnWdth') || '1');
let fontIntnColor = layer.get('fontIntnColor');
//let minViewSize = layer.get('minViewSize') || 13;
let minViewSize = 14;
if (feature == null) {
return;
}
let prop = feature.getProperties();
let label = prop[layer.get('lblAtrbNm')];
let scaleFactor = 1 / resolution; // 해상도가 낮아질수록 글자 크기 증가
const zoom = GisApp.Module.core.map.getView().getZoom();
if (zoom <= minViewSize) {
label = '';
}
let text = new Text({
// stroke: new Stroke({
// color: '#FFFFFF', // 흰색 테두리
// width: fontExtnWdth,
// }),
fill: new Fill({
color: fontIntnColor,
width: fontExtnWdth,
}),
font: `${layer.get('fontNm')}`, // 동적으로 폰트 크기 적용
textBaseline: textBaseline,
textAlign: textAlign,
overflow: true,
text: label,
offsetY: 20,
});
return text;
},
getArrowStyle: function (feature, lineColor, linewidth) {
let styles = [
new Style({
stroke: new Stroke({
color: lineColor,
width: linewidth,
}),
}),
];
let geomLienString = GisApp.Module.core.getGeomLineOfFeature(feature);
if (geomLienString) {
let startCoord = geomLienString.getFirstCoordinate();
let endCoord = geomLienString.getLastCoordinate();
let dx = endCoord[0] - startCoord[0];
let dy = endCoord[1] - startCoord[1];
let rotation = Math.atan2(dy, dx);
let centerCoord = GisApp.Module.core.getCenterOfLineFeature(feature);
styles.push(
new Style({
geometry: new Point(centerCoord),
image: new RegularShape({
fill: new Fill({ color: lineColor }),
points: 3,
radius: 6,
rotation: -rotation,
angle: Math.PI / 2,
}),
})
);
}
return styles;
},
//레이어 스타일 반환
getLayerStyle: function (layer, options) {
let gobjTy = layer.get('gobjTy');
if (!gobjTy) return;
let style = this[gobjTy](layer, options);
return style;
},
getLayerStyleFunction: function (layer, options) {
let gobjTy = layer.get('gobjTy');
let styleFunction = function (feature, resolution) {
return this[gobjTy](layer, options, feature, resolution);
}.bind(this);
return styleFunction;
},
adjustOverlayPosition: function (overlay, coordinate) {
const overlays = this.overlays;
let adjustedCoordinate = [...coordinate]; // 초기 좌표 설정
const minDistance = 100000; // 최소 거리 (필요시 조정)
let offset = overlay.getOffset() || [0, 0];
let closestOverlay = null;
let closestDistance = Infinity;
// 모든 오버레이의 위치를 비교하여 가장 가까운 오버레이 찾기
Object.values(overlays).forEach(existingOverlay => {
if (existingOverlay) {
const existingCoordinate = existingOverlay.getPosition();
const existingOffset = existingOverlay.getOffset() || [0, 0];
// 현재 오버레이의 좌표 가져오기
const overlayCoordinate = overlay.getPosition();
if (!overlayCoordinate || !existingCoordinate) return;
// 거리 계산 (유클리드 거리)
const distance = Math.sqrt(Math.pow(overlayCoordinate[0] - existingCoordinate[0], 2) + Math.pow(overlayCoordinate[1] - existingCoordinate[1], 2));
// 가장 가까운 오버레이 업데이트
if (distance > 0 && distance < closestDistance) {
closestDistance = distance;
closestOverlay = existingOverlay;
} else {
offset = [50, 50];
}
}
});
// 가장 가까운 오버레이가 있으면 offset 조정
if (closestOverlay && closestDistance < minDistance) {
const closestOffset = closestOverlay.getOffset() || [0, 0];
// X, Y 좌표를 기존 offset 값의 반대로 설정
offset = [-closestOffset[0], -closestOffset[1]];
}
// 최종 offset 적용
overlay.setOffset(offset);
},
};
export default GisOlStyle;