Files
Aislo/B05_Profile/B05_Profile_Engine_Sections.py
T
eomsangdonandClaude Opus 5 e1d0d6dda3 feat(B05): 계획 종단선이 횡단배수 최소고를 자동 확보
계획선의 변화점(PVI)은 배수유역이 확정한 배관 배치 측점이다. 그 자리 계획고를
지반고와 같게 두면 관·구체가 들어갈 자리가 없어, 시설 제원만큼 들어 올린다.

- common_util_drainage_pipes: facility_clearance_m()/pipe_anchor_clearances() 신설
  (정본 산식) — 배수관 관경+토피 0.5, BOX암거 구체높이+토피 0.5,
  물넘이·세월교는 좌측 패널이 설계유량으로 산출한 월류 높이(ford_height_m)
- Engine_Sections: 관 지점에서 시설 여유를 함께 읽어(_load_pipe_anchors) 전달
- Engine_Grade_Profile: 앵커 목표 표고 = 지반고 + 시설 여유
- Profile_MinCover(화면 경고): 세월교·물넘이를 월류 높이 기준으로 추가.
  백엔드가 정본이고 이쪽은 편집 중 즉시 경고용 사본 — 값 일치는 테스트로 잠금

검증: 재생성한 계획선의 배관 측점 실측 — Ø1000 +1.50 / 세월교 +0.30 /
BOX +2.50 / Ø800 +1.30 (전부 요구 여유와 일치), 화면 경고 소거 확인.
pytest 10/10, tsc·ruff 통과

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-23 14:12:38 +09:00

388 lines
16 KiB
Python

"""B05 경로 계산 후 종횡단을 생성하는 엔진 오케스트레이터.
확정 경로 GeoJSON과 확정 지표면 모델 sampler로 종단·횡단을 생성하고, 종단은
longitudinal/, 각 횡단은 cross_sections/ 아래 파일로 저장한다. DB 기록용
데이터(경로·요약·측점별 파일)를 준비해 반환한다. 라우터에서 asyncio.to_thread로
호출한다.
"""
import json
import logging
from pathlib import Path
from typing import Any
from B05_Profile.B05_Profile_Engine_Grade import GradeDesignOptions, design_grade_line
from B05_Profile.B05_Profile_Engine_Grade_Profile import (
design_alignment_profile,
design_pipe_anchored_profile,
)
from B05_Profile.B05_Profile_Engine_Sections_Core import (
SectionGenerationOptions,
generate_sections,
)
from common_util.common_util_drainage_pipes import (
parse_pipe_points,
pipe_anchor_clearances,
route_signature,
)
from common_util.common_util_json import atomic_write_json
from common_util.common_util_route_geometry import RouteVertex
from common_util.common_util_surface_sampler import build_surface_sampler
from config.config_system import (
DRAINAGE_CACHE_DIRNAME,
DRAINAGE_EDITS_DIRNAME,
DRAINAGE_PIPE_POINTS_FILENAME,
FOREST_ROAD_PROFILE_CRITERIA,
)
logger = logging.getLogger(__name__)
_STAGE_SUBDIR = Path("B06_Section")
_MODELS_SUBDIR = Path("B04_PreProcess") / "models"
def _load_route_polyline(project_root: Path, route_data_path: str) -> list[list[float]]:
"""B05가 저장한 경로 GeoJSON에서 3D 폴리라인 좌표열을 로드한다."""
geojson_path = project_root / Path(route_data_path)
if not geojson_path.is_file():
raise FileNotFoundError(f"경로 GeoJSON을 찾을 수 없습니다: {route_data_path}")
geo = json.loads(geojson_path.read_text(encoding="utf-8"))
coords = geo.get("geometry", {}).get("coordinates")
if not coords or len(coords) < 2:
raise ValueError("경로 GeoJSON에 유효한 LineString 좌표가 없습니다.")
return [[float(c[0]), float(c[1]), float(c[2]) if len(c) > 2 else 0.0] for c in coords]
def cross_filename(chainage_m: float) -> str:
"""측점 chainage에 대응하는 횡단면 파일명(단일 규칙)."""
return f"cross_{int(round(float(chainage_m))):05d}m.json"
def prune_stale_cross_files(cross_dir: Path, stations: list[Any]) -> set[str]:
"""stations에 없는 잔재 cross_*.json을 삭제하고 유효 파일명 집합을 반환한다.
측점 정보가 비어 있으면 오삭제를 피하기 위해 아무것도 지우지 않고
빈 집합을 반환한다(호출부는 빈 집합이면 필터를 생략한다).
"""
valid = {
cross_filename(station["chainage_m"])
for station in stations
if isinstance(station, dict) and isinstance(station.get("chainage_m"), (int, float))
}
if not valid:
return valid
for path in cross_dir.glob("cross_*.json"):
if path.name not in valid:
path.unlink(missing_ok=True)
return valid
def _cross_summary(cross_section: dict[str, Any]) -> dict[str, Any]:
"""횡단면 상세에서 DB data 컬럼에 저장할 요약을 만든다."""
samples = cross_section.get("samples", [])
valid_z = [
s["elevation_m"] for s in samples if s.get("valid") and s.get("elevation_m") is not None
]
return {
"chainage_m": cross_section.get("chainage_m"),
"center_z": cross_section.get("center_z"),
"azimuth_deg": cross_section.get("azimuth_deg"),
"sample_count": len(samples),
"min_elevation_m": min(valid_z) if valid_z else None,
"max_elevation_m": max(valid_z) if valid_z else None,
}
def _load_pipe_anchors(
project_root: Path, polyline: list[list[float]]
) -> tuple[list[float], dict[float, float]]:
"""배수유역도가 확정한 배관 배치 측점(누가거리)을 읽는다.
관 지점 파일에는 저장 당시 노선 지문이 함께 있다 — 노선이 바뀌었으면 버린다
(옛 노선의 배관 자리로 계획선을 앉히면 전부 어긋난다). 파일이 없거나 못 읽으면 빈 목록.
"""
path = (
project_root
/ "B04_PreProcess"
/ DRAINAGE_CACHE_DIRNAME
/ DRAINAGE_EDITS_DIRNAME
/ DRAINAGE_PIPE_POINTS_FILENAME
)
if not path.is_file():
return [], {}
try:
document = json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
logger.warning("B05 계획선: 관 지점 파일을 읽지 못했습니다 (%s)", path)
return [], {}
vertices = [
RouteVertex(
x=float(p[0]), y=float(p[1]), z=float(p[2]) if len(p) > 2 else 0.0, chainage_m=0.0
)
for p in polyline
]
if str(document.get("route_signature") or "") != route_signature(vertices):
logger.info("B05 계획선: 노선이 바뀌어 저장된 관 지점을 쓰지 않습니다.")
return [], {}
points = parse_pipe_points(document.get("points"))
# 시설 제원이 요구하는 최소 여유(관경+토피 등)를 함께 넘긴다 — 계획선이 그만큼
# 들려야 관·구체가 들어갈 자리가 생긴다(2026-08-23 사용자 지시).
clearances = {chainage: clearance for chainage, clearance in pipe_anchor_clearances(points)}
return [pipe.chainage_m for pipe in points], clearances
def _append_design_profiles(
longitudinal: dict[str, Any],
grade_options: GradeDesignOptions | None,
station_interval_m: float | None = None,
pipe_chainages: list[float] | None = None,
pipe_clearances: dict[float, float] | None = None,
) -> dict[str, Any] | None:
"""종단 계획선을 산출해 longitudinal에 붙이고 요약을 반환한다.
1순위는 **배관 정착 선형** — 배수유역도의 배관 배치 측점마다 계획선이 지면선과
만나도록 직선으로 잇고 기본 R을 얹는다(2026-08-03 사용자 확정). 배관이 없거나
산출이 불가하면 2순위로 기존 지반 추종 직선 분할 선형(`design_alignment_profile`),
그마저 실패하면 구 균형 최적화 계획선으로 폴백하고(편집 불가), 모두 실패해도
종횡단 생성 자체는 유지한다.
"""
longitudinal.setdefault("design_profiles", [])
if grade_options is None:
return None
alignment = None
profile = None
if pipe_chainages:
try:
alignment, profile = design_pipe_anchored_profile(
longitudinal,
grade_options,
pipe_chainages,
station_interval_m=station_interval_m,
pipe_clearances=pipe_clearances,
)
except (ValueError, KeyError, ArithmeticError):
logger.exception("B05 배관 정착 계획선 산출 실패 — 직선 분할 선형으로 대체")
if profile is None:
try:
alignment, profile = design_alignment_profile(
longitudinal, grade_options, station_interval_m=station_interval_m
)
except (ValueError, KeyError, ArithmeticError):
logger.exception("B05 계획선 선형 산출 실패 — 균형 최적화 계획선으로 대체")
try:
profile = design_grade_line(longitudinal, grade_options)
except (ValueError, KeyError, ArithmeticError):
logger.exception("B05 종단 계획선 산출 실패 (종횡단은 유지)")
return None
if alignment is not None:
longitudinal["profile_alignment"] = alignment
longitudinal["design_profiles"].append(profile)
return {"id": profile["id"], **profile["summary"]}
def _annotate_pavement_suggestions(
longitudinal: dict[str, Any], grade_options: GradeDesignOptions | None
) -> None:
"""계획선 국소 종단경사가 비포장 법정 상한을 넘는 측점에 포장 제안을 표기한다.
근거: 「임도설치 및 관리 등에 관한 규정」[별표 1-2] — 기준(설계속도×지형) 초과
구간은 노면포장 시에 한해 상한(`paved_exception_grade_pct`)까지 허용된다.
stations에 `pavement_suggested`(bool)와 판정 근거(경사·상한)를 남기며, B06이
포장 기본값과 법정 근거 문구 표기에 그대로 사용한다. 실패해도 종횡단은 유지한다.
"""
if grade_options is None:
return
profiles = longitudinal.get("design_profiles") or []
samples = profiles[0].get("samples", []) if profiles else []
points = [
(float(s["chainage_m"]), float(s["elevation_m"]))
for s in samples
if isinstance(s.get("chainage_m"), (int, float))
and isinstance(s.get("elevation_m"), (int, float))
]
stations = longitudinal.get("stations")
if len(points) < 2 or not isinstance(stations, list):
return
criteria = FOREST_ROAD_PROFILE_CRITERIA["design_speed"].get(grade_options.design_speed_kph)
if not criteria:
return
terrain = grade_options.terrain_type if grade_options.terrain_type else "normal"
unpaved_limit = float(
criteria["max_grade_pct"].get(terrain, criteria["max_grade_pct"]["normal"])
)
def local_grade_pct(chainage: float) -> float:
"""측점을 감싸는 인접 계획선 구간들의 경사 중 최댓값(절댓값 %)을 돌려준다."""
worst = 0.0
for index in range(1, len(points)):
c0, z0 = points[index - 1]
c1, z1 = points[index]
if c1 < chainage - 1e-6 or c0 > chainage + 1e-6:
continue
span = c1 - c0
if span <= 1e-9:
continue
worst = max(worst, abs((z1 - z0) / span) * 100.0)
return worst
for station in stations:
chainage = station.get("chainage_m")
if not isinstance(chainage, (int, float)):
continue
grade_pct = local_grade_pct(float(chainage))
station["pavement_suggested"] = grade_pct > unpaved_limit + 1e-6
station["pavement_grade_pct"] = round(grade_pct, 2)
station["pavement_grade_limit_pct"] = round(unpaved_limit, 2)
def run_section_generation(
project_root: Path,
route_data_path: str,
filter_key: str,
method: str,
smooth: bool,
*,
options: SectionGenerationOptions | None = None,
grade_options: GradeDesignOptions | None = None,
grade_overrides: dict[str, Any] | None = None,
crs: str | None = None,
) -> dict[str, Any]:
"""종횡단을 생성·저장하고 DB 기록용 데이터를 반환한다.
반환 dict:
- longitudinal: {file_path, data(요약)}
- cross_sections: [{chainage_m, sequence_num, data(요약), cross_section_file_path}]
- result: generate_sections 원본 결과
"""
polyline = _load_route_polyline(project_root, route_data_path)
models_dir = project_root / _MODELS_SUBDIR
sampler = build_surface_sampler(models_dir, filter_key, method, smooth)
result = generate_sections(
polyline,
sampler,
options,
source_snapshot={"filter": filter_key, "method": method, "smooth": smooth},
crs=crs,
)
stage_root = project_root / _STAGE_SUBDIR
long_dir = stage_root / "longitudinal"
cross_dir = stage_root / "cross_sections"
long_dir.mkdir(parents=True, exist_ok=True)
cross_dir.mkdir(parents=True, exist_ok=True)
# 종단면 저장 (계획선은 저장 직전에 종단 데이터에 붙인다)
pipe_anchors = _load_pipe_anchors(project_root, polyline)
grade_summary = _append_design_profiles(
result["longitudinal"],
grade_options,
(result.get("options") or {}).get("station_interval_m"),
# 1차 계획선의 변화점 = 배수유역도가 확정한 배관 배치 측점.
pipe_chainages=pipe_anchors[0],
pipe_clearances=pipe_anchors[1],
)
# 계획선 경사 기반 포장 제안(법정 상한 초과 측점) — 비치명적.
try:
_annotate_pavement_suggestions(result["longitudinal"], grade_options)
except Exception:
logger.exception("B05 포장 제안 판정 실패 (종횡단은 유지)")
long_file = long_dir / "longitudinal.json"
atomic_write_json(long_file, result["longitudinal"])
long_summary = {
"length_m": result["longitudinal"]["length_m"],
"station_count": result["summary"]["station_count"],
"invalid_samples": result["summary"]["invalid_longitudinal_samples"],
# 사용자 선택값의 단일 소스(DB): 재생성·재탐색 시 이 값을 우선 사용한다.
"options": result["options"],
# 해석이 끝난 기준값은 기록·표시용으로만 보관한다. 이 값을 재계산 시
# 폴백으로 되쓰면 등급·지형을 바꿔도 옛 기본값이 법정값을 이겨 갱신되지 않는다.
"grade_options": grade_options.as_dict() if grade_options else None,
# 재계산 폴백에 쓰는 단일 소스: 사용자가 명시적으로 입력한 값만 담는다.
"grade_overrides": {
key: value for key, value in (grade_overrides or {}).items() if value is not None
},
"grade_summary": grade_summary,
}
# 측점별 횡단면 저장 (detail 조회가 폴더 전체를 glob하므로 이전 실행 잔재를 먼저 비운다)
for stale_file in cross_dir.glob("cross_*.json"):
stale_file.unlink()
cross_records: list[dict[str, Any]] = []
for seq, cross_section in enumerate(result["cross_sections"]):
chainage = float(cross_section["chainage_m"])
cross_file = cross_dir / cross_filename(chainage)
atomic_write_json(cross_file, cross_section)
cross_records.append(
{
"chainage_m": chainage,
"sequence_num": seq,
"data": _cross_summary(cross_section),
"cross_section_file_path": cross_file.relative_to(project_root).as_posix(),
}
)
return {
"longitudinal": {
"file_path": long_file.relative_to(project_root).as_posix(),
"data": long_summary,
},
"cross_sections": cross_records,
"result": result,
}
def generate_irregular_sections(
project_root: Path,
route_data_path: str,
filter_key: str,
method: str,
smooth: bool,
*,
extra_stations: tuple[tuple[float, str], ...],
options: SectionGenerationOptions | None = None,
crs: str | None = None,
) -> list[dict[str, Any]]:
"""비정규 측점만 샘플링해 **횡단 파일을 쓰고** 측점 dict 목록을 돌려준다.
기존 종단 계획선·규칙 측점·횡단은 건드리지 않는다. 격자 측점과 똑같은 접선·오프셋
샘플링을 거치되(`generate_sections` 재사용) kind="irregular"인 것만 골라내 각각
`cross_*.json`으로 저장한다. B06 상세 조회가 이 폴더를 glob하고 종단 파일의 stations로
필터링하므로, 이 파일들 + 종단 stations 병합만으로 다음 페이지에 횡단이 나타난다(DB 불필요).
"""
if not extra_stations:
return []
base = options or SectionGenerationOptions()
merged_options = SectionGenerationOptions(
station_interval_m=base.station_interval_m,
cross_half_width_m=base.cross_half_width_m,
cross_sample_interval_m=base.cross_sample_interval_m,
long_sample_interval_m=base.long_sample_interval_m,
include_endpoint=base.include_endpoint,
extra_stations=tuple(extra_stations),
)
polyline = _load_route_polyline(project_root, route_data_path)
sampler = build_surface_sampler(project_root / _MODELS_SUBDIR, filter_key, method, smooth)
result = generate_sections(
polyline,
sampler,
merged_options,
source_snapshot={"filter": filter_key, "method": method, "smooth": smooth},
crs=crs,
)
irregular_stations = [
station
for station in result["longitudinal"]["stations"]
if station.get("kind") == "irregular"
]
if not irregular_stations:
return []
cross_dir = project_root / _STAGE_SUBDIR / "cross_sections"
cross_dir.mkdir(parents=True, exist_ok=True)
for cross_section in result["cross_sections"]:
if cross_section.get("kind") != "irregular":
continue
cross_file = cross_dir / cross_filename(float(cross_section["chainage_m"]))
atomic_write_json(cross_file, cross_section)
return irregular_stations