Files
Aislo/B05_Profile/B05_Profile_Plus_Stations.py
T
eomsangdonandClaude Opus 5.5 b0e38de44c refactor(476-1 ②): 측점 · 1차 계획선 셈과 파일 쓰기 나눔(동작 같음)
- Engine_Sections `build_sections`(측점 틀 · 지반 · 438 1차 계획선 · 포장 제안 · 파일 안 씀) · run_section_generation = 읽기 → 셈 → 쓰기
- 횡단 이름 규칙 → 새 `B05_Profile_Engine_Sections_Names`(700줄 · 다시 내보냄)
- resolve_extra_stations 사본 칸(structures · plus) · 따라감 셈 몫 `followed_structures` · `followed_plus_rows` · `followed_pipes` · `section_seed` · `seat_base_pvi` 뗌
- 체인 · 추천 초기 종단이 같은 조각을 부를 자리

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 16:36:26 +09:00

101 lines
4.0 KiB
Python

"""추가 측점 정본(`B05_Profile/route/plus_stations.json`) — 구조물 정본 곁(368-2 · 요구 240-21).
사용자가 종단 원지반선에서 넣은 일반 측점(정수 m) · 이름 없음. 자리 정본 = 좌표(x · y) — 노선이
바뀌면 관 · 구조물처럼 새 노선에 투영해 누가거리를 다시 매김(TS `followPlusRows` · Node 입구).
측점 계획에는 `resolve_extra_stations` → `plan_stations(plus=)` 로 들어감(TS `planStations` 짝).
"""
from __future__ import annotations
import json
import os
from pathlib import Path
from typing import Any, Sequence
from common_util.common_util_calc_runner import run_calc
from common_util.common_util_json import atomic_write_json
PLUS_STATIONS_FILE_NAME = "plus_stations.json"
def plus_stations_path(project_root: str | Path) -> Path:
return Path(project_root) / "B05_Profile" / "route" / PLUS_STATIONS_FILE_NAME
def load_plus_stations(project_root: str | Path) -> list[dict[str, Any]]:
"""[{chainage_m, x, y}] — 없거나 깨졌으면 빈 목록."""
path = plus_stations_path(project_root)
if not path.is_file():
return []
try:
rows = json.loads(path.read_text(encoding="utf-8")).get("stations") or []
except (OSError, ValueError, AttributeError):
return []
return [
row
for row in rows
if isinstance(row, dict) and isinstance(row.get("chainage_m"), (int, float))
]
def plus_chainages(project_root: str | Path) -> list[float]:
return sorted({float(row["chainage_m"]) for row in load_plus_stations(project_root)})
def save_plus_stations(
project_root: str | Path, chainages: Sequence[float], xy: Sequence[Sequence[float]] | None
) -> list[float]:
"""정수 m 로 맞춰(0 ~ 노선 끝 · 같은 m 하나) 좌표와 함께 씀 → 쓴 누가거리."""
from shapely.geometry import LineString
line = LineString([(float(p[0]), float(p[1])) for p in xy]) if xy and len(xy) > 1 else None
end = float(line.length) if line is not None else float("inf")
values = sorted({float(round(c)) for c in chainages if 0 <= round(c) <= end})
rows = []
for value in values:
point = line.interpolate(value) if line is not None else None
rows.append(
{
"chainage_m": value,
"x": round(float(point.x), 4) if point is not None else None,
"y": round(float(point.y), 4) if point is not None else None,
}
)
path = plus_stations_path(project_root)
if not rows and not path.exists():
return []
os.makedirs(path.parent, exist_ok=True)
atomic_write_json(str(path), {"stations": rows})
return values
def follow_plus_stations(project_root: str | Path, new_xy: Sequence[Sequence[float]]) -> int:
"""노선이 바뀔 때 좌표를 새 노선에 투영해 누가거리를 다시 매김 → 옮긴 수(같은 m 는 하나).
셈 = TS `followPlusRows`(Node 입구 `facility.plus_follow` · 472-4 ①)."""
rows = load_plus_stations(project_root)
if not rows:
return 0
followed = followed_plus_rows(rows, new_xy)
if followed["moved"]:
atomic_write_json(str(plus_stations_path(project_root)), {"stations": followed["rows"]})
return int(followed["moved"])
def followed_plus_rows(rows: list[dict[str, Any]], new_xy: Sequence[Sequence[float]]) -> dict:
"""추가 측점을 새 노선에 투영(저장 안 함) → `{rows, moved}` — 체인 · 추천 초기 종단(476)."""
results = run_calc(
["facility.plus_follow"],
{
"plus": {
"rows": [
{"chainage_m": float(r["chainage_m"]), "x": r.get("x"), "y": r.get("y")}
for r in rows
],
"line": [[float(p[0]), float(p[1])] for p in new_xy],
}
},
)
if results is None:
raise RuntimeError("추가 측점 따라감 실패 — 계산 Node 입구(facility.plus_follow)")
return results["facility.plus_follow"]