2024-12-06 12:04:07 -05:00
|
|
|
import json
|
|
|
|
from collections import defaultdict
|
|
|
|
|
2024-12-06 14:06:47 -05:00
|
|
|
input_filename = "../data/cerc_cmm_corrected.geojson"
|
2024-12-06 12:04:07 -05:00
|
|
|
|
|
|
|
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.")
|