// The painted map wrapped onto the globe: a nadir camera at an altitude, 45° vertical fov like the client. // Usage: bun gen-globe.ts import { geoPath, geoGraticule10, geoCircle } from "d3-geo"; import { geoSatellite } from "d3-geo-projection"; const out = process.argv[2] ?? "."; const J = async (f: string) => Bun.file(f).json(); const urban = await J("ne_10m_urban_areas.geojson"); const [land, lakes, countries, places, lc3, lc4, eqlc] = await Promise.all([ J("ne_50m_land.geojson"), J("ne_50m_lakes.geojson"), J("ne_50m_admin_0_countries.geojson"), J("ne_10m_populated_places_simple.geojson"), J("lc_z3.geojson"), J("lc_z4.geojson"), Promise.resolve(null)]); const LC: Record = { farmland: "#979262", grassland: "#6f8a55", forest: "#3e5a3c", residential: "#8a817a", marsh: "#6f9a86", heath: "#9c7d93", scrub: "#9aa25c", bare_rock: "#e6d6a6" }; const ER = 6371; async function frame(altKm: number, W: number, H: number, lon: number, lat: number, lc: any, opt: { labels: number; hex?: boolean; zoom?: number; night?: [number, number]; clouds?: number }) { const P = (ER + altKm) / ER; const proj = geoSatellite().distance(P).tilt(0).rotate([-lon, -lat]).translate([W / 2, H / 2]).scale(1) .clipAngle((Math.acos(1 / P) * 180) / Math.PI - 1e-6).precision(0.3); // calibrate: a point θ north must sit at the pixel a 45° vertical fov puts it const th = 2 * Math.PI / 180; const alpha = Math.atan((ER * Math.sin(th)) / (ER + altKm - ER * Math.cos(th))); const want = (H / 2) / Math.tan(Math.PI / 8) * Math.tan(alpha) * (opt.zoom ?? 1); const got = H / 2 - proj([lon, lat + 2])![1]; proj.scale(want / got); const pth = geoPath(proj); let s = ` `; let rng = 3; const rnd = () => ((rng = (rng * 16807) % 2147483647) / 2147483647); for (let i = 0; i < 260; i++) s += ``; const sphere = pth({ type: "Sphere" } as any); s += ``; s += ``; const landD = land.features.map((f: any) => pth(f)).join(""); s += ``; s += ``; s += ``; const by: Record = {}; for (const f of lc.features) if (f.properties.layer === "land") by[f.properties.kind] = (by[f.properties.kind] ?? "") + (pth(f) ?? ""); for (const k of ["farmland", "grassland", "scrub", "heath", "marsh", "bare_rock", "forest", "residential"]) if (by[k]) { s += ``; if (k === "forest") s += ``; } s += ``; s += ``; s += ``; const nl = countries.features.find((f: any) => f.properties.ADM0_A3 === "NLD"); s += ``; s += ``; if (opt.clouds) { s += ``; s += ``; } if (opt.night) { const [slon, slat] = opt.night; const anti: [number, number] = [slon + 180, -slat]; // twilight: stacked caps round the antisolar point, darkest in the middle const toV = (lo: number, la: number) => { const a = lo * Math.PI / 180, b = la * Math.PI / 180; return [Math.cos(b) * Math.cos(a), Math.cos(b) * Math.sin(a), Math.sin(b)]; }; const sun = toV(slon, slat); const night = new Uint8Array(W * H); for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) { const ll = proj.invert!([x + 0.5, y + 0.5]); if (!ll || !isFinite(ll[0]) || !pth({ type: "Point", coordinates: ll } as any)) { night[y * W + x] = x ? night[y * W + x - 1] : 0; continue; } const v = toV(ll[0], ll[1]); const c = v[0] * sun[0] + v[1] * sun[1] + v[2] * sun[2]; const t = Math.min(1, Math.max(0, (0.06 - c) / 0.2)); // twilight band about 12 degrees wide night[y * W + x] = Math.round(255 * t * t * (3 - 2 * t)); } const pgm = Buffer.concat([Buffer.from(`P5 ${W} ${H} 255\n`), Buffer.from(night)]); await Bun.write(`/tmp/s525-night.pgm`, pgm); Bun.spawnSync(["convert", "/tmp/s525-night.pgm", `${out}/night-mask.png`]); s += ``; s += ``; s += ``; const ud = urban.features.map((u: any) => pth(u) ?? "").join(""); s += ``; for (const p of places.features) { const v = p.properties.pop_max; if (v < 150000) continue; const q = proj(p.geometry.coordinates); if (!q || !pth({ type: "Point", coordinates: p.geometry.coordinates } as any)) continue; s += ``; } s += ``; } s += ``; s += ``; // city banners: biggest places on the near side const cand = places.features.filter((f: any) => f.properties.pop_max > 0).sort((a: any, b: any) => b.properties.pop_max - a.properties.pop_max); const taken: number[][] = []; let n = 0; const fmt = (v: number) => (v >= 1e6 ? (v / 1e6).toFixed(1) + "M" : Math.round(v / 1e3) + "k"); for (const f of cand) { if (n >= opt.labels) break; const [lo, la] = f.geometry.coordinates; const vis = pth({ type: "Point", coordinates: [lo, la] } as any); if (!vis) continue; const p = proj([lo, la]); if (!p) continue; const [x, y] = p; if (x < 60 || x > W - 60 || y < 50 || y > H - 20) continue; const name = f.properties.name; const pop = fmt(f.properties.pop_max); const tw = name.length * 7.6 + 8, pw = pop.length * 6.6 + 12, w = tw + pw + 10, h = 21; const box = [x - w / 2, y - 36, x + w / 2, y + 4]; if (taken.some((t) => !(box[2] < t[0] || box[0] > t[2] || box[3] < t[1] || box[1] > t[3]))) continue; taken.push(box); n++; s += ` ${pop} ${name}`; } return s + ``; } await Bun.write(`${out}/globe-3000.svg`, await frame(3000, 1600, 900, 8, 52, lc4, { labels: 14, zoom: 1.44 })); await Bun.write(`${out}/globe-8000.svg`, await frame(8000, 1600, 900, 8, 52, lc3, { labels: 12, zoom: 0.92 })); await Bun.write(`${out}/globe-8000-night.svg`, await frame(8000, 1600, 900, 8, 52, lc3, { labels: 0, zoom: 0.92, night: [-78.5, -14] })); await Bun.write(`${out}/globe-3000-clouds.svg`, await frame(3000, 1600, 900, 8, 52, lc4, { labels: 14, zoom: 1.44, clouds: 0.45 })); console.log("done");