Files
planet/scripts/build_earth_boundary_policy_geometry.py
rayd1o 5bfc790dec
Some checks failed
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.75.0
2026-09-23 21:20:12 +08:00

80 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Extract reviewed Natural Earth China-POV geometries by stable feature ID."""
from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
SOURCE_COMMIT = "ca96624a56bd078437bca8184e78163e5039ad19"
COUNTRY_IDS = {
"CHN": 1159320471, "IND": 1159320847, "JPN": 1159320937,
"SRB": 1159321267, "PSE": 1159320899,
}
DISPUTE_IDS = {
"china-diaoyu-chiwei": 1159320959,
"china-paracels": 1159320477,
"china-spratlys": 1159321165,
"china-huangyan": 1159321227,
}
def select_feature(features: list[dict], ne_id: int) -> dict:
matches = [feature for feature in features if feature["properties"].get("NE_ID") == ne_id]
if len(matches) != 1 or matches[0]["geometry"]["type"] not in {"Polygon", "MultiPolygon"}:
raise ValueError(f"expected one audited polygon for NE_ID {ne_id}")
return matches[0]
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--china-pov-source", type=Path, required=True)
parser.add_argument("--disputed-source", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
country_bytes = args.china_pov_source.read_bytes()
disputed_bytes = args.disputed_source.read_bytes()
country_features = json.loads(country_bytes)["features"]
disputed_features = json.loads(disputed_bytes)["features"]
payload = {
"schema": "planet-earth-boundary-reviewed-geometry/v1",
"profile": "china-pov-v1",
"sourceCommit": SOURCE_COMMIT,
"sources": [
{
"url": f"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/{SOURCE_COMMIT}/geojson/ne_10m_admin_0_countries_chn.geojson",
"sha256": hashlib.sha256(country_bytes).hexdigest(),
},
{
"url": f"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/{SOURCE_COMMIT}/geojson/ne_10m_admin_0_disputed_areas.geojson",
"sha256": hashlib.sha256(disputed_bytes).hexdigest(),
},
],
"countryGeometries": {
code: select_feature(country_features, ne_id)["geometry"]
for code, ne_id in COUNTRY_IDS.items()
},
"countryFeatureIds": COUNTRY_IDS,
"disputedGeometries": {
rule: select_feature(disputed_features, ne_id)["geometry"]
for rule, ne_id in DISPUTE_IDS.items()
},
"disputedFeatureIds": DISPUTE_IDS,
"limitations": [
"Natural Earth China POV is not a Chinese government-authored administrative boundary.",
"Only named Natural Earth feature geometries are included; Dongsha, Zhongsha, Zengmu Ansha and the maritime claim line are still unverified.",
],
}
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(
json.dumps(payload, ensure_ascii=False, separators=(",", ":")) + "\n",
encoding="utf-8",
)
if __name__ == "__main__":
main()