"""Stitch built tiles into one overview PNG -- a layout sanity check. python tools/preview.py [zoom] # default 3 Writes build/world-preview.png showing every map at its world position, so you can confirm a newly added map landed in the grid cell you meant before opening a browser. """ from __future__ import annotations import json import sys from PIL import Image from common import BUILD, DATA, ROOT, TILES Image.MAX_IMAGE_PIXELS = None def main() -> None: z = int(sys.argv[1]) if len(sys.argv) > 1 else 3 world_path = DATA / "world.json" if not world_path.exists(): raise SystemExit("web/data/world.json missing -- run tools/build.py first") world = json.loads(world_path.read_text(encoding="utf-8")) ts, zmax = world["tileSize"], world["maxZoom"] if not 0 <= z <= zmax: raise SystemExit(f"zoom must be 0..{zmax}") scale = 2.0 ** (z - zmax) def at(v: float) -> int: return round(v * scale) width = max(at(m["origin"][0] + m["size"][0]) for m in world["maps"]) height = max(at(m["origin"][1] + m["size"][1]) for m in world["maps"]) canvas = Image.new("RGB", (width, height), (20, 22, 26)) for m in world["maps"]: gx, gy = m["cell"] ox, oy = at(m["origin"][0]), at(m["origin"][1]) tdir = TILES / m["id"] / str(z) if not tdir.exists(): print(f" {m['id']}: no tiles at z{z}, skipped") continue n = 0 for col in tdir.iterdir(): for tile in col.glob("*.webp"): lx, ly = int(col.name), int(tile.stem) canvas.paste(Image.open(tile).convert("RGB"), (ox + lx * ts, oy + ly * ts)) n += 1 print(f" {m['id']:<16} cell {gx},{gy} world {ox},{oy} {n} tiles") out = BUILD / "world-preview.png" canvas.save(out) print(f"\nwrote {out.relative_to(ROOT)} {width}x{height} (zoom {z})") if __name__ == "__main__": main()