/
Seler1ty
/
DataSet_Pipeline
Обзор
Документация
Войти
/
Seler1ty
/
DataSet_Pipeline
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
json_to_csv.py
65 строк
2 KB
Seler1ty
-m create main.py json_to_csv.py, area.py, population.py
11 дек 2025, 11:33
11 дек 2025, 11:33
dcb73d9
Код
Авторство
О чём код?
import json import csv def geojson_to_csv(filename): """Конвертирует GeoJSON в CSV с тем же именем""" input_file = f"{filename}.geojson" output_file = f"{filename}.csv" print(f"Конвертация {input_file} -> {output_file}") try: with open(input_file, 'r', encoding='utf-8') as f: data = json.load(f) with open(output_file, 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) writer.writerow(['id', 'lat', 'lon', 'coordinates']) for feature in data['features']: feature_id = feature.get('id', 'unknown') try: # Получаем координаты coords = feature['geometry']['coordinates'] # Рекурсивно извлекаем точки def extract_points(arr): points = [] for item in arr: if isinstance(item, list): if len(item) == 2: points.append(item) else: points.extend(extract_points(item)) return points points = extract_points(coords) if points: # Вычисляем центр lats = [p[1] for p in points] lons = [p[0] for p in points] center_lat = sum(lats) / len(lats) center_lon = sum(lons) / len(lons) # Формируем координаты в нужном формате bounding_coords = [(p[1], p[0]) for p in points] writer.writerow([ feature_id, round(center_lat, 6), round(center_lon, 6), str(bounding_coords) ]) except Exception: continue print(f"✓ Создан {output_file}") return True except Exception as e: print(f"✗ Ошибка: {e}") return False