315 lines
9.8 KiB
TypeScript
315 lines
9.8 KiB
TypeScript
"use client";
|
|
|
|
import Feature from "ol/Feature.js";
|
|
import type { FeatureLike } from "ol/Feature.js";
|
|
import GeoJSON from "ol/format/GeoJSON.js";
|
|
import Point from "ol/geom/Point.js";
|
|
import type OlMap from "ol/Map.js";
|
|
import { unByKey } from "ol/Observable.js";
|
|
import { bbox as bboxStrategy } from "ol/loadingstrategy.js";
|
|
import { transform } from "ol/proj.js";
|
|
import VectorLayer from "ol/layer/Vector.js";
|
|
import VectorSource from "ol/source/Vector.js";
|
|
import CircleStyle from "ol/style/Circle.js";
|
|
import Fill from "ol/style/Fill.js";
|
|
import Stroke from "ol/style/Stroke.js";
|
|
import Style from "ol/style/Style.js";
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
import Gis, { useGisContext } from "@common/business_components/ui/map/Gis";
|
|
import GisApp from "@common/business_components/ui/map/lib/gis.core";
|
|
|
|
import type { GeoPoint, PipeMatch } from "../_lib/types";
|
|
|
|
type DredgingLocationMapProps = {
|
|
value: GeoPoint | null;
|
|
onChange?: (point: GeoPoint) => void;
|
|
interactive?: boolean;
|
|
statusText?: string;
|
|
matchedPipe?: PipeMatch | null;
|
|
};
|
|
|
|
type GisContextValue = {
|
|
gisMap: OlMap | null;
|
|
};
|
|
|
|
const confirmedMarkerStyle = new Style({
|
|
image: new CircleStyle({
|
|
radius: 9,
|
|
fill: new Fill({ color: "rgba(220, 38, 38, 0.9)" }),
|
|
stroke: new Stroke({ color: "#ffffff", width: 3 }),
|
|
}),
|
|
});
|
|
|
|
const draftMarkerStyle = new Style({
|
|
image: new CircleStyle({
|
|
radius: 9,
|
|
fill: new Fill({ color: "rgba(37, 99, 235, 0.9)" }),
|
|
stroke: new Stroke({ color: "#ffffff", width: 3 }),
|
|
}),
|
|
});
|
|
|
|
const nearbyPipeStyle = [
|
|
new Style({
|
|
stroke: new Stroke({ color: "rgba(255, 255, 255, 0.95)", width: 5 }),
|
|
zIndex: 10,
|
|
}),
|
|
new Style({
|
|
stroke: new Stroke({ color: "#0f6cbd", width: 3 }),
|
|
zIndex: 11,
|
|
}),
|
|
];
|
|
|
|
const matchedPipeStyle = [
|
|
new Style({
|
|
stroke: new Stroke({ color: "rgba(255, 255, 255, 0.98)", width: 8 }),
|
|
zIndex: 20,
|
|
}),
|
|
new Style({
|
|
stroke: new Stroke({ color: "#f97316", width: 5 }),
|
|
zIndex: 21,
|
|
}),
|
|
];
|
|
|
|
const PIPE_LAYER_MIN_ZOOM = 13;
|
|
|
|
const normalizePipeFid = (fid: unknown) => {
|
|
const value = String(fid ?? "").trim();
|
|
const separatorIndex = value.lastIndexOf(".");
|
|
return separatorIndex >= 0 ? value.slice(separatorIndex + 1) : value;
|
|
};
|
|
|
|
const getPipeFeatureStyle = (matchedPipeFid?: string) => (feature: FeatureLike) =>
|
|
normalizePipeFid(feature.getId()) === normalizePipeFid(matchedPipeFid)
|
|
? matchedPipeStyle
|
|
: nearbyPipeStyle;
|
|
|
|
/**
|
|
* 공통 Gis 내부 지도 객체에 관로 layer, 클릭 interaction과 위치 marker를 추가한다.
|
|
* 공통 지도 코드는 수정하지 않고 이 컴포넌트가 추가한 layer/event만 정리한다.
|
|
*/
|
|
const LocationInteraction = ({
|
|
value,
|
|
onChange,
|
|
interactive,
|
|
matchedPipe,
|
|
}: Required<Pick<DredgingLocationMapProps, "interactive">> &
|
|
Omit<DredgingLocationMapProps, "interactive">) => {
|
|
const { gisMap } = useGisContext() as GisContextValue;
|
|
const markerLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
|
const pipeLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
|
const [pipeLoadState, setPipeLoadState] = useState<
|
|
"idle" | "loading" | "loaded" | "error"
|
|
>("idle");
|
|
const [visiblePipeCount, setVisiblePipeCount] = useState(0);
|
|
|
|
useEffect(() => {
|
|
if (!gisMap) return;
|
|
|
|
const pipeSource = new VectorSource({
|
|
format: new GeoJSON(),
|
|
strategy: bboxStrategy,
|
|
url: (extent) => {
|
|
const [minX, minY, maxX, maxY] = extent;
|
|
const layerModule = GisApp.Module.layer as unknown as {
|
|
storageNm?: string;
|
|
} | null;
|
|
const storageName = String(layerModule?.storageNm ?? "hasudo:");
|
|
const params = new URLSearchParams({
|
|
SERVICE: "WFS",
|
|
VERSION: "1.1.0",
|
|
REQUEST: "GetFeature",
|
|
TYPENAME: `${storageName}tb_ms_swage_ppl`,
|
|
SRSNAME: GisApp.TO_PROJ,
|
|
OUTPUTFORMAT: "application/json",
|
|
MAXFEATURES: "2000",
|
|
CQL_FILTER: `BBOX(geom, ${minX}, ${minY}, ${maxX}, ${maxY}, '${GisApp.TO_PROJ}')`,
|
|
});
|
|
return `${GisApp.GEOSERVER_URL}?${params.toString()}`;
|
|
},
|
|
});
|
|
const pipeLayer = new VectorLayer({
|
|
source: pipeSource,
|
|
minZoom: PIPE_LAYER_MIN_ZOOM,
|
|
style: getPipeFeatureStyle(matchedPipe?.fid),
|
|
updateWhileAnimating: false,
|
|
updateWhileInteracting: false,
|
|
zIndex: 700,
|
|
});
|
|
pipeLayer.set("dredgingPipeLayer", true);
|
|
pipeLayer.set("lyrTyNm", "tb_ms_swage_ppl");
|
|
pipeLayer.set("clickPsblYn", "N");
|
|
gisMap.addLayer(pipeLayer);
|
|
pipeLayerRef.current = pipeLayer;
|
|
|
|
const sourceEventKeys = [
|
|
pipeSource.on("featuresloadstart", () => setPipeLoadState("loading")),
|
|
pipeSource.on("featuresloadend", () => {
|
|
setVisiblePipeCount(pipeSource.getFeatures().length);
|
|
setPipeLoadState("loaded");
|
|
}),
|
|
pipeSource.on("featuresloaderror", () => {
|
|
setVisiblePipeCount(0);
|
|
setPipeLoadState("error");
|
|
}),
|
|
];
|
|
|
|
const markerSource = new VectorSource();
|
|
const markerLayer = new VectorLayer({
|
|
source: markerSource,
|
|
style: interactive ? draftMarkerStyle : confirmedMarkerStyle,
|
|
zIndex: 999,
|
|
});
|
|
markerLayer.set("dredgingLocationLayer", true);
|
|
gisMap.addLayer(markerLayer);
|
|
markerLayerRef.current = markerLayer;
|
|
|
|
const clickKey = interactive
|
|
? gisMap.on("singleclick", (event) => {
|
|
const [longitude, latitude] = transform(
|
|
event.coordinate,
|
|
GisApp.TO_PROJ,
|
|
GisApp.FROM_PROJ,
|
|
);
|
|
if (
|
|
longitude === undefined ||
|
|
latitude === undefined ||
|
|
!Number.isFinite(longitude) ||
|
|
!Number.isFinite(latitude)
|
|
) {
|
|
return;
|
|
}
|
|
onChange?.({ longitude, latitude });
|
|
})
|
|
: null;
|
|
|
|
return () => {
|
|
if (clickKey) unByKey(clickKey);
|
|
unByKey(sourceEventKeys);
|
|
gisMap.removeLayer(markerLayer);
|
|
gisMap.removeLayer(pipeLayer);
|
|
markerLayerRef.current = null;
|
|
pipeLayerRef.current = null;
|
|
};
|
|
}, [gisMap, interactive, onChange]);
|
|
|
|
useEffect(() => {
|
|
if (!gisMap || !markerLayerRef.current) return;
|
|
const source = markerLayerRef.current.getSource();
|
|
source?.clear();
|
|
if (!value) return;
|
|
|
|
const coordinate = transform(
|
|
[value.longitude, value.latitude],
|
|
GisApp.FROM_PROJ,
|
|
GisApp.TO_PROJ,
|
|
);
|
|
source?.addFeature(new Feature({ geometry: new Point(coordinate) }));
|
|
gisMap.getView().animate({ center: coordinate, zoom: 17, duration: 250 });
|
|
}, [gisMap, interactive, value]);
|
|
|
|
useEffect(() => {
|
|
if (!pipeLayerRef.current) return;
|
|
pipeLayerRef.current.setStyle(getPipeFeatureStyle(matchedPipe?.fid));
|
|
pipeLayerRef.current.changed();
|
|
}, [matchedPipe?.fid]);
|
|
|
|
const pipeStatusText = (() => {
|
|
if (!value) return "위치를 등록하면 주변 관로가 표시됩니다.";
|
|
if (pipeLoadState === "loading") return "주변 관로를 불러오는 중입니다.";
|
|
if (pipeLoadState === "error") return "주변 관로를 불러오지 못했습니다.";
|
|
if (pipeLoadState === "loaded") {
|
|
return visiblePipeCount > 0
|
|
? `주변 관로 ${visiblePipeCount.toLocaleString()}건 표시`
|
|
: "현재 지도 범위에 표시할 관로가 없습니다.";
|
|
}
|
|
return "지도를 확대하면 주변 관로가 표시됩니다.";
|
|
})();
|
|
|
|
return (
|
|
<div className="rounded-sm border border-slate-300 bg-white/95 px-3 py-2 text-sm text-slate-700 shadow">
|
|
<p className="font-medium">
|
|
{interactive
|
|
? "지도를 클릭해 준설위치를 선택하세요."
|
|
: "등록 위치와 주변 관로를 함께 확인합니다."}
|
|
</p>
|
|
<div className="mt-1 flex flex-wrap items-center gap-x-4 gap-y-1 text-xs">
|
|
<span className="inline-flex items-center gap-1.5">
|
|
<span className="inline-block h-1 w-6 rounded bg-[#0f6cbd]" />
|
|
주변 관로
|
|
</span>
|
|
<span className="inline-flex items-center gap-1.5">
|
|
<span className="inline-block h-1.5 w-6 rounded bg-[#f97316]" />
|
|
자동 연결 관로
|
|
</span>
|
|
<span
|
|
className={
|
|
pipeLoadState === "error" ? "text-rose-600" : "text-slate-500"
|
|
}
|
|
data-testid="dredging-pipe-map-status"
|
|
>
|
|
{pipeStatusText}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const DredgingLocationMap = ({
|
|
value,
|
|
onChange,
|
|
interactive = true,
|
|
statusText,
|
|
matchedPipe,
|
|
}: DredgingLocationMapProps) => {
|
|
const gisMaps = useRef<OlMap | null>(null);
|
|
const defaultLonLat = useMemo<[number, number]>(
|
|
() => (value ? [value.longitude, value.latitude] : [127.75, 36]),
|
|
[value],
|
|
);
|
|
|
|
return (
|
|
<div className="overflow-hidden rounded-sm border border-slate-300">
|
|
<Gis
|
|
treeData={[]}
|
|
gisMaps={gisMaps}
|
|
isSearch={false}
|
|
isMiniMap={false}
|
|
isOpen={false}
|
|
setIsOpen={() => undefined}
|
|
featureData={null}
|
|
setFeatureData={() => undefined}
|
|
isSearchModalOpen={false}
|
|
setIsSearchModalOpen={() => undefined}
|
|
searchBar={
|
|
<LocationInteraction
|
|
value={value}
|
|
onChange={onChange}
|
|
interactive={interactive}
|
|
matchedPipe={matchedPipe}
|
|
/>
|
|
}
|
|
defaultZoom={value ? 17 : 7.5}
|
|
defaultLonLat={defaultLonLat}
|
|
height="h-[540px]"
|
|
showZoomLevel={true}
|
|
showFloatingSearchButton={false}
|
|
treeTogglePosition="top"
|
|
/>
|
|
<div
|
|
className="border-t bg-slate-50 px-4 py-2 text-sm text-slate-700"
|
|
data-testid="dredging-map-status"
|
|
>
|
|
{statusText ??
|
|
(interactive
|
|
? "지도에서 준설위치를 선택해주세요."
|
|
: value
|
|
? "등록된 준설위치가 표시되었습니다."
|
|
: "위치가 등록되지 않았습니다.")}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default DredgingLocationMap;
|