GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — website/scripts/generate-travel-map.ts at main · dk-e/website

Expand file tree

/

generate-travel-map.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
135 lines (128 loc) · 4.26 KB
/

generate-travel-map.ts

Copy path

File metadata and controls

135 lines (128 loc) · 4.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/** Public-domain Natural Earth map units, projected once rather than in the browser.
* Map units give French Guiana (GF), Réunion (RE), etc. their own ISO identities.
* Run: bun run travel:map
*/
import { writeFile } from "node:fs/promises";
type Point = [number, number];
type Feature = {
properties: {
ISO_A2_EH: string;
GU_A3: string;
CONTINENT: string;
NAME_EN: string;
};
geometry:
| { type: "Polygon"; coordinates: Point[][] }
| { type: "MultiPolygon"; coordinates: Point[][][] };
};
const source =
"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_map_units.geojson";
function simplify(points: Point[], tolerance = 0.12): Point[] {
if (points.length < 3) return points;
const a = points[0]!;
const b = points[points.length - 1]!;
const dx = b[0] - a[0],
dy = b[1] - a[1];
const length = dx * dx + dy * dy;
let best = 0,
index = 0;
for (let i = 1; i < points.length - 1; i++) {
const p = points[i]!;
const t = length
? Math.max(
0,
Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / length),
)
: 0;
const distance = (p[0] - a[0] - t * dx) ** 2 + (p[1] - a[1] - t * dy) ** 2;
if (distance > best) {
best = distance;
index = i;
}
}
if (best > tolerance * tolerance)
return [
...simplify(points.slice(0, index + 1)).slice(0, -1),
...simplify(points.slice(index)),
];
return [a, b];
}
// Equal Earth projection. Round at generation time to keep the bundled SVG small.
function project([lon, lat]: Point): Point {
const theta = Math.asin((Math.sqrt(3) / 2) * Math.sin((lat * Math.PI) / 180));
const t2 = theta * theta,
t6 = t2 * t2 * t2;
const a1 = 1.340264,
a2 = -0.081106,
a3 = 0.000893,
a4 = 0.003796;
const x =
(2 * Math.sqrt(3) * ((lon * Math.PI) / 180) * Math.cos(theta)) /
(3 * (a1 + 3 * a2 * t2 + t6 * (7 * a3 + 9 * a4 * t2)));
const y = theta * (a1 + a2 * t2 + t6 * (a3 + a4 * t2));
return [
Math.round((360 + 128 * x) * 10) / 10,
Math.round((177 - 128 * y) * 10) / 10,
];
}
const response = await fetch(source, { signal: AbortSignal.timeout(30_000) });
if (!response.ok) throw new Error(`Map download failed: ${response.status}`);
const { features } = (await response.json()) as { features: Feature[] };
const names = new Intl.DisplayNames(["en"], { type: "region" });
const countries = new Map<
string,
{ code: string; name: string; continent: string }
>();
const shapes = new Map<string, { code: string; path: string }>();
const oceanContinents: Record<string, string> = {
GS: "South America",
IO: "Asia",
SH: "Africa",
SC: "Africa",
PT: "Europe",
MU: "Africa",
MV: "Asia",
RE: "Africa",
TF: "Antarctica",
HM: "Antarctica",
};
for (const { properties: p, geometry } of features) {
if (p.ISO_A2_EH === "AQ") continue;
const code = /^[A-Z]{2}$/.test(p.ISO_A2_EH) ? p.ISO_A2_EH : p.GU_A3;
if (code.length === 2) {
const continent =
p.CONTINENT === "Seven seas (open ocean)"
? oceanContinents[code]
: p.CONTINENT;
if (!continent) throw new Error(`Missing continent for ${code}`);
countries.set(code, { code, name: names.of(code) ?? p.NAME_EN, continent });
}
const polygons =
geometry.type === "MultiPolygon"
? geometry.coordinates
: [geometry.coordinates];
let path = "";
for (const polygon of polygons) {
for (const ring of polygon) {
// Tiny islands retain their outline instead of disappearing on simplification.
const simplified = simplify(ring);
const points = (simplified.length < 4 ? ring : simplified).map(project);
path += `M${points.map(([x, y]) => `${x},${y}`).join("L")}Z`;
}
}
// England, Scotland, Wales and Northern Ireland share GB; overseas ISO codes do not.
shapes.set(code, { code, path: (shapes.get(code)?.path ?? "") + path });
}
await writeFile(
new URL("../data/travel-countries.json", import.meta.url),
JSON.stringify(
[...countries.values()].sort((a, b) => a.name.localeCompare(b.name)),
) + "\n",
);
await writeFile(
new URL("../data/travel-map.json", import.meta.url),
JSON.stringify([...shapes.values()]) + "\n",
);
console.log(
`Generated ${countries.size} country/territory entries and ${shapes.size} map shapes.`,
);
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.