#!/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; }; // Rings that cross the antimeridian carry a +-360 degree longitude jump (the source is cut there). // Unwrap them into one continuous ring, then clip against the map band [-180,180] on // copies shifted by -360/0/+360, so each piece is closed along the map border instead of // by a chord across the map. const unwrap = (pts) => { const out = [[pts[0][0], pts[0][1]]]; let off = 0; for (let i = 1; i < pts.length; i++) { const d = pts[i][0] + off - out[i - 1][0]; if (d > 180) off -= 360; else if (d < -180) off += 360; out.push([pts[i][0] + off, pts[i][1]]); } return out; }; // Sutherland-Hodgman against one vertical line const clipX = (pts, edge, keepLeft) => { const inside = (p) => (keepLeft ? p[0] <= edge : p[0] >= edge); const out = []; for (let i = 0; i < pts.length; i++) { const a = pts[i], b = pts[(i + 1) % pts.length]; const ia = inside(a), ib = inside(b); if (ia) out.push(a); if (ia !== ib) out.push([edge, a[1] + ((b[1] - a[1]) * (edge - a[0])) / (b[0] - a[0])]); } return out; }; const pieces = (pts) => { if (pts.length > 1 && pts[0][0] === pts[pts.length - 1][0] && pts[0][1] === pts[pts.length - 1][1]) pts = pts.slice(0, -1); pts = unwrap(pts); const res = []; for (const k of [-360, 0, 360]) { const shifted = pts.map(([x, y]) => [x + k, y]); if (!shifted.some(([x]) => x > -180 && x < 180)) continue; const c = clipX(clipX(shifted, 180, true), -180, false); if (c.length >= 3) res.push(c); } return res; }; 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) { for (const piece of pieces(ring(r))) { const out = []; for (const [lo, la] of piece) { const p = [Math.round(px(lo)), Math.round(py(la))]; const l = out[out.length - 1]; if (!l || l[0] !== p[0] || l[1] !== p[1]) out.push(p); } if (out.length > 1 && out[0][0] === out[out.length - 1][0] && out[0][1] === out[out.length - 1][1]) out.pop(); if (out.length < 3) 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)) { s += `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`);