test
This commit is contained in:
59
services/mif_processor.py
Normal file
59
services/mif_processor.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""Извлечение контуров из 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 → X (колонка C), вторая → Y (D).
|
||||
- Все точки нумеруются подряд по всем контурам.
|
||||
- Последняя (замыкающая) точка контура получает номер первой точки этого контура.
|
||||
"""
|
||||
rows = []
|
||||
global_num = 1
|
||||
for ci, contour in enumerate(contours):
|
||||
if ci > 0:
|
||||
rows.append(None)
|
||||
if not contour:
|
||||
continue
|
||||
first_num = global_num
|
||||
for mx, my in contour[:-1]:
|
||||
rows.append((global_num, f"{mx:.2f}", f"{my:.2f}", method, mt))
|
||||
global_num += 1
|
||||
if len(contour) >= 2:
|
||||
mx, my = contour[-1]
|
||||
rows.append((first_num, f"{mx:.2f}", f"{my:.2f}", method, mt))
|
||||
return rows
|
||||
Reference in New Issue
Block a user