halkyon/apps/learn/tool/maps/japan_prefectures.py
Tizian 「ティツィアーン」 af1dbee509
build: Melos, a Dart check and every path for the single repository
- Melos 8.9 as the root's dev dependency, with scripts across the
  workspace (analyze, test:dart, test:flutter, generate, check) and its
  filters (--diff, --include-dependents); cli_util overridden to 0.5.2
  for it, since Learn's flutter_launcher_icons asks for 0.4 and uses only
  its logger, unchanged in 0.5.
- tool/check.dart in place of tool/check.sh: packages, formatting of our
  own files, analysis, the tools' tests, then every package's tests
  through Melos (--changed-since=<commit> for only what changed and what
  depends on it). tool/pre-push is the hook: the signature check, then
  the checks. CI generates every server's code and runs the same check.
- The images build from the root with a workspace narrowed to what they
  need (tool/workspace_only.sh); compose, the deploy's migration test,
  the snap, the release script, the editor's launch files and Learn's
  asset tools (run from apps/learn) follow the new layout.
- The project's source is named as project-halkyon/halkyon (source link,
  snap, F-Droid listing, release uploads); the AGPL packages carry the
  licence's own text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-22 18:11:04 +02:00

69 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Builds the app's map of Japan's prefectures from Natural Earth.
Natural Earth's vector data is in the public domain. This script downloads
its Admin 1 (states, provinces) layer at 1:10m, release v5.1.2, checks the
download against the hash recorded below, keeps Japan's 47 prefectures, and
reduces them to a compact outline for display (outlines.py): Douglas-Peucker
simplification (0.006 degrees), islands too small to see dropped (each
prefecture keeps its largest), coordinates rounded to 4 decimals. The result
is an approximation, not survey-accurate data.
Each region is named by its ISO 3166-2 code (JP-01 ... JP-47), with Natural
Earth's English and Japanese names.
Run from apps/learn with Python 3 and no other packages:
python3 tool/maps/japan_prefectures.py
It rewrites halkyon_learn_flutter/assets/maps/japan_prefectures.geojson.
"""
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from outlines import download, polygons_of, reduce # noqa: E402
SOURCE = (
"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/"
"v5.1.2/geojson/ne_10m_admin_1_states_provinces.geojson"
)
SOURCE_SHA256 = "22d0e3ad85eb3e27f17cabf8ba2d50e554fbc27a87796ff891d958185da62fb5"
TARGET = Path("halkyon_learn_flutter/assets/maps/japan_prefectures.geojson")
TOLERANCE = 0.006 # degrees
MIN_AREA = 0.0005 # square degrees; smaller islands are dropped
DECIMALS = 4
def main():
source = json.loads(download(SOURCE, SOURCE_SHA256))
features = []
for feature in source["features"]:
properties = feature["properties"]
if properties.get("adm0_a3") != "JPN":
continue
features.append({
"type": "Feature",
"properties": {
"code": properties["iso_3166_2"],
"name": properties["name"],
"name_ja": properties["name_ja"],
},
"geometry": reduce(polygons_of(feature["geometry"]), TOLERANCE,
MIN_AREA, DECIMALS),
})
features.sort(key=lambda feature: feature["properties"]["code"])
codes = [feature["properties"]["code"] for feature in features]
if codes != [f"JP-{n:02d}" for n in range(1, 48)]:
sys.exit(f"Expected the 47 prefectures JP-01 ... JP-47, got {codes}")
TARGET.write_text(json.dumps(
{"type": "FeatureCollection", "features": features},
ensure_ascii=False, separators=(",", ":")) + "\n", encoding="utf-8")
print(f"Wrote {TARGET} ({TARGET.stat().st_size} bytes)", file=sys.stderr)
if __name__ == "__main__":
main()