Files
geojson_conv/main_conv_reg_to_city.py
2024-11-15 15:03:05 +03:00

56 lines
2.3 KiB
Python
Raw Permalink 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.
import json
# Загрузка данных о регионах из внешнего JSON
def load_region_data(data_file="regions_data.json"):
try:
with open(data_file, "r", encoding="utf-8") as f:
return json.load(f).get("regions", [])
except FileNotFoundError:
print(f"Файл {data_file} не найден.")
return []
# Функция для добавления городов в GeoJSON
def add_cities_to_geojson(input_geojson="russia_2.geojson", output_geojson="updated_russia_2.geojson"):
# Загрузка исходного GeoJSON файла
with open(input_geojson, "r", encoding="utf-8") as f:
geo_data = json.load(f)
# Загрузка данных о регионах
region_data = load_region_data()
region_map = {region["id"]: region for region in region_data}
for feature in geo_data.get("features", []):
# Обработка данных региона
region_id = feature["properties"].get("id")
region_info = region_map.get(region_id, {})
# Добавление информации о главном городе
major_city = region_info.get("major_cities", None)
if major_city:
# Примерные координаты (замените на реальные для каждого города, если они известны)
city_coords = [[-100.0, 60.0]] # Заглушка, замените на реальные координаты для каждого города
city_feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": city_coords
},
"properties": {
"name": major_city,
"region_id": region_id
}
}
# Добавляем город в список фич
geo_data["features"].append(city_feature)
# Запись обновленного GeoJSON файла
with open(output_geojson, "w", encoding="utf-8") as f:
json.dump(geo_data, f, ensure_ascii=False, indent=4)
print(f"Файл обновлен и сохранен как {output_geojson}")
# Запуск функции
add_cities_to_geojson()