Files
kadastr-17/services/mif_processor.py
2026-03-25 13:38:54 +05:00

73 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Извлечение контуров из MIF-файлов и формирование строк координат."""
import sys
from pathlib import Path
# read_mif_file лежит в корне проекта — добавляем его в путь поиска
_PROJECT_ROOT = str(Path(__file__).parent.parent)
if _PROJECT_ROOT not in sys.path:
sys.path.insert(0, _PROJECT_ROOT)
from read_mif_file import parse_mif # noqa: E402
def get_contours(mif_path: str) -> list:
"""
Извлечь список контуров (Region) из MIF-файла.
Возвращает: [[(x, y), ...], ...] — список контуров.
Region может содержать несколько полигонов (contours).
"""
result = parse_mif(mif_path)
if isinstance(result, list):
return []
all_contours = []
for feat in result.features:
if feat["type"] == "Region":
# contours — список полигонов (исправленный парсер)
for poly in feat["props"].get("contours", []):
if poly:
all_contours.append(list(poly))
return all_contours
def build_coord_rows(contours: list, method: str, mt) -> list:
"""
Сформировать список строк для раздела координат.
Каждый элемент: (номер, X_str, Y_str, method, mt)
или None — пустой разделитель между контурами.
Правила:
- Первая координата MIF → Y (колонка D), вторая → X (C).
- Все точки нумеруются подряд по всем контурам.
- Последняя (замыкающая) точка контура получает номер первой точки этого контура.
"""
rows = []
global_num = 1
for ci, contour in enumerate(contours):
if ci > 0:
rows.append(None)
if not contour:
continue
# Single-point contour — добавляем одну строку
if len(contour) == 1:
my, mx = contour[0] # MIF: первая=Y(восток), вторая=X(север)
rows.append((global_num, f"{mx:.2f}", f"{my:.2f}", method, mt))
global_num += 1
continue
# Для многоточечных контуров: нумеруем все точки, кроме последней,
# затем добавляем завершающую точку, которой присваивается номер
# первой точки контура (замыкание).
first_num = global_num
for my, mx in contour[:-1]: # MIF: первая=Y(восток), вторая=X(север)
rows.append((global_num, f"{mx:.2f}", f"{my:.2f}", method, mt))
global_num += 1
if len(contour) >= 2:
my, mx = contour[-1] # MIF: первая=Y(восток), вторая=X(север)
rows.append((first_num, f"{mx:.2f}", f"{my:.2f}", method, mt))
return rows