@johnmorrisdotca/chikyu-earth 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/NOTICE.md +4 -0
- package/README.md +27 -3
- package/dist/sheets/common.d.ts +50 -0
- package/dist/sheets/common.js +94 -0
- package/dist/sheets/country.d.ts +50 -0
- package/dist/sheets/country.js +70 -0
- package/dist/sheets/earth.d.ts +50 -0
- package/dist/sheets/earth.js +103 -0
- package/dist/sheets/hatch.d.ts +23 -0
- package/dist/sheets/hatch.js +66 -0
- package/dist/sheets/index.d.ts +7 -0
- package/dist/sheets/index.js +9 -0
- package/dist/sheets/land.d.ts +83 -0
- package/dist/sheets/land.js +257 -0
- package/dist/sheets/outline.d.ts +15 -0
- package/dist/sheets/outline.js +116 -0
- package/dist/sheets/sun.d.ts +68 -0
- package/dist/sheets/sun.js +216 -0
- package/dist/sheets/text.d.ts +213 -0
- package/dist/sheets/text.js +244 -0
- package/package.json +11 -4
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Shading that prints in black and white: parallel lines, drawn only where a test says so (the night side of the Earth). The lines are plain geometry, not a pattern fill, so they print the same in any browser. Pure.
|
|
2
|
+
const r = (v) => (Math.round(v * 100) / 100).toString();
|
|
3
|
+
/**
|
|
4
|
+
* Parallel lines, `spacing` apart at `angleDeg`, cut to where `inside(x, y)` is true, as the data of one SVG path (a millimetre of the picture is a millimetre of the paper). Each end is found to a twentieth of a millimetre.
|
|
5
|
+
* @param bounds - The box the lines are laid over.
|
|
6
|
+
* @param spacing - The gap between lines, in millimetres.
|
|
7
|
+
* @param angleDeg - The turn of the lines, in degrees from the x axis.
|
|
8
|
+
* @param inside - Whether a point is in the shaded part.
|
|
9
|
+
* @returns Path data: `M x y L x y` for each stroke.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { hatchPath } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
14
|
+
*
|
|
15
|
+
* const d = hatchPath({ x0: 0, y0: 0, x1: 20, y1: 20 }, 4, 45, (x) => x < 10);
|
|
16
|
+
* console.log(d.startsWith("M"));
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function hatchPath(bounds, spacing, angleDeg, inside) {
|
|
20
|
+
const a = (angleDeg * Math.PI) / 180;
|
|
21
|
+
const ux = Math.cos(a);
|
|
22
|
+
const uy = Math.sin(a);
|
|
23
|
+
const nx = -uy;
|
|
24
|
+
const ny = ux;
|
|
25
|
+
const corners = [[bounds.x0, bounds.y0], [bounds.x1, bounds.y0], [bounds.x0, bounds.y1], [bounds.x1, bounds.y1]];
|
|
26
|
+
const ts = corners.map(([x, y]) => x * nx + y * ny);
|
|
27
|
+
const ss = corners.map(([x, y]) => x * ux + y * uy);
|
|
28
|
+
const tMin = Math.min(...ts);
|
|
29
|
+
const tMax = Math.max(...ts);
|
|
30
|
+
const sMin = Math.min(...ss);
|
|
31
|
+
const sMax = Math.max(...ss);
|
|
32
|
+
const step = 0.4;
|
|
33
|
+
const at = (t, s) => [t * nx + s * ux, t * ny + s * uy];
|
|
34
|
+
/** The place between an outside sample and an inside one where the shading starts or stops. */
|
|
35
|
+
const edge = (t, outside, within) => {
|
|
36
|
+
let lo = outside;
|
|
37
|
+
let hi = within;
|
|
38
|
+
for (let i = 0; i < 4; i += 1) {
|
|
39
|
+
const m = (lo + hi) / 2;
|
|
40
|
+
if (inside(...at(t, m)))
|
|
41
|
+
hi = m;
|
|
42
|
+
else
|
|
43
|
+
lo = m;
|
|
44
|
+
}
|
|
45
|
+
return (lo + hi) / 2;
|
|
46
|
+
};
|
|
47
|
+
const out = [];
|
|
48
|
+
for (let t = tMin + spacing / 2; t <= tMax; t += spacing) {
|
|
49
|
+
let start;
|
|
50
|
+
let previous = sMin;
|
|
51
|
+
for (let s = sMin; s <= sMax + step; s += step) {
|
|
52
|
+
const on = inside(...at(t, s));
|
|
53
|
+
if (on && start === undefined)
|
|
54
|
+
start = s === sMin ? s : edge(t, previous, s);
|
|
55
|
+
if (!on && start !== undefined) {
|
|
56
|
+
const end = edge(t, s, previous);
|
|
57
|
+
const [x0, y0] = at(t, start);
|
|
58
|
+
const [x1, y1] = at(t, end);
|
|
59
|
+
out.push(`M${r(x0)} ${r(y0)}L${r(x1)} ${r(y1)}`);
|
|
60
|
+
start = undefined;
|
|
61
|
+
}
|
|
62
|
+
previous = s;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out.join("");
|
|
66
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { globeSheetSvg, worldMapSvg, type SheetOptions } from "./earth.ts";
|
|
2
|
+
export { countrySheetSvg, type Capital, type CountryOptions } from "./country.ts";
|
|
3
|
+
export { dayNightSvg, dayNightView, seasonsSvg, terminator } from "./sun.ts";
|
|
4
|
+
export { continentLines, countryCodes, countryShape, landShares, type CountryShape } from "./land.ts";
|
|
5
|
+
export { thickPath } from "./outline.ts";
|
|
6
|
+
export { hatchPath } from "./hatch.ts";
|
|
7
|
+
export { GLOBE_VIEWS, PLACES, SAY, TILT_DEGREES, TITLES, countryFacts, dayNightFacts, earthFacts, seasonsFacts, type GlobeView, type Place } from "./text.ts";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The printable sheets of the Earth for children and everyone: the globe to colour, a world map, a "my country" sheet, day and night, and the seasons. Pure: each is a string of SVG measured in millimetres, to put inside kazoku-ui's print sheet (the demo does),
|
|
2
|
+
// so the page's header, footer and QR code are not made here and the sheets need no browser.
|
|
3
|
+
export { globeSheetSvg, worldMapSvg } from "./earth.js";
|
|
4
|
+
export { countrySheetSvg } from "./country.js";
|
|
5
|
+
export { dayNightSvg, dayNightView, seasonsSvg, terminator } from "./sun.js";
|
|
6
|
+
export { continentLines, countryCodes, countryShape, landShares } from "./land.js";
|
|
7
|
+
export { thickPath } from "./outline.js";
|
|
8
|
+
export { hatchPath } from "./hatch.js";
|
|
9
|
+
export { GLOBE_VIEWS, PLACES, SAY, TILT_DEGREES, TITLES, countryFacts, dayNightFacts, earthFacts, seasonsFacts } from "./text.js";
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Feature, MultiPolygon } from "geojson";
|
|
2
|
+
export type ContinentId = "africa" | "asia" | "europe" | "north-america" | "south-america" | "oceania" | "antarctica";
|
|
3
|
+
/**
|
|
4
|
+
* The coasts of the continents as lines in longitude and latitude, for a child to colour a whole continent: the line round each continent and between two continents (Europe and Asia), and none between two countries of one continent. Islands under `minAreaSr` steradians are left out
|
|
5
|
+
* (about 80,000 square kilometres at the default, so Great Britain, Japan, Madagascar and New Zealand stay and small islands go, which at a thick line would only be blobs).
|
|
6
|
+
* @param minAreaSr - The smallest island kept, in steradians (the whole sphere is 4 pi).
|
|
7
|
+
* @returns Lines of `[longitude, latitude]` points.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { continentLines } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
12
|
+
*
|
|
13
|
+
* const lines = continentLines();
|
|
14
|
+
* console.log(lines.length > 5, lines[0]?.length);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function continentLines(minAreaSr?: number): [number, number][][];
|
|
18
|
+
/**
|
|
19
|
+
* How much of the Earth is land, by the package's outlines, in percent, and the share of all the land each continent has.
|
|
20
|
+
* @returns The land's percent of the whole sphere, and each continent's percent of the land.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { landShares } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
25
|
+
*
|
|
26
|
+
* const shares = landShares();
|
|
27
|
+
* console.log(Math.round(shares.landPercent), shares.continents.asia > shares.continents.africa);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function landShares(): {
|
|
31
|
+
landPercent: number;
|
|
32
|
+
continents: Record<ContinentId, number>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* One country as the sheets draw it: its names, its continent, its land as one feature and the names of the countries it touches.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { countryShape, type CountryShape } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
40
|
+
*
|
|
41
|
+
* const spain: CountryShape | undefined = countryShape("ES");
|
|
42
|
+
* console.log(spain?.neighbours.map((one) => one.name));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export interface CountryShape {
|
|
46
|
+
name: string;
|
|
47
|
+
ja: string;
|
|
48
|
+
alpha2: string | null;
|
|
49
|
+
continent: ContinentId;
|
|
50
|
+
/** The main land and the nearby islands as one feature. */
|
|
51
|
+
feature: Feature<MultiPolygon>;
|
|
52
|
+
/** The names of the countries it shares a border with, English and Japanese. */
|
|
53
|
+
neighbours: {
|
|
54
|
+
name: string;
|
|
55
|
+
ja: string;
|
|
56
|
+
}[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* One country by its ISO alpha-2 code, for a "my country" sheet: its main land and the islands near it (a far territory such as French Guiana is left out), the continent it is in and the countries it touches.
|
|
60
|
+
* @param alpha2 - The two-letter code, such as `JP`.
|
|
61
|
+
* @returns The shape, or `undefined` when the package has no outline for the code.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { countryShape } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
66
|
+
*
|
|
67
|
+
* const france = countryShape("FR");
|
|
68
|
+
* console.log(france?.continent, france?.neighbours.length);
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function countryShape(alpha2: string): CountryShape | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* The two-letter codes of the countries that have an outline.
|
|
74
|
+
* @returns The codes, in the order of the outlines.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { countryCodes } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
79
|
+
*
|
|
80
|
+
* console.log(countryCodes().includes("JP"));
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare const countryCodes: () => string[];
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// The land as the printable sheets need it, worked out from the package's own outlines (chizu's Natural Earth 1:110m): the coast of each continent as lines (the borders inside a continent are left out, so a child colours a whole continent),
|
|
2
|
+
// how much of the Earth is land, and which countries touch each other. Pure.
|
|
3
|
+
import WORLD_GEO from "@johnmorrisdotca/chizu/world-geo";
|
|
4
|
+
import { geoArea, geoCentroid, geoDistance } from "d3-geo";
|
|
5
|
+
/** The continents, by the name chizu gives each country's group. */
|
|
6
|
+
const GROUP_IDS = {
|
|
7
|
+
Africa: "africa",
|
|
8
|
+
Asia: "asia",
|
|
9
|
+
Europe: "europe",
|
|
10
|
+
"North America": "north-america",
|
|
11
|
+
"South America": "south-america",
|
|
12
|
+
Oceania: "oceania",
|
|
13
|
+
Antarctica: "antarctica",
|
|
14
|
+
};
|
|
15
|
+
const LANDS = WORLD_GEO.features.map((feature) => {
|
|
16
|
+
const geometry = feature.geometry;
|
|
17
|
+
return {
|
|
18
|
+
name: feature.properties.name,
|
|
19
|
+
ja: feature.properties.nameJa || feature.properties.name,
|
|
20
|
+
alpha2: feature.properties.code.length === 2 ? feature.properties.code : null,
|
|
21
|
+
continent: GROUP_IDS[feature.properties.group] ?? "asia",
|
|
22
|
+
polygons: (geometry.type === "Polygon" ? [geometry.coordinates] : geometry.coordinates),
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
/** The area of a polygon in steradians (the whole sphere is 4π), whichever way its ring is wound. */
|
|
26
|
+
const areaOf = (rings) => {
|
|
27
|
+
const a = geoArea({ type: "Polygon", coordinates: rings });
|
|
28
|
+
return a > 2 * Math.PI ? 4 * Math.PI - a : a;
|
|
29
|
+
};
|
|
30
|
+
/** Russia is in both Europe and Asia: the line between them is taken as the meridian 60 degrees east (the Urals are close to it), and everything east of it, with Chukotka past the antimeridian, is Asia. */
|
|
31
|
+
const URALS = 60;
|
|
32
|
+
const isEast = (lon) => lon >= URALS || lon <= -150;
|
|
33
|
+
/** The part of a ring on the east side of the Urals meridian (one half-plane, so a plain clip). */
|
|
34
|
+
function eastOf(ring) {
|
|
35
|
+
const out = [];
|
|
36
|
+
for (let i = 0; i < ring.length - 1; i += 1) {
|
|
37
|
+
const a = ring[i];
|
|
38
|
+
const b = ring[i + 1];
|
|
39
|
+
const ain = a[0] >= URALS;
|
|
40
|
+
const bin = b[0] >= URALS;
|
|
41
|
+
if (ain)
|
|
42
|
+
out.push(a);
|
|
43
|
+
if (ain !== bin)
|
|
44
|
+
out.push([URALS, a[1] + ((URALS - a[0]) / (b[0] - a[0])) * (b[1] - a[1])]);
|
|
45
|
+
}
|
|
46
|
+
if (out.length > 0)
|
|
47
|
+
out.push(out[0]);
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
/** The point a vertex is known by: the antimeridian is one line, so 180 and -180 are the same. */
|
|
51
|
+
const keyOf = (p) => `${p[0] === 180 ? -180 : p[0]},${p[1]}`;
|
|
52
|
+
/** Every edge of every outline, and who it is shared with. Built on first use. */
|
|
53
|
+
let table;
|
|
54
|
+
function edgesOf() {
|
|
55
|
+
if (table !== undefined)
|
|
56
|
+
return table;
|
|
57
|
+
const edges = new Map();
|
|
58
|
+
const areas = new Map();
|
|
59
|
+
let total = 0;
|
|
60
|
+
LANDS.forEach((land, index) => {
|
|
61
|
+
land.polygons.forEach((polygon, which) => {
|
|
62
|
+
const size = areaOf(polygon);
|
|
63
|
+
if (land.alpha2 === "RU") {
|
|
64
|
+
const ring = polygon[0];
|
|
65
|
+
const lons = ring.map((p) => p[0]);
|
|
66
|
+
const east = Math.min(...lons) >= URALS || Math.max(...lons) <= URALS ? (isEast(lons[0]) ? size : 0) : areaOf([eastOf(ring)]);
|
|
67
|
+
areas.set("asia", (areas.get("asia") ?? 0) + east);
|
|
68
|
+
areas.set("europe", (areas.get("europe") ?? 0) + size - east);
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
areas.set(land.continent, (areas.get(land.continent) ?? 0) + size);
|
|
72
|
+
total += size;
|
|
73
|
+
// a hole is another country's land (Lesotho in South Africa), never a line of its own
|
|
74
|
+
const ring = polygon[0];
|
|
75
|
+
for (let i = 0; i < ring.length - 1; i += 1) {
|
|
76
|
+
const a = ring[i];
|
|
77
|
+
const b = ring[i + 1];
|
|
78
|
+
const ka = keyOf(a);
|
|
79
|
+
const kb = keyOf(b);
|
|
80
|
+
if (ka === kb)
|
|
81
|
+
continue;
|
|
82
|
+
const key = ka < kb ? `${ka}|${kb}` : `${kb}|${ka}`;
|
|
83
|
+
let edge = edges.get(key);
|
|
84
|
+
if (edge === undefined) {
|
|
85
|
+
edge = { a, b, continents: new Set(), lands: new Set(), polygons: new Set(), count: 0, biggest: 0 };
|
|
86
|
+
edges.set(key, edge);
|
|
87
|
+
}
|
|
88
|
+
edge.continents.add(land.alpha2 === "RU" ? (isEast((a[0] + b[0]) / 2) ? "asia" : "europe") : land.continent);
|
|
89
|
+
edge.lands.add(index);
|
|
90
|
+
edge.polygons.add(`${index}:${which}`);
|
|
91
|
+
edge.count += 1;
|
|
92
|
+
edge.biggest = Math.max(edge.biggest, size);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
table = { edges: [...edges.values()], areas, total };
|
|
97
|
+
return table;
|
|
98
|
+
}
|
|
99
|
+
/** The line between Europe and Asia through Russia: the meridian from where the main land is crossed by it in the south to where it is crossed in the north. */
|
|
100
|
+
function uralsLine() {
|
|
101
|
+
const russia = LANDS.find((land) => land.alpha2 === "RU");
|
|
102
|
+
if (russia === undefined)
|
|
103
|
+
return undefined;
|
|
104
|
+
const ring = russia.polygons.reduce((a, b) => (b[0].length > a[0].length ? b : a))[0];
|
|
105
|
+
const crossings = [];
|
|
106
|
+
for (let i = 0; i < ring.length - 1; i += 1) {
|
|
107
|
+
const a = ring[i];
|
|
108
|
+
const b = ring[i + 1];
|
|
109
|
+
if ((a[0] - URALS) * (b[0] - URALS) < 0)
|
|
110
|
+
crossings.push(a[1] + ((URALS - a[0]) / (b[0] - a[0])) * (b[1] - a[1]));
|
|
111
|
+
}
|
|
112
|
+
if (crossings.length < 2)
|
|
113
|
+
return undefined;
|
|
114
|
+
return [[URALS, Math.min(...crossings)], [URALS, Math.max(...crossings)]];
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The coasts of the continents as lines in longitude and latitude, for a child to colour a whole continent: the line round each continent and between two continents (Europe and Asia), and none between two countries of one continent. Islands under `minAreaSr` steradians are left out
|
|
118
|
+
* (about 80,000 square kilometres at the default, so Great Britain, Japan, Madagascar and New Zealand stay and small islands go, which at a thick line would only be blobs).
|
|
119
|
+
* @param minAreaSr - The smallest island kept, in steradians (the whole sphere is 4 pi).
|
|
120
|
+
* @returns Lines of `[longitude, latitude]` points.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { continentLines } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
125
|
+
*
|
|
126
|
+
* const lines = continentLines();
|
|
127
|
+
* console.log(lines.length > 5, lines[0]?.length);
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function continentLines(minAreaSr = 0.002) {
|
|
131
|
+
const edges = edgesOf().edges.filter((edge) => (edge.count === 1 || edge.continents.size > 1) && edge.biggest >= minAreaSr);
|
|
132
|
+
const ends = new Map();
|
|
133
|
+
const add = (point, at) => {
|
|
134
|
+
const key = keyOf(point);
|
|
135
|
+
const list = ends.get(key);
|
|
136
|
+
if (list === undefined)
|
|
137
|
+
ends.set(key, [at]);
|
|
138
|
+
else
|
|
139
|
+
list.push(at);
|
|
140
|
+
};
|
|
141
|
+
edges.forEach((edge, i) => {
|
|
142
|
+
add(edge.a, i);
|
|
143
|
+
add(edge.b, i);
|
|
144
|
+
});
|
|
145
|
+
const used = new Set();
|
|
146
|
+
const lines = [];
|
|
147
|
+
const other = (edge, from) => (keyOf(edge.a) === from ? edge.b : edge.a);
|
|
148
|
+
const walk = (start, first) => {
|
|
149
|
+
const points = [start];
|
|
150
|
+
let at = first;
|
|
151
|
+
let from = keyOf(start);
|
|
152
|
+
for (;;) {
|
|
153
|
+
used.add(at);
|
|
154
|
+
const next = other(edges[at], from);
|
|
155
|
+
points.push(next);
|
|
156
|
+
from = keyOf(next);
|
|
157
|
+
const here = ends.get(from);
|
|
158
|
+
if (here.length !== 2)
|
|
159
|
+
break;
|
|
160
|
+
const following = here.find((i) => !used.has(i));
|
|
161
|
+
if (following === undefined)
|
|
162
|
+
break;
|
|
163
|
+
at = following;
|
|
164
|
+
}
|
|
165
|
+
return points;
|
|
166
|
+
};
|
|
167
|
+
// lines start at a point where three lines meet or one ends, then whatever loops are left
|
|
168
|
+
for (const [key, list] of ends) {
|
|
169
|
+
if (list.length === 2)
|
|
170
|
+
continue;
|
|
171
|
+
for (const first of list) {
|
|
172
|
+
if (used.has(first))
|
|
173
|
+
continue;
|
|
174
|
+
const edge = edges[first];
|
|
175
|
+
const start = keyOf(edge.a) === key ? edge.a : edge.b;
|
|
176
|
+
lines.push(walk(start, first));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
edges.forEach((edge, i) => {
|
|
180
|
+
if (!used.has(i))
|
|
181
|
+
lines.push(walk(edge.a, i));
|
|
182
|
+
});
|
|
183
|
+
const urals = uralsLine();
|
|
184
|
+
if (urals !== undefined)
|
|
185
|
+
lines.push(urals);
|
|
186
|
+
return lines;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* How much of the Earth is land, by the package's outlines, in percent, and the share of all the land each continent has.
|
|
190
|
+
* @returns The land's percent of the whole sphere, and each continent's percent of the land.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* import { landShares } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
195
|
+
*
|
|
196
|
+
* const shares = landShares();
|
|
197
|
+
* console.log(Math.round(shares.landPercent), shares.continents.asia > shares.continents.africa);
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export function landShares() {
|
|
201
|
+
const { areas, total } = edgesOf();
|
|
202
|
+
const continents = {};
|
|
203
|
+
for (const id of Object.values(GROUP_IDS))
|
|
204
|
+
continents[id] = ((areas.get(id) ?? 0) / total) * 100;
|
|
205
|
+
return { landPercent: (total / (4 * Math.PI)) * 100, continents };
|
|
206
|
+
}
|
|
207
|
+
/** The farthest an island may be from the main land and still be drawn with it (France's Guiana and Hawaii are too far; Japan's and Indonesia's islands are not). */
|
|
208
|
+
const NEAR = 0.52;
|
|
209
|
+
/**
|
|
210
|
+
* One country by its ISO alpha-2 code, for a "my country" sheet: its main land and the islands near it (a far territory such as French Guiana is left out), the continent it is in and the countries it touches.
|
|
211
|
+
* @param alpha2 - The two-letter code, such as `JP`.
|
|
212
|
+
* @returns The shape, or `undefined` when the package has no outline for the code.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* import { countryShape } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
217
|
+
*
|
|
218
|
+
* const france = countryShape("FR");
|
|
219
|
+
* console.log(france?.continent, france?.neighbours.length);
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
export function countryShape(alpha2) {
|
|
223
|
+
const index = LANDS.findIndex((land) => land.alpha2 === alpha2.toUpperCase());
|
|
224
|
+
if (index < 0)
|
|
225
|
+
return undefined;
|
|
226
|
+
const land = LANDS[index];
|
|
227
|
+
const sized = land.polygons.map((rings, which) => ({ rings, which, size: areaOf(rings), centre: geoCentroid({ type: "Polygon", coordinates: rings }) }));
|
|
228
|
+
const main = sized.reduce((a, b) => (b.size > a.size ? b : a));
|
|
229
|
+
const kept = sized.filter((p) => p === main || (geoDistance(p.centre, main.centre) <= NEAR && p.size >= main.size * 0.003));
|
|
230
|
+
// only the land that is drawn: French Guiana's neighbours are not France's on this sheet
|
|
231
|
+
const touching = new Set();
|
|
232
|
+
for (const edge of edgesOf().edges)
|
|
233
|
+
if (kept.some((p) => edge.polygons.has(`${index}:${p.which}`)) && edge.lands.size > 1)
|
|
234
|
+
for (const other of edge.lands)
|
|
235
|
+
if (other !== index)
|
|
236
|
+
touching.add(other);
|
|
237
|
+
return {
|
|
238
|
+
name: land.name,
|
|
239
|
+
ja: land.ja,
|
|
240
|
+
alpha2: land.alpha2,
|
|
241
|
+
continent: land.continent,
|
|
242
|
+
feature: { type: "Feature", properties: {}, geometry: { type: "MultiPolygon", coordinates: kept.map((p) => p.rings) } },
|
|
243
|
+
neighbours: [...touching].map((i) => ({ name: LANDS[i].name, ja: LANDS[i].ja })).sort((a, b) => a.name.localeCompare(b.name)),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* The two-letter codes of the countries that have an outline.
|
|
248
|
+
* @returns The codes, in the order of the outlines.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* import { countryCodes } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
253
|
+
*
|
|
254
|
+
* console.log(countryCodes().includes("JP"));
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
export const countryCodes = () => LANDS.flatMap((land) => (land.alpha2 === null ? [] : [land.alpha2]));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A d3-geo path made fit for a thick line: simpler to within `toleranceMm`, loops whose longest side is under `minLoopMm` left out, corners rounded.
|
|
3
|
+
* @param d - The path as d3-geo's `geoPath` writes it, in millimetres.
|
|
4
|
+
* @param toleranceMm - How far the simpler line may stray from the real one.
|
|
5
|
+
* @param minLoopMm - The smallest island, by its longest side, that is kept.
|
|
6
|
+
* @returns The path.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { thickPath } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
11
|
+
*
|
|
12
|
+
* console.log(thickPath("M0,0L5,0.1L10,0L20,0", 0.5, 0));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function thickPath(d: string, toleranceMm: number, minLoopMm: number): string;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Thick lines hide fine detail, so a coast is made simpler before it is drawn: a path from d3-geo is read back as points in millimetres, the points that matter at the line's thickness are kept (Douglas-Peucker), loops too small to colour
|
|
2
|
+
// are dropped, and the corners are rounded. Pure.
|
|
3
|
+
/** The runs of a path written by d3-geo: `M x,y L x,y ... Z` or without the `Z`. */
|
|
4
|
+
function runsOf(d) {
|
|
5
|
+
const runs = [];
|
|
6
|
+
for (const part of d.split("M").slice(1)) {
|
|
7
|
+
const closed = part.endsWith("Z");
|
|
8
|
+
const points = part
|
|
9
|
+
.replace(/Z$/, "")
|
|
10
|
+
.split("L")
|
|
11
|
+
.map((pair) => {
|
|
12
|
+
const [x, y] = pair.split(",");
|
|
13
|
+
return [Number(x), Number(y)];
|
|
14
|
+
});
|
|
15
|
+
const first = points[0];
|
|
16
|
+
const last = points.at(-1);
|
|
17
|
+
const loops = closed || (points.length > 3 && Math.hypot(first[0] - last[0], first[1] - last[1]) < 1e-6);
|
|
18
|
+
if (loops && Math.hypot(first[0] - last[0], first[1] - last[1]) < 1e-6)
|
|
19
|
+
points.pop();
|
|
20
|
+
runs.push({ points, closed: loops });
|
|
21
|
+
}
|
|
22
|
+
return runs;
|
|
23
|
+
}
|
|
24
|
+
const distance = (p, a, b) => {
|
|
25
|
+
const dx = b[0] - a[0];
|
|
26
|
+
const dy = b[1] - a[1];
|
|
27
|
+
const length = dx * dx + dy * dy;
|
|
28
|
+
if (length === 0)
|
|
29
|
+
return Math.hypot(p[0] - a[0], p[1] - a[1]);
|
|
30
|
+
const t = Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / length));
|
|
31
|
+
return Math.hypot(p[0] - (a[0] + t * dx), p[1] - (a[1] + t * dy));
|
|
32
|
+
};
|
|
33
|
+
/** Douglas-Peucker: the points of a line that leave it within `tolerance` of the simpler line through them. */
|
|
34
|
+
function simplify(points, tolerance) {
|
|
35
|
+
if (points.length < 3)
|
|
36
|
+
return points;
|
|
37
|
+
const keep = new Array(points.length).fill(false);
|
|
38
|
+
keep[0] = true;
|
|
39
|
+
keep[points.length - 1] = true;
|
|
40
|
+
const stack = [[0, points.length - 1]];
|
|
41
|
+
while (stack.length > 0) {
|
|
42
|
+
const [from, to] = stack.pop();
|
|
43
|
+
let far = -1;
|
|
44
|
+
let most = tolerance;
|
|
45
|
+
for (let i = from + 1; i < to; i += 1) {
|
|
46
|
+
const d = distance(points[i], points[from], points[to]);
|
|
47
|
+
if (d > most) {
|
|
48
|
+
most = d;
|
|
49
|
+
far = i;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (far >= 0) {
|
|
53
|
+
keep[far] = true;
|
|
54
|
+
stack.push([from, far], [far, to]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return points.filter((_, i) => keep[i]);
|
|
58
|
+
}
|
|
59
|
+
const r = (v) => (Math.round(v * 100) / 100).toString();
|
|
60
|
+
const mid = (a, b) => [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
61
|
+
/** A run as a path with rounded corners: straight to the middle of the first side, then a curve through each corner. */
|
|
62
|
+
function rounded(run) {
|
|
63
|
+
const p = run.points;
|
|
64
|
+
if (p.length < 3)
|
|
65
|
+
return `M${r(p[0][0])} ${r(p[0][1])}L${r(p.at(-1)[0])} ${r(p.at(-1)[1])}`;
|
|
66
|
+
if (run.closed) {
|
|
67
|
+
const start = mid(p[0], p[1]);
|
|
68
|
+
let d = `M${r(start[0])} ${r(start[1])}`;
|
|
69
|
+
for (let i = 1; i <= p.length; i += 1) {
|
|
70
|
+
const corner = p[i % p.length];
|
|
71
|
+
const next = mid(corner, p[(i + 1) % p.length]);
|
|
72
|
+
d += `Q${r(corner[0])} ${r(corner[1])} ${r(next[0])} ${r(next[1])}`;
|
|
73
|
+
}
|
|
74
|
+
return `${d}Z`;
|
|
75
|
+
}
|
|
76
|
+
let d = `M${r(p[0][0])} ${r(p[0][1])}`;
|
|
77
|
+
const first = mid(p[0], p[1]);
|
|
78
|
+
d += `L${r(first[0])} ${r(first[1])}`;
|
|
79
|
+
for (let i = 1; i < p.length - 1; i += 1) {
|
|
80
|
+
const next = mid(p[i], p[i + 1]);
|
|
81
|
+
d += `Q${r(p[i][0])} ${r(p[i][1])} ${r(next[0])} ${r(next[1])}`;
|
|
82
|
+
}
|
|
83
|
+
return `${d}L${r(p.at(-1)[0])} ${r(p.at(-1)[1])}`;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A d3-geo path made fit for a thick line: simpler to within `toleranceMm`, loops whose longest side is under `minLoopMm` left out, corners rounded.
|
|
87
|
+
* @param d - The path as d3-geo's `geoPath` writes it, in millimetres.
|
|
88
|
+
* @param toleranceMm - How far the simpler line may stray from the real one.
|
|
89
|
+
* @param minLoopMm - The smallest island, by its longest side, that is kept.
|
|
90
|
+
* @returns The path.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* import { thickPath } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
95
|
+
*
|
|
96
|
+
* console.log(thickPath("M0,0L5,0.1L10,0L20,0", 0.5, 0));
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function thickPath(d, toleranceMm, minLoopMm) {
|
|
100
|
+
const out = [];
|
|
101
|
+
for (const run of runsOf(d)) {
|
|
102
|
+
if (run.points.length < 2)
|
|
103
|
+
continue;
|
|
104
|
+
if (run.closed) {
|
|
105
|
+
const xs = run.points.map((p) => p[0]);
|
|
106
|
+
const ys = run.points.map((p) => p[1]);
|
|
107
|
+
if (Math.max(Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys)) < minLoopMm)
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const points = simplify(run.points, toleranceMm);
|
|
111
|
+
if (run.closed ? points.length < 3 : points.length < 2)
|
|
112
|
+
continue;
|
|
113
|
+
out.push(rounded({ points, closed: run.closed }));
|
|
114
|
+
}
|
|
115
|
+
return out.join("");
|
|
116
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { SheetOptions } from "./earth.ts";
|
|
2
|
+
/** How far the view of the day and night globe is tipped to show the north (degrees): the line between day and night is then a curve. */
|
|
3
|
+
export declare const DAY_NIGHT_TIP = 20;
|
|
4
|
+
/**
|
|
5
|
+
* The turn of the globe on the day and night sheet for a moment: the Sun's longitude less 90 degrees at the middle, so the lit half is on the right, tipped to show the north.
|
|
6
|
+
* @param ms - The moment, in milliseconds since the Unix epoch.
|
|
7
|
+
* @returns The place at the middle of the globe, the place where the Sun is straight overhead and the roll of the globe about the line of sight, in degrees.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { dayNightView } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
12
|
+
*
|
|
13
|
+
* const view = dayNightView(Date.UTC(2026, 5, 21, 12));
|
|
14
|
+
* console.log(view.sun.lat.toFixed(1));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function dayNightView(ms: number): {
|
|
18
|
+
centre: [number, number];
|
|
19
|
+
sun: {
|
|
20
|
+
lon: number;
|
|
21
|
+
lat: number;
|
|
22
|
+
};
|
|
23
|
+
roll: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The line between day and night at a moment: the points 90 degrees from where the Sun is straight overhead, as longitude and latitude.
|
|
27
|
+
* @param ms - The moment, in milliseconds since the Unix epoch.
|
|
28
|
+
* @param points - How many points (at least 3, default 72).
|
|
29
|
+
* @returns The points in order round the Earth.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { terminator } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
34
|
+
*
|
|
35
|
+
* console.log(terminator(Date.UTC(2026, 2, 20, 12), 8).length);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function terminator(ms: number, points?: number): [number, number][];
|
|
39
|
+
/**
|
|
40
|
+
* The day and night sheet: the Earth with the Sun's light coming from the right, the lit half left white (day) and the dark half hatched (night), the line between them thick, and three facts, for the date and time you give. The line is worked out from the package's own sun position.
|
|
41
|
+
* @param ms - The moment, in milliseconds since the Unix epoch.
|
|
42
|
+
* @param options - The room, the language and the type.
|
|
43
|
+
* @returns The `<svg>` markup, carrying the place of the Sun as `data-sun`.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { dayNightSvg } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
48
|
+
*
|
|
49
|
+
* const svg = dayNightSvg(Date.UTC(2026, 5, 21, 12), { widthMm: 184.6, heightMm: 180 });
|
|
50
|
+
* console.log(svg.includes("data-sun="), svg.includes("Night"));
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function dayNightSvg(ms: number, options: SheetOptions): string;
|
|
54
|
+
/**
|
|
55
|
+
* The seasons sheet: the Sun in the middle, the Earth's orbit as a ring, and the Earth at four points of it (March, June, September and December), each with its tilt, the dark half hatched, the season of the northern half named, and three facts.
|
|
56
|
+
* The axis leans the same way at all four, so in June the north leans toward the Sun and in December away from it.
|
|
57
|
+
* @param options - The room, the language and the type.
|
|
58
|
+
* @returns The `<svg>` markup.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { seasonsSvg } from "@johnmorrisdotca/chikyu-earth/sheets";
|
|
63
|
+
*
|
|
64
|
+
* const svg = seasonsSvg({ widthMm: 184.6, heightMm: 180, lang: "en" });
|
|
65
|
+
* console.log(svg.includes("Summer"), svg.includes("Winter"));
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function seasonsSvg(options: SheetOptions): string;
|