#!/usr/bin/env node // Regenerates internal/server/web/world.js (simplified world map, one SVG path // per country, plus country centroids) from the public-domain Natural Earth data // shipped in the npm package "world-atlas" and a REST Countries style JSON // (cca2, ccn3, latlng). Only needed when the map should change. // // node scripts/gen-world.mjs countries-110m.json countries.json > internal/server/web/world.js import { readFileSync } from 'node:fs'; const [topoFile, cFile] = process.argv.slice(2); const topo = JSON.parse(readFileSync(topoFile, 'utf8')); const countries = JSON.parse(readFileSync(cFile, 'utf8')); const byNum = new Map(countries.map((c) => [String(c.ccn3).padStart(3, '0'), c])); const W = 2000, LAT_TOP = 84, LAT_BOTTOM = -58; const H = Math.round((W * (LAT_TOP - LAT_BOTTOM)) / 360); const px = (lon) => ((lon + 180) / 360) * W; const py = (lat) => ((LAT_TOP - Math.max(LAT_BOTTOM, Math.min(LAT_TOP, lat))) / (LAT_TOP - LAT_BOTTOM)) * H; const { scale, translate } = topo.transform; const arcs = topo.arcs.map((arc) => { let x = 0, y = 0; return arc.map(([dx, dy]) => { x += dx; y += dy; return [x * scale[0] + translate[0], y * scale[1] + translate[1]]; }); }); const arcPts = (i) => (i >= 0 ? arcs[i] : arcs[~i].slice().reverse()); const ring = (idx) => { const pts = []; for (const i of idx) { const a = arcPts(i); pts.push(...(pts.length ? a.slice(1) : a)); } return pts; }; const paths = {}, cent = {}; for (const g of topo.objects.countries.geometries) { const c = byNum.get(String(g.id).padStart(3, '0')); if (!c || c.cca2 === 'AQ') continue; const polys = g.type === 'Polygon' ? [g.arcs] : g.type === 'MultiPolygon' ? g.arcs : []; let d = ''; for (const poly of polys) { for (const r of poly) { const pts = ring(r).map(([lo, la]) => [Math.round(px(lo)), Math.round(py(la))]); const out = []; for (const p of pts) { const l = out[out.length - 1]; if (!l || l[0] !== p[0] || l[1] !== p[1]) out.push(p); } if (out.length < 4) continue; let s = `M${out[0][0]} ${out[0][1]}`, px0 = out[0][0], py0 = out[0][1]; for (const [x, y] of out.slice(1)) { // a ring crossing the antimeridian jumps across the map: start a new subpath instead of drawing a line over it s += Math.abs(x - px0) > W / 2 ? `M${x} ${y}` : `l${x - px0} ${y - py0}`; px0 = x; py0 = y; } d += s + 'z'; } } if (d) paths[c.cca2] = d; } for (const c of countries) if (c.latlng?.length === 2) cent[c.cca2] = [Math.round(px(c.latlng[1])), Math.round(py(c.latlng[0]))]; process.stdout.write(`/* Generated by scripts/gen-world.mjs from Natural Earth (public domain). */\nwindow.URUTAU_WORLD = ${JSON.stringify({ w: W, h: H, paths, centroids: cent })};\n`);