aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/mapgen/viewer_export.py
diff options
context:
space:
mode:
authorgodosa <godosa@godosa.eu>2026-10-06 23:52:03 +0200
committergodosa <godosa@godosa.eu>2026-10-06 23:52:03 +0200
commit346b1c5195bffc71ceaa9262453e3c189656400b (patch)
tree01ac0d31e2724cd6abcc689a5a228e2cbea2f6cf /mapgen/viewer_export.py
downloadworldgen-346b1c5195bffc71ceaa9262453e3c189656400b.tar.gz
worldgen-346b1c5195bffc71ceaa9262453e3c189656400b.zip
worldgen: initial public history
Diffstat (limited to 'mapgen/viewer_export.py')
-rw-r--r--mapgen/viewer_export.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/mapgen/viewer_export.py b/mapgen/viewer_export.py
new file mode 100644
index 0000000..e6f51b4
--- /dev/null
+++ b/mapgen/viewer_export.py
@@ -0,0 +1,39 @@
+"""Viewer textures: colourised equirectangular JPEG layers + layers.json."""
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import numpy as np
+from PIL import Image
+
+
+def hex_color(c) -> str:
+ return "#%02x%02x%02x" % tuple(int(v) for v in list(c)[:3])
+
+
+def categorical_legend(names, palette) -> dict:
+ return {"type": "categorical",
+ "items": [{"name": n, "color": hex_color(palette[i % len(palette)])} for i, n in enumerate(names)]}
+
+
+def continuous_legend(unit, lo, hi, stops) -> dict:
+ vals = np.linspace(lo, hi, len(stops))
+ return {"type": "continuous", "unit": unit,
+ "stops": [[round(float(v), 4), hex_color(s)] for v, s in zip(vals, stops)]}
+
+
+def write_layer(vdir: Path, L: dict) -> dict:
+ """Write one layer's JPEG; returns its layers.json entry."""
+ vdir.mkdir(parents=True, exist_ok=True)
+ Image.fromarray(np.ascontiguousarray(L["rgb"], dtype=np.uint8)).save(vdir / f"{L['id']}.jpg", quality=90)
+ return {"id": L["id"], "name": L["name"], "file": f"{L['id']}.jpg", "legend": L["legend"]}
+
+
+def write_index(vdir: Path, entries: list[dict]) -> None:
+ vdir.mkdir(parents=True, exist_ok=True)
+ (vdir / "layers.json").write_text(json.dumps(entries, indent=1))
+
+
+def write_all(vdir: Path, layers: list[dict]) -> None:
+ write_index(vdir, [write_layer(vdir, L) for L in layers])