hub/scripts/geojson_layers.py

31 lines
963 B
Python
Raw Normal View History

import json
from collections import defaultdict
input_filename = "../data/cerc_cmm_corrected_with_archetype.geojson"
with open(input_filename, "r", encoding="utf-8") as f:
data = json.load(f)
if data.get("type") != "FeatureCollection":
raise ValueError("Input GeoJSON does not have type 'FeatureCollection'.")
features_by_region = defaultdict(list)
for feature in data.get("features", []):
region = feature["properties"].get("region", "unknown_region")
features_by_region[region].append(feature)
for region_name, features in features_by_region.items():
out_data = {
"type": "FeatureCollection",
"crs": data.get("crs", None),
"features": features
}
output_filename = f"output_{region_name}.geojson"
with open(output_filename, "w", encoding="utf-8") as outfile:
json.dump(out_data, outfile, ensure_ascii=False, indent=2)
print(f"Created {output_filename} with {len(features)} features.")