diff --git a/common_util/common_util_structure_lengths.py b/common_util/common_util_structure_lengths.py index 43c617f8..bb6e45b7 100644 --- a/common_util/common_util_structure_lengths.py +++ b/common_util/common_util_structure_lengths.py @@ -23,14 +23,17 @@ from B05_Profile.B05_Profile_Structures_Schema import structure_type_map PENDING_TYPE_IDS: frozenset[str] = frozenset() -def _merge(spans: list[tuple[float, float]]) -> float: - """겹치는 구간을 합쳐 실제 덮인 길이를 낸다. +def _merge(spans: list[tuple[float, float]]) -> list[tuple[float, float]]: + """겹치는 구간을 **합쳐** 실제 덮인 구간 목록을 낸다. 같은 시설을 겹치게 두 번 넣으면 단순 합은 그 구간을 **두 번 센다**. 연장은 「덮인 길이」라 겹침을 지우는 쪽이 맞다. 원래 합(`raw_length_m`)도 함께 내보내므로 입력이 겹쳤다는 사실은 숨지 않는다. + + 합친 **구간 자체**를 돌려준다 — 길이만 내면 산출근거에 「어디부터 어디까지」를 못 적는다 + (2026-09-08 B08 창 요청). 길이는 부르는 쪽이 이 목록에서 더한다. """ - total = 0.0 + merged: list[tuple[float, float]] = [] current_start: float | None = None current_end = 0.0 for start, end in sorted(spans): @@ -40,11 +43,11 @@ def _merge(spans: list[tuple[float, float]]) -> float: if start <= current_end: current_end = max(current_end, end) continue - total += current_end - current_start + merged.append((current_start, current_end)) current_start, current_end = start, end if current_start is not None: - total += current_end - current_start - return total + merged.append((current_start, current_end)) + return merged def structure_lengths(project_root: str | Path) -> list[dict[str, Any]]: @@ -78,6 +81,7 @@ def structure_lengths(project_root: str | Path) -> list[dict[str, Any]]: rows: list[dict[str, Any]] = [] for type_id, count in counts.items(): entries = spans[type_id] + merged = _merge(entries) rows.append( { "type_id": type_id, @@ -85,9 +89,14 @@ def structure_lengths(project_root: str | Path) -> list[dict[str, Any]]: "name": types[type_id].name, "count": count, # 겹침을 지운 실제 연장 — 수량서에 쓸 값. - "length_m": round(_merge(entries), 2), + "length_m": round(sum(end - start for start, end in merged), 2), # 입력한 구간 길이의 단순 합 — 위와 다르면 구간이 겹쳐 있다는 뜻. "raw_length_m": round(sum(end - start for start, end in entries), 2), + # 겹침을 지운 **구간 목록**(누가거리 m) — 산출근거에 「어디부터 어디까지」를 + # 적는 자리다. 길이는 이 목록의 합과 같다(2026-09-08 B08 창 요청). + "spans": [ + {"start_m": round(start, 2), "end_m": round(end, 2)} for start, end in merged + ], } ) rows.sort(key=lambda row: (row["group"], row["name"]))