분석이 30초 걸리는데 B05는 일반 사용자 화면이다. 관리자 확인용 B04에서
한 번 돌려 저장하고, B05는 그 결과를 읽어 관 보충과 세부유역만 처리한다
(2026-07-31 사용자 지시).
노선 원천 변경
- B05 확정 경로 -> B03 업로드 계획 노선 파일(CSV). 분석이 노선 설계보다
먼저 끝나 있어야 하기 때문. 샘플 planned_route_sample_epsg5187.csv 로 검증.
- common_util_route_geometry.py 신설 — RouteVertex/StructureCandidate/누가거리
보간/세류 교차점/계획 노선 CSV 리더. B04와 B05가 같은 표현을 쓰도록 공용화.
열 이름은 대소문자·한글 표기를 함께 받는다(B03이 여러 형식 수용 예정).
B04 (관리자 확인용, 신규)
- Engine_Watershed_{Grid,Stream,Descent,Flow,Expand,Export} — B05에서 git mv
- Engine_Watershed_Analyze.py — 1~8단계 오케스트레이션
- Router_Watershed.py — GET /drainage/primary-region
- UI_Watershed.ts — 2D 지도 GIS 레이어 그룹에 "배수유역" 토글 추가.
격자/화살표/세류망/1차영역/2차유역/기본관을 겹쳐 그린다.
- 저장 위치 B05_wf2_Route/drainage -> B04_wf1_Surface/drainage
- 03_road_routing 단계 추가: B05가 세부유역을 나눌 최소 배열(셀->도로셀 귀속,
유하장, 강도, 도로셀 제원, 셀 표고) + 계획도로선/기본배관/2차유역 기하
B05 (일반 사용자용, 축소)
- Engine_Drainage_Basin.py — B04 산출물 로더 + 관 보충(9) + 측구 라우팅/세부유역(10,11)
- Engine_Drainage.py 는 관경 산정만 남기고 322 -> 27줄
- Router_Drainage.py 509 -> 142줄. POST /drainage/basins 만 남김
- 화살표·격자·강도 띠 렌더 제거. 계획도로선/기본배관/2차유역만 받는다
삭제
- _legacy_watershed/ 4파일 (능선 행진 방식 원본 보관본)
- Engine_Watershed_Basin.py (B04 Analyze + B05 Drainage_Basin 으로 분할)
- GET /drainage/candidates 와 propose_structure_stations (구방식 후보 제안)
E2E 검증 (실데이터)
B04 분석 28.2s -> 저장(geojson 11KB + npz 2.6MB)
B05 로드 + 세부 설계 0.11s <-- 30초가 0.1초로
면적 457,404m2 로 B04 2차 유역과 정확히 일치
관 편집 재산정 0.12s, 관 3개 -> 세부유역 3개, 면적 보존
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
401 lines
15 KiB
Python
401 lines
15 KiB
Python
"""B03 원본 입력 파일 메타데이터 분석."""
|
|
|
|
import csv
|
|
import logging
|
|
import math
|
|
import re
|
|
from pathlib import Path
|
|
from threading import get_ident
|
|
from typing import Any
|
|
|
|
import laspy
|
|
import numpy as np
|
|
import rasterio
|
|
from pyproj import CRS
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_CUSTOM_VERTICAL_AUTHORITY_PATTERN = re.compile(
|
|
r',?\s*AUTHORITY\["EPSG","(9995|99999)"\]',
|
|
re.IGNORECASE,
|
|
)
|
|
_CUSTOM_VERTICAL_WARNING_PATTERN = re.compile(
|
|
r"proj_create_from_database: crs not found: EPSG:(9995|99999)",
|
|
re.IGNORECASE,
|
|
)
|
|
|
|
|
|
class _CustomVerticalCrsWarningFilter(logging.Filter):
|
|
"""알려진 사설 수직 CRS 경고만 B03 안내 로그로 변환한다."""
|
|
|
|
def __init__(self, source: Path) -> None:
|
|
super().__init__()
|
|
self.source = source
|
|
self.thread_id = get_ident()
|
|
self.logged_codes: set[str] = set()
|
|
|
|
def filter(self, record: logging.LogRecord) -> bool:
|
|
if get_ident() != self.thread_id:
|
|
return True
|
|
match = _CUSTOM_VERTICAL_WARNING_PATTERN.search(record.getMessage())
|
|
if match is None:
|
|
return True
|
|
code = f"EPSG:{match.group(1)}"
|
|
if code not in self.logged_codes:
|
|
logger.info(
|
|
"사용자 정의 수직 CRS 코드를 보존합니다: file=%s code=%s",
|
|
self.source.name,
|
|
code,
|
|
)
|
|
self.logged_codes.add(code)
|
|
return False
|
|
|
|
|
|
def _component_metadata(crs: CRS) -> dict[str, Any]:
|
|
"""CRS 구성 요소를 JSON 저장 가능한 메타데이터로 변환한다."""
|
|
authority = crs.to_authority()
|
|
epsg = crs.to_epsg()
|
|
return {
|
|
"name": crs.name,
|
|
"type": crs.type_name,
|
|
"epsg": epsg,
|
|
"authority": (
|
|
{"name": authority[0], "code": authority[1]} if authority is not None else None
|
|
),
|
|
}
|
|
|
|
|
|
def normalize_crs_metadata(crs: Any | None) -> dict[str, Any]:
|
|
"""복합 CRS를 수평·수직 구성 요소로 분리해 일관된 상태를 반환한다."""
|
|
if crs is None:
|
|
return {
|
|
"crs": None,
|
|
"epsg": None,
|
|
"horizontal_crs": None,
|
|
"vertical_crs": None,
|
|
"crs_status": "missing_crs",
|
|
}
|
|
|
|
parsed = crs if isinstance(crs, CRS) else CRS.from_user_input(crs)
|
|
components = parsed.sub_crs_list or [parsed]
|
|
horizontal = next(
|
|
(item for item in components if item.is_projected or item.is_geographic),
|
|
None,
|
|
)
|
|
vertical = next((item for item in components if item.is_vertical), None)
|
|
|
|
horizontal_metadata = _component_metadata(horizontal) if horizontal is not None else None
|
|
vertical_metadata = _component_metadata(vertical) if vertical is not None else None
|
|
horizontal_epsg = horizontal_metadata["epsg"] if horizontal_metadata is not None else None
|
|
|
|
if horizontal_epsg is None:
|
|
status = "unknown_horizontal_crs"
|
|
elif vertical_metadata is not None and vertical_metadata["epsg"] is None:
|
|
status = "custom_vertical_crs"
|
|
else:
|
|
status = "identified"
|
|
|
|
return {
|
|
"crs": crs.to_string(),
|
|
"epsg": horizontal_epsg,
|
|
"horizontal_crs": horizontal_metadata,
|
|
"vertical_crs": vertical_metadata,
|
|
"crs_status": status,
|
|
}
|
|
|
|
|
|
def _log_crs_status(source: Path, metadata: dict[str, Any]) -> None:
|
|
"""정상화된 CRS 상태를 B03 도메인 로그로 남긴다."""
|
|
if metadata["crs_status"] == "custom_vertical_crs":
|
|
logger.info(
|
|
"사용자 정의 수직 CRS를 보존합니다: file=%s horizontal_epsg=%s vertical=%s",
|
|
source.name,
|
|
metadata["epsg"],
|
|
metadata["vertical_crs"]["name"],
|
|
)
|
|
elif metadata["crs_status"] == "unknown_horizontal_crs":
|
|
logger.warning("수평 CRS를 EPSG로 식별하지 못했습니다: file=%s", source.name)
|
|
|
|
|
|
def _prepare_prj_wkt(text: str) -> tuple[str, list[str]]:
|
|
"""KNGeoid24 사설 EPSG 표식만 파싱용 WKT에서 분리한다."""
|
|
if "KNGeoid24" not in text:
|
|
return text, []
|
|
codes = sorted({f"EPSG:{code}" for code in _CUSTOM_VERTICAL_AUTHORITY_PATTERN.findall(text)})
|
|
return _CUSTOM_VERTICAL_AUTHORITY_PATTERN.sub("", text), codes
|
|
|
|
|
|
def analyze_las_metadata(path: str | Path) -> dict[str, Any]:
|
|
"""LAS/LAZ 헤더와 분류 통계를 메모리에 전체 적재하지 않고 분석한다."""
|
|
source = Path(path)
|
|
with laspy.open(source) as las_file:
|
|
header = las_file.header
|
|
point_format = header.point_format
|
|
dimension_names = list(point_format.dimension_names)
|
|
point_count = int(header.point_count)
|
|
crs = header.parse_crs()
|
|
crs_metadata = normalize_crs_metadata(crs)
|
|
_log_crs_status(source, crs_metadata)
|
|
metadata: dict[str, Any] = {
|
|
"file": source.name,
|
|
"version": f"{header.version.major}.{header.version.minor}",
|
|
"point_format": {
|
|
"id": point_format.id,
|
|
"dimensions": dimension_names,
|
|
},
|
|
"point_count": point_count,
|
|
"bounds": {
|
|
"x": [float(header.mins[0]), float(header.maxs[0])],
|
|
"y": [float(header.mins[1]), float(header.maxs[1])],
|
|
"z": [float(header.mins[2]), float(header.maxs[2])],
|
|
},
|
|
"scale": [float(value) for value in header.scales],
|
|
"offset": [float(value) for value in header.offsets],
|
|
"has_crs": crs is not None,
|
|
**crs_metadata,
|
|
"has_classification": "classification" in dimension_names,
|
|
"has_rgb": all(name in dimension_names for name in ("red", "green", "blue")),
|
|
"has_intensity": "intensity" in dimension_names,
|
|
"has_return_number": "return_number" in dimension_names,
|
|
}
|
|
|
|
if metadata["has_classification"] and point_count > 0:
|
|
classification_counts: dict[int, int] = {}
|
|
for chunk in las_file.chunk_iterator(500_000):
|
|
values, counts = np.unique(
|
|
np.asarray(chunk.classification, dtype=np.uint8),
|
|
return_counts=True,
|
|
)
|
|
for value, count in zip(values.tolist(), counts.tolist(), strict=True):
|
|
classification_counts[value] = classification_counts.get(value, 0) + count
|
|
metadata["classification_summary"] = {
|
|
str(key): value for key, value in sorted(classification_counts.items())
|
|
}
|
|
|
|
return metadata
|
|
|
|
|
|
def analyze_prj_metadata(path: str | Path) -> dict[str, Any]:
|
|
"""PRJ WKT에서 좌표계 식별자와 명칭을 추출한다."""
|
|
source = Path(path)
|
|
text = source.read_text(encoding="utf-8", errors="replace").strip()
|
|
metadata: dict[str, Any] = {
|
|
"file": source.name,
|
|
"text_preview": text[:500],
|
|
"epsg": None,
|
|
"name": None,
|
|
"authority": None,
|
|
"is_valid": False,
|
|
}
|
|
if not text:
|
|
metadata["error"] = "PRJ 파일이 비어 있습니다."
|
|
return metadata
|
|
|
|
parse_text, custom_authority_codes = _prepare_prj_wkt(text)
|
|
try:
|
|
crs = CRS.from_wkt(parse_text)
|
|
except Exception as exc:
|
|
metadata["error"] = str(exc)
|
|
return metadata
|
|
|
|
crs_metadata = normalize_crs_metadata(crs)
|
|
_log_crs_status(source, crs_metadata)
|
|
metadata.update(
|
|
{
|
|
**crs_metadata,
|
|
"name": crs.name,
|
|
"authority": crs.to_authority(),
|
|
"custom_authority_codes": custom_authority_codes,
|
|
"is_valid": True,
|
|
}
|
|
)
|
|
return metadata
|
|
|
|
|
|
def analyze_tfw_metadata(path: str | Path) -> dict[str, Any]:
|
|
"""TFW의 affine 변환 계수와 유효성을 분석한다."""
|
|
source = Path(path)
|
|
values = [
|
|
float(line.strip())
|
|
for line in source.read_text(encoding="utf-8", errors="replace").splitlines()
|
|
if line.strip()
|
|
]
|
|
if any(not math.isfinite(value) for value in values):
|
|
raise ValueError("TFW 변환 계수는 유한한 숫자여야 합니다.")
|
|
|
|
return {
|
|
"file": source.name,
|
|
"values": values,
|
|
"pixel_size_x": values[0] if len(values) > 0 else None,
|
|
"rotation_y": values[1] if len(values) > 1 else None,
|
|
"rotation_x": values[2] if len(values) > 2 else None,
|
|
"pixel_size_y": values[3] if len(values) > 3 else None,
|
|
"origin_x": values[4] if len(values) > 4 else None,
|
|
"origin_y": values[5] if len(values) > 5 else None,
|
|
"is_valid": len(values) == 6,
|
|
}
|
|
|
|
|
|
def analyze_tif_metadata(path: str | Path) -> dict[str, Any]:
|
|
"""TIF/GeoTIFF 데이터셋의 공간 및 밴드 메타데이터를 분석한다."""
|
|
source = Path(path)
|
|
rasterio_logger = logging.getLogger("rasterio._env")
|
|
warning_filter = _CustomVerticalCrsWarningFilter(source)
|
|
rasterio_logger.addFilter(warning_filter)
|
|
try:
|
|
with rasterio.open(source) as dataset:
|
|
crs = dataset.crs
|
|
crs_metadata = normalize_crs_metadata(crs)
|
|
_log_crs_status(source, crs_metadata)
|
|
bounds = dataset.bounds
|
|
return {
|
|
"file": source.name,
|
|
"width": int(dataset.width),
|
|
"height": int(dataset.height),
|
|
"count": int(dataset.count),
|
|
"dtypes": list(dataset.dtypes),
|
|
"nodata": float(dataset.nodata) if dataset.nodata is not None else None,
|
|
**crs_metadata,
|
|
"bounds": {
|
|
"left": float(bounds.left),
|
|
"bottom": float(bounds.bottom),
|
|
"right": float(bounds.right),
|
|
"top": float(bounds.top),
|
|
},
|
|
"transform": [float(value) for value in list(dataset.transform)[:6]],
|
|
"resolution": [float(value) for value in dataset.res],
|
|
"likely_type": "dem" if dataset.count == 1 else "image",
|
|
}
|
|
finally:
|
|
rasterio_logger.removeFilter(warning_filter)
|
|
|
|
|
|
_PLANNED_ROUTE_COLUMNS = ("route_name", "sequence", "x", "y", "z", "crs_epsg")
|
|
|
|
|
|
def _parse_route_integer(value: str, *, field: str, row_number: int) -> int:
|
|
normalized = value.strip()
|
|
if not re.fullmatch(r"[0-9]+", normalized):
|
|
raise ValueError(f"CSV {row_number}행의 {field} 값은 양의 정수여야 합니다.")
|
|
parsed = int(normalized)
|
|
if parsed <= 0:
|
|
raise ValueError(f"CSV {row_number}행의 {field} 값은 양의 정수여야 합니다.")
|
|
return parsed
|
|
|
|
|
|
def _parse_route_coordinate(value: str, *, field: str, row_number: int) -> float:
|
|
try:
|
|
parsed = float(value.strip())
|
|
except (AttributeError, ValueError) as exc:
|
|
raise ValueError(f"CSV {row_number}행의 {field} 값은 숫자여야 합니다.") from exc
|
|
if not math.isfinite(parsed):
|
|
raise ValueError(f"CSV {row_number}행의 {field} 값은 유한한 숫자여야 합니다.")
|
|
return parsed
|
|
|
|
|
|
def analyze_planned_route_csv(path: str | Path) -> dict[str, Any]:
|
|
"""원청 계획노선 CSV를 검증하고 경로 메타데이터를 반환한다."""
|
|
source = Path(path)
|
|
with source.open("r", encoding="utf-8-sig", newline="") as csv_file:
|
|
reader = csv.DictReader(csv_file)
|
|
if reader.fieldnames is None:
|
|
raise ValueError("계획노선 CSV 헤더를 찾을 수 없습니다.")
|
|
|
|
normalized_headers = [header.strip() for header in reader.fieldnames]
|
|
if len(set(normalized_headers)) != len(normalized_headers):
|
|
raise ValueError("계획노선 CSV 헤더에 중복된 열이 있습니다.")
|
|
header_map = dict(zip(normalized_headers, reader.fieldnames, strict=True))
|
|
missing = [column for column in _PLANNED_ROUTE_COLUMNS if column not in header_map]
|
|
if missing:
|
|
raise ValueError(f"계획노선 CSV 필수 열이 없습니다: {', '.join(missing)}")
|
|
|
|
route_name: str | None = None
|
|
crs_epsg: int | None = None
|
|
points: list[tuple[float, float, float]] = []
|
|
for expected_sequence, row in enumerate(reader, start=1):
|
|
row_number = expected_sequence + 1
|
|
current_name = (row.get(header_map["route_name"]) or "").strip()
|
|
if not current_name:
|
|
raise ValueError(f"CSV {row_number}행의 route_name 값이 비어 있습니다.")
|
|
if route_name is None:
|
|
route_name = current_name
|
|
elif current_name != route_name:
|
|
raise ValueError("계획노선 CSV에는 하나의 route_name만 사용할 수 있습니다.")
|
|
|
|
sequence = _parse_route_integer(
|
|
row.get(header_map["sequence"]) or "",
|
|
field="sequence",
|
|
row_number=row_number,
|
|
)
|
|
if sequence != expected_sequence:
|
|
raise ValueError(
|
|
f"CSV {row_number}행의 sequence는 {expected_sequence}이어야 합니다."
|
|
)
|
|
|
|
current_epsg = _parse_route_integer(
|
|
row.get(header_map["crs_epsg"]) or "",
|
|
field="crs_epsg",
|
|
row_number=row_number,
|
|
)
|
|
if crs_epsg is None:
|
|
crs_epsg = current_epsg
|
|
elif current_epsg != crs_epsg:
|
|
raise ValueError("계획노선 CSV의 crs_epsg는 모든 행에서 같아야 합니다.")
|
|
|
|
points.append(
|
|
tuple(
|
|
_parse_route_coordinate(
|
|
row.get(header_map[field]) or "",
|
|
field=field,
|
|
row_number=row_number,
|
|
)
|
|
for field in ("x", "y", "z")
|
|
)
|
|
)
|
|
|
|
if len(points) < 2:
|
|
raise ValueError("계획노선 CSV에는 좌표가 2개 이상 있어야 합니다.")
|
|
|
|
xs, ys, zs = zip(*points, strict=True)
|
|
return {
|
|
"file": source.name,
|
|
"extension": "csv",
|
|
"size_bytes": source.stat().st_size,
|
|
"purpose": "planned_route",
|
|
"route_name": route_name,
|
|
"point_count": len(points),
|
|
"epsg": crs_epsg,
|
|
"columns": list(_PLANNED_ROUTE_COLUMNS),
|
|
"bounds": {
|
|
"x_min": min(xs),
|
|
"x_max": max(xs),
|
|
"y_min": min(ys),
|
|
"y_max": max(ys),
|
|
"z_min": min(zs),
|
|
"z_max": max(zs),
|
|
},
|
|
"start_point": list(points[0]),
|
|
"end_point": list(points[-1]),
|
|
}
|
|
|
|
|
|
def analyze_input_metadata(path: str | Path) -> dict[str, Any]:
|
|
"""입력 파일 확장자에 맞는 B03 메타데이터 분석 함수를 호출한다."""
|
|
source = Path(path)
|
|
extension = source.suffix.lower()
|
|
if extension == ".csv":
|
|
return analyze_planned_route_csv(source)
|
|
if extension in {".las", ".laz"}:
|
|
return analyze_las_metadata(source)
|
|
if extension == ".prj":
|
|
return analyze_prj_metadata(source)
|
|
if extension == ".tfw":
|
|
return analyze_tfw_metadata(source)
|
|
if extension in {".tif", ".tiff"}:
|
|
return analyze_tif_metadata(source)
|
|
return {
|
|
"file": source.name,
|
|
"extension": extension.lstrip("."),
|
|
"size_bytes": source.stat().st_size,
|
|
}
|