@johnmorrisdotca/chikyu-earth 0.46.5 → 0.47.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.
@@ -0,0 +1,308 @@
1
+ import { BEAM_COLOUR, beamCells, footprintAngle, footprintKm } from "./geometry.js";
2
+ /** Earth radii: the Earth's radius in kilometres, the unit the scene measures in. */
3
+ const EARTH_KM = 6371;
4
+ /** The footprint sits this far above the ground (in Earth radii): over the map tiles and the sphere's flat facets, as the grid does. */
5
+ const GROUND = 1.0015;
6
+ const CONE_SEGMENTS = 72;
7
+ const DISC_SEGMENTS = 64;
8
+ const DISC_RINGS = 6;
9
+ export const newBeamState = () => ({ on: false, elevation: 25, cells: 0, cellSize: 0, colour: BEAM_COLOUR, strength: 0.6, stats: { shown: false, heightKm: 0, radiusKm: 0, angleDegrees: 0, elevation: 25, cells: 0, cellRadiusKm: 0, builds: 0, failed: false } });
10
+ // The footprint's flat units (its radius is 1) mapped onto the sphere about the point below the satellite, the angle growing with the distance from the middle: the same map the rings use.
11
+ const MAP = /* glsl */ `
12
+ vec3 ground(vec2 p) {
13
+ float r = length(p);
14
+ vec2 d = r > 1e-6 ? p / r : vec2(1.0, 0.0);
15
+ float a = r * lambda;
16
+ return (nadir * cos(a) + (e1 * d.x + e2 * d.y) * sin(a)) * ${GROUND.toFixed(4)};
17
+ }`;
18
+ const CONE_VERT = /* glsl */ `
19
+ attribute vec4 cone;
20
+ attribute float shaft;
21
+ uniform vec3 sat;
22
+ uniform vec3 nadir;
23
+ uniform vec3 e1;
24
+ uniform vec3 e2;
25
+ uniform float lambda;
26
+ uniform float pitch;
27
+ uniform float circle;
28
+ uniform float weights;
29
+ uniform float cone0;
30
+ uniform float thin;
31
+ varying float vT;
32
+ varying float vFace;
33
+ varying float vRay;
34
+ varying float vDist;
35
+ varying float vWeight;
36
+ ${MAP}
37
+ // the footprint's own cone (shaft 0) or the thin one to a cell (shaft 1)
38
+ vec3 rim(float phi) { return shaft > 0.5 ? ground(cone.zw * pitch + circle * thin * vec2(cos(phi), sin(phi))) : ground(vec2(cos(phi), sin(phi))); }
39
+ vec3 at(float t, float phi) { return mix(sat, rim(phi), t); }
40
+ void main() {
41
+ float t = cone.x;
42
+ float s = clamp(t, 0.05, 1.0);
43
+ vec3 p = at(t, cone.y);
44
+ vec3 dp = at(s, cone.y + 0.01) - at(s, cone.y - 0.01);
45
+ vec3 n = normalize(cross(dp, rim(cone.y) - sat));
46
+ vec3 v = cameraPosition - p;
47
+ vDist = length(v);
48
+ vFace = abs(dot(n, normalize(v)));
49
+ vT = t;
50
+ vRay = cone.y;
51
+ vWeight = shaft > 0.5 ? weights : cone0;
52
+ gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0);
53
+ }`;
54
+ const CONE_FRAG = /* glsl */ `
55
+ uniform vec3 colour;
56
+ uniform float strength;
57
+ varying float vT;
58
+ varying float vFace;
59
+ varying float vRay;
60
+ varying float vDist;
61
+ varying float vWeight;
62
+ void main() {
63
+ // brightest at the satellite, fading towards the ground; soft at the edges (a shaft is thickest through its middle), and a little streaked round
64
+ float along = 0.04 + 0.96 * pow(1.0 - vT, 1.7);
65
+ float body = 0.1 + 0.9 * pow(vFace, 1.4);
66
+ float streaks = 0.88 + 0.12 * sin(vRay * 36.0);
67
+ float near = smoothstep(0.004, 0.06, vDist);
68
+ float a = strength * vWeight * along * body * streaks * near;
69
+ gl_FragColor = vec4(colour, a);
70
+ }`;
71
+ // The footprint itself (kinds 2 and 3) and the cells (kinds 0 and 1), each a faint fill with a soft inner glow and a thin bright line, mapped from the footprint's flat units onto the sphere.
72
+ // The line is drawn in a band either side of the edge and shaped in pixels (from how fast the radius changes across the screen), so it is as thin on a small cell as on the whole footprint.
73
+ const DISC_VERT = /* glsl */ `
74
+ attribute vec4 disc;
75
+ attribute vec2 pol;
76
+ uniform vec3 nadir;
77
+ uniform vec3 e1;
78
+ uniform vec3 e2;
79
+ uniform float lambda;
80
+ uniform float pitch;
81
+ uniform float circle;
82
+ varying float vRho;
83
+ varying float vRing;
84
+ varying float vFill;
85
+ varying float vShare;
86
+ ${MAP}
87
+ void main() {
88
+ bool foot = disc.w > 1.5;
89
+ vec2 q = vec2(cos(pol.y), sin(pol.y)) * pol.x;
90
+ vec2 p = foot ? q : disc.xy * pitch + circle * q;
91
+ vRho = pol.x;
92
+ vRing = mod(disc.w, 2.0);
93
+ vFill = foot ? 0.05 : 0.04;
94
+ vShare = foot ? 1.0 : 0.8;
95
+ gl_Position = projectionMatrix * viewMatrix * vec4(ground(p), 1.0);
96
+ }`;
97
+ const DISC_FRAG = /* glsl */ `
98
+ uniform vec3 colour;
99
+ uniform float strength;
100
+ varying float vRho;
101
+ varying float vRing;
102
+ varying float vFill;
103
+ varying float vShare;
104
+ void main() {
105
+ float a;
106
+ if (vRing > 0.5) {
107
+ float d = (vRho - 1.0) / max(fwidth(vRho), 1e-5);
108
+ float line = 1.0 - smoothstep(0.35, 1.1, abs(d));
109
+ float glow = d < 0.0 ? 0.34 * exp(d / 7.0) : 0.2 * exp(-d / 4.0);
110
+ a = vShare * min(1.0, line + glow);
111
+ } else {
112
+ a = vFill * (0.25 + 0.75 * pow(clamp(vRho, 0.0, 1.0), 5.0));
113
+ }
114
+ gl_FragColor = vec4(colour, strength * a);
115
+ }`;
116
+ /** The cones: the footprint's own, then one thin one for each lattice point. Each is a strip from the apex (t = 0) to the rim (t = 1), every phi round. */
117
+ function coneGeometry(three, lattice) {
118
+ const verts = [];
119
+ const shaft = [];
120
+ const index = [];
121
+ const add = (x, y, thin, rows, segments) => {
122
+ const base = verts.length / 4;
123
+ for (let r = 0; r <= rows; r += 1) {
124
+ for (let s = 0; s <= segments; s += 1) {
125
+ verts.push(r / rows, (s / segments) * Math.PI * 2, x, y);
126
+ shaft.push(thin);
127
+ }
128
+ }
129
+ for (let r = 0; r < rows; r += 1) {
130
+ for (let s = 0; s < segments; s += 1) {
131
+ const a = base + r * (segments + 1) + s;
132
+ const b = a + segments + 1;
133
+ index.push(a, b, a + 1, a + 1, b, b + 1);
134
+ }
135
+ }
136
+ };
137
+ add(0, 0, 0, 10, CONE_SEGMENTS);
138
+ for (const point of lattice)
139
+ add(point[0], point[1], 1, 6, 24);
140
+ const geometry = new three.BufferGeometry();
141
+ geometry.setAttribute("cone", new three.BufferAttribute(new Float32Array(verts), 4));
142
+ geometry.setAttribute("shaft", new three.BufferAttribute(new Float32Array(shaft), 1));
143
+ geometry.setAttribute("position", new three.BufferAttribute(new Float32Array((verts.length / 4) * 3), 3));
144
+ geometry.setIndex(index);
145
+ return geometry;
146
+ }
147
+ /** The ground: the footprint's disc and ring (kinds 2 and 3), then each cell's (kinds 0 and 1). */
148
+ function groundGeometry(three, lattice) {
149
+ const disc = [];
150
+ const pol = [];
151
+ const index = [];
152
+ const push = (x, y, kind, rho, phi, band = 0) => {
153
+ disc.push(x, y, band, kind);
154
+ pol.push(rho, phi);
155
+ return pol.length / 2 - 1;
156
+ };
157
+ const circleAt = (x, y, fillKind) => {
158
+ const fill = push(x, y, fillKind, 0, 0);
159
+ const first = pol.length / 2;
160
+ for (let r = 1; r <= DISC_RINGS; r += 1)
161
+ for (let s = 0; s < DISC_SEGMENTS; s += 1)
162
+ push(x, y, fillKind, (r / DISC_RINGS) * 0.985, (s / DISC_SEGMENTS) * Math.PI * 2);
163
+ for (let s = 0; s < DISC_SEGMENTS; s += 1)
164
+ index.push(fill, first + s, first + ((s + 1) % DISC_SEGMENTS));
165
+ for (let r = 1; r < DISC_RINGS; r += 1) {
166
+ for (let s = 0; s < DISC_SEGMENTS; s += 1) {
167
+ const a = first + (r - 1) * DISC_SEGMENTS + s;
168
+ const b = first + (r - 1) * DISC_SEGMENTS + ((s + 1) % DISC_SEGMENTS);
169
+ index.push(a, a + DISC_SEGMENTS, b, b, a + DISC_SEGMENTS, b + DISC_SEGMENTS);
170
+ }
171
+ }
172
+ // the ring: a band just inside the edge, brightest in the middle of it
173
+ const band = pol.length / 2;
174
+ for (const w of [0, 1])
175
+ for (let s = 0; s < DISC_SEGMENTS; s += 1)
176
+ push(x, y, fillKind + 1, 0.82 + 0.36 * w, (s / DISC_SEGMENTS) * Math.PI * 2, w);
177
+ for (let s = 0; s < DISC_SEGMENTS; s += 1) {
178
+ const a = band + s;
179
+ const b = band + ((s + 1) % DISC_SEGMENTS);
180
+ index.push(a, a + DISC_SEGMENTS, b, b, a + DISC_SEGMENTS, b + DISC_SEGMENTS);
181
+ }
182
+ };
183
+ circleAt(0, 0, 2);
184
+ for (const point of lattice)
185
+ circleAt(point[0], point[1], 0);
186
+ const geometry = new three.BufferGeometry();
187
+ geometry.setAttribute("disc", new three.BufferAttribute(new Float32Array(disc), 4));
188
+ geometry.setAttribute("pol", new three.BufferAttribute(new Float32Array(pol), 2));
189
+ geometry.setAttribute("position", new three.BufferAttribute(new Float32Array((pol.length / 2) * 3), 3));
190
+ geometry.setIndex(index);
191
+ return geometry;
192
+ }
193
+ /**
194
+ * Makes the beam in `scene` (the world the satellites are in, not the Earth's turning group: the footprint is worked out for the satellite's place now).
195
+ *
196
+ * @param three - The page's three.js; it must have `AdditiveBlending`.
197
+ * @param scene - The scene the satellites are in.
198
+ * @param state - The model's beam.
199
+ * @param order - The render order: after the satellites' dots.
200
+ * @returns The layer.
201
+ */
202
+ export function createBeamLayer(three, scene, state, order) {
203
+ if (three.AdditiveBlending === undefined)
204
+ throw new Error("the three.js handed over has no AdditiveBlending, which the beam needs");
205
+ const blending = three.AdditiveBlending;
206
+ const uniforms = {
207
+ sat: { value: new three.Vector3() },
208
+ nadir: { value: new three.Vector3(0, 1, 0) },
209
+ e1: { value: new three.Vector3(1, 0, 0) },
210
+ e2: { value: new three.Vector3(0, 0, 1) },
211
+ lambda: { value: 0 },
212
+ colour: { value: new three.Color(state.colour) },
213
+ strength: { value: state.strength },
214
+ pitch: { value: 0.2 },
215
+ circle: { value: 0.1 },
216
+ weights: { value: 0.3 },
217
+ cone0: { value: 0.42 },
218
+ thin: { value: 0.5 },
219
+ };
220
+ // forceSinglePass: a double-sided transparent material is drawn twice (back faces, then front); added light does not care about the order, so once is enough and the beam costs half the draw calls.
221
+ // Two draw calls in all: the cones (the footprint's own and the thin one to each cell), and the ground (the footprint's disc and ring and the cells').
222
+ const common = { transparent: true, depthWrite: false, blending, forceSinglePass: true, side: three.DoubleSide, uniforms };
223
+ const coneMaterial = new three.ShaderMaterial({ vertexShader: CONE_VERT, fragmentShader: CONE_FRAG, ...common });
224
+ const groundMaterial = new three.ShaderMaterial({ vertexShader: DISC_VERT, fragmentShader: DISC_FRAG, ...common });
225
+ let cones = null;
226
+ let ground = null;
227
+ let cellKey = "";
228
+ const make = (lattice) => {
229
+ for (const old of [cones, ground]) {
230
+ if (!old)
231
+ continue;
232
+ scene.remove(old);
233
+ old.geometry.dispose();
234
+ }
235
+ cones = new three.Mesh(coneGeometry(three, lattice), coneMaterial);
236
+ ground = new three.Mesh(groundGeometry(three, lattice), groundMaterial);
237
+ for (const part of [cones, ground]) {
238
+ part.frustumCulled = false;
239
+ part.renderOrder = order;
240
+ part.visible = false;
241
+ scene.add(part);
242
+ }
243
+ state.stats.builds += 1;
244
+ };
245
+ const stats = state.stats;
246
+ const up = new three.Vector3(0, 1, 0);
247
+ const side = new three.Vector3();
248
+ return {
249
+ sync(position) {
250
+ const height = position ? Math.hypot(position[0], position[1], position[2]) : 0;
251
+ const on = state.on && position !== null && height > 1;
252
+ stats.shown = on;
253
+ if (cones)
254
+ cones.visible = on;
255
+ if (ground)
256
+ ground.visible = on;
257
+ if (!on || !position) {
258
+ stats.heightKm = 0;
259
+ stats.radiusKm = 0;
260
+ stats.angleDegrees = 0;
261
+ stats.cells = 0;
262
+ stats.cellRadiusKm = 0;
263
+ return;
264
+ }
265
+ const heightKm = (height - 1) * EARTH_KM;
266
+ const lambda = footprintAngle(heightKm, state.elevation);
267
+ stats.heightKm = heightKm;
268
+ stats.radiusKm = footprintKm(heightKm, state.elevation);
269
+ stats.angleDegrees = (lambda * 180) / Math.PI;
270
+ stats.elevation = state.elevation;
271
+ // the cells: the geometry is made again only when their number changes (and the first time); their size follows the footprint
272
+ const wanted = beamCells(state.cells, stats.radiusKm, state.cellSize);
273
+ const key = `${wanted.lattice.length}`;
274
+ if (!cones || key !== cellKey) {
275
+ cellKey = key;
276
+ make(wanted.lattice);
277
+ }
278
+ cones.visible = true;
279
+ ground.visible = true;
280
+ uniforms.sat.value.set(position[0], position[1], position[2]);
281
+ uniforms.nadir.value.set(position[0] / height, position[1] / height, position[2] / height);
282
+ side.copy(Math.abs(uniforms.nadir.value.y) > 0.99 ? side.set(1, 0, 0) : up).cross(uniforms.nadir.value).normalize();
283
+ uniforms.e1.value.copy(side);
284
+ uniforms.e2.value.copy(uniforms.nadir.value).cross(side);
285
+ uniforms.lambda.value = lambda;
286
+ uniforms.colour.value.set(state.colour);
287
+ uniforms.strength.value = state.strength;
288
+ uniforms.pitch.value = wanted.pitch;
289
+ uniforms.circle.value = wanted.radius;
290
+ // the shafts are the beam's body when there are cells (the footprint's own cone is then a faint haze); each is dimmer the more there are, since they add up at the satellite
291
+ uniforms.weights.value = 0.7 / Math.sqrt(Math.max(1, wanted.lattice.length / 7));
292
+ uniforms.cone0.value = wanted.lattice.length > 0 ? 0.12 : 0.42;
293
+ stats.cells = wanted.lattice.length;
294
+ stats.cellRadiusKm = wanted.lattice.length > 0 ? wanted.radius * stats.radiusKm : 0;
295
+ },
296
+ dispose() {
297
+ for (const part of [cones, ground]) {
298
+ if (!part)
299
+ continue;
300
+ scene.remove(part);
301
+ part.geometry.dispose();
302
+ }
303
+ coneMaterial.dispose();
304
+ groundMaterial.dispose();
305
+ stats.shown = false;
306
+ },
307
+ };
308
+ }
@@ -12,6 +12,8 @@ export interface SatelliteMark {
12
12
  x: number;
13
13
  y: number;
14
14
  visible: boolean;
15
+ /** The Earth hides it. The name is dimmed and the ring is left off, unless dimming is off. */
16
+ occluded?: boolean;
15
17
  }
16
18
  /**
17
19
  * The most places in sight drawn as markers of their own (a beam, a pin or a ring standing out of the ground, and a name): `here` first, then the rest in the order given. Every other place is a ring on the ground, all of them one draw call.
@@ -73,6 +75,8 @@ export interface PlaceLayer {
73
75
  * fading at the Earth's edge and dimmer at night, and `stats.omitted` counts the places in sight it had no room for. `stats.sprites` counts only the rings the picture draws; `stats.dots` counts the circles.
74
76
  */
75
77
  ringsInPicture: boolean;
78
+ /** Dim a name the Earth hides, and leave its ring off. On by default. Off leaves that name out. */
79
+ dimOccluded: boolean;
76
80
  readonly stats: {
77
81
  places: number;
78
82
  shown: number;
@@ -28,6 +28,7 @@ export const PLACE_STYLE = "#places{position:absolute;inset:0;overflow:hidden;po
28
28
  ".pl-name[data-kind=group]{padding:0 5px;font-size:10px;line-height:14px;border-radius:7px;background:rgba(4,10,20,.6)}" +
29
29
  ".pl-name[data-night]{opacity:.82}" +
30
30
  ".pl-name[data-kind=satellite]{--c:#cfeaff;color:#cfeaff;font-weight:500;background:rgba(4,10,20,.5);border-color:transparent}" +
31
+ ".pl-name[data-occluded]{opacity:.45}" +
31
32
  ".pl-sat{position:absolute;left:-8px;top:-8px;width:16px;height:16px;box-sizing:border-box;border:1.2px solid #cfeaff;border-radius:50%;opacity:.85;will-change:transform}" +
32
33
  ".pl-sat[hidden]{display:none}" +
33
34
  ".pl-dot{position:absolute;left:0;top:0;box-sizing:border-box;border:1.5px solid var(--c);border-radius:50%;opacity:.9}" +
@@ -93,6 +94,7 @@ export function createPlaceLayer(box) {
93
94
  const stats = { places: 0, shown: 0, labels: 0, renders: 0, rings: 0, satelliteNames: 0, marks: 0, groups: 0, sprites: 0, dots: 0, omitted: 0 };
94
95
  let grouping = true;
95
96
  let ringsInPicture = true;
97
+ let dimOccluded = true;
96
98
  // the small circles that stand in for the picture's rings when it cannot draw them: the element, and what it was last written with
97
99
  const dots = [];
98
100
  // where each group fell on the screen the last time the picture could not draw the rings: x, y, facing. Grown when the groups grow, never one array a frame.
@@ -194,7 +196,7 @@ export function createPlaceLayer(box) {
194
196
  if (!list[i].hidden)
195
197
  list[i].hidden = true;
196
198
  };
197
- const showName = (name, text, kind, left, top, color, night) => {
199
+ const showName = (name, text, kind, left, top, color, night, occluded = false) => {
198
200
  if (name.textContent !== text)
199
201
  name.textContent = text;
200
202
  if (name.dataset["kind"] !== kind)
@@ -204,6 +206,11 @@ export function createPlaceLayer(box) {
204
206
  else if (name.style.getPropertyValue("--c"))
205
207
  name.style.removeProperty("--c");
206
208
  name.toggleAttribute("data-night", night);
209
+ name.toggleAttribute("data-occluded", occluded);
210
+ if (occluded)
211
+ name.style.opacity = "0.45";
212
+ else if (name.style.opacity === "0.45")
213
+ name.style.opacity = "";
207
214
  if (name.hidden)
208
215
  name.hidden = false;
209
216
  name.style.transform = `translate(${Math.round(left)}px,${Math.round(top)}px)`;
@@ -346,6 +353,12 @@ export function createPlaceLayer(box) {
346
353
  set ringsInPicture(value) {
347
354
  ringsInPicture = value;
348
355
  },
356
+ get dimOccluded() {
357
+ return dimOccluded;
358
+ },
359
+ set dimOccluded(value) {
360
+ dimOccluded = value;
361
+ },
349
362
  get sprites() {
350
363
  return sprites;
351
364
  },
@@ -507,8 +520,9 @@ export function createPlaceLayer(box) {
507
520
  }
508
521
  hideDots(drawnDots);
509
522
  stats.omitted = omitted;
510
- // the ringed satellites: a ring round each that can be seen, and its name where it is free of the places' names and the boxes taken
523
+ // a ring round each satellite that can be seen. A name the Earth hides is dimmed and has no ring, unless that dimming is off, in which case the name is left out.
511
524
  const seen = satellites.filter((mark) => mark.visible);
525
+ const labelled = dimOccluded ? satellites.filter((mark) => mark.visible || mark.occluded) : seen;
512
526
  for (const [i, mark] of seen.entries()) {
513
527
  let ring = rings[i];
514
528
  if (!ring) {
@@ -524,18 +538,18 @@ export function createPlaceLayer(box) {
524
538
  hideFrom(rings, seen.length);
525
539
  const items = [];
526
540
  const owners = [];
527
- measureAll(seen.filter((mark) => mark.label).map((mark) => ({ label: mark.label, kind: "satellite" })));
528
- for (const [i, mark] of seen.entries()) {
541
+ measureAll(labelled.filter((mark) => mark.label).map((mark) => ({ label: mark.label, kind: "satellite" })));
542
+ for (const [i, mark] of labelled.entries()) {
529
543
  const found = mark.label ? size({ label: mark.label, kind: "satellite" }) : null;
530
544
  if (!found)
531
545
  continue;
532
- items.push({ x: mark.x, y: mark.y, r: SATELLITE_RING + 2, w: found.width, h: found.height, priority: -i });
546
+ items.push({ x: mark.x, y: mark.y, r: mark.occluded ? 2 : SATELLITE_RING + 2, w: found.width, h: found.height, priority: -i });
533
547
  owners.push(mark);
534
548
  }
535
549
  const placed = placeLabels(items, view.width, view.height, 24, 64, busy);
536
550
  for (const spot of placed) {
537
551
  const mark = owners[spot.item];
538
- showName(nameAt(used), mark.label, "satellite", spot.left, spot.top, undefined, false);
552
+ showName(nameAt(used), mark.label, "satellite", spot.left, spot.top, undefined, false, mark.occluded === true);
539
553
  used += 1;
540
554
  }
541
555
  hideFrom(names, used);
@@ -0,0 +1,230 @@
1
+ import { type HereTown } from "../markers/here-city.ts";
2
+ import { PLACE_COAST_KM } from "./land.ts";
3
+ import { SEA_REACH_KM } from "./water.ts";
4
+ export { PLACE_COAST_KM, SEA_REACH_KM };
5
+ /**
6
+ * A named place: an id, and the English and Japanese names.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import type { PlaceName } from "@johnmorrisdotca/chikyu-earth/place";
11
+ *
12
+ * const pacific: PlaceName = { id: "pacific-ocean", name: "Pacific Ocean", nameJa: "太平洋" };
13
+ * console.log(pacific.name);
14
+ * ```
15
+ */
16
+ export interface PlaceName {
17
+ id: string;
18
+ name: string;
19
+ nameJa: string;
20
+ }
21
+ /**
22
+ * A country from the outlines. `code` is the ISO 3166-1 alpha-2 code, or null where the outline has none.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import type { PlaceCountry } from "@johnmorrisdotca/chikyu-earth/place";
27
+ *
28
+ * const japan: PlaceCountry = { code: "JP", name: "Japan", nameJa: "日本" };
29
+ * console.log(japan.code);
30
+ * ```
31
+ */
32
+ export interface PlaceCountry {
33
+ code: string | null;
34
+ name: string;
35
+ nameJa: string;
36
+ }
37
+ /**
38
+ * The nearest country when the point is outside every outline and within {@link PLACE_COAST_KM} of one.
39
+ * The 1:110m line is not trusted that close to a coast, so this is not reported as `country`.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import type { PlaceNear } from "@johnmorrisdotca/chikyu-earth/place";
44
+ *
45
+ * const off: PlaceNear = { code: "JP", name: "Japan", nameJa: "日本", distanceKm: 12 };
46
+ * console.log(off.distanceKm);
47
+ * ```
48
+ */
49
+ export interface PlaceNear extends PlaceCountry {
50
+ distanceKm: number;
51
+ }
52
+ /**
53
+ * The nearest city, from the lists the caller passed. `nameJa` is the short Japanese name, then the Japanese name, and the English name when neither is known.
54
+ * `bearingDeg` is degrees clockwise from north, from the point to the city, or 0 when the city has no coordinates.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * import type { PlaceCity } from "@johnmorrisdotca/chikyu-earth/place";
59
+ *
60
+ * const paris: PlaceCity = { name: "Paris", nameJa: "パリ", countryCode: "FR", distanceKm: 1, bearingDeg: 20 };
61
+ * console.log(paris.name);
62
+ * ```
63
+ */
64
+ export interface PlaceCity {
65
+ name: string;
66
+ nameJa: string;
67
+ countryCode: string | null;
68
+ distanceKm: number;
69
+ bearingDeg: number;
70
+ }
71
+ /**
72
+ * What {@link placeOf} returns. `country` is set only when the point is inside an outline. `near` is set only when it is not, and a country lies within {@link PLACE_COAST_KM}.
73
+ * `sea` is set when `country` is not, and a named water is within its reach. `continent` follows the country or the nearby country, and is null over open water.
74
+ * Oceania is not one of the seven continents in the sea-names list: an island country chizu groups there has `id` `oceania` and chizu's own names. Australia the country uses the Australia continent.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * import type { PlaceOf } from "@johnmorrisdotca/chikyu-earth/place";
79
+ *
80
+ * const empty: PlaceOf = { country: null, near: null, sea: null, continent: null, nearestCity: null };
81
+ * console.log(empty.country);
82
+ * ```
83
+ */
84
+ export interface PlaceOf {
85
+ country: PlaceCountry | null;
86
+ near: PlaceNear | null;
87
+ sea: PlaceName | null;
88
+ continent: PlaceName | null;
89
+ nearestCity: PlaceCity | null;
90
+ }
91
+ /**
92
+ * The city lists for {@link placeOf}. The three tiers here-city loads (capitals, a million, 100,000) match this.
93
+ * `towns` is read only when the caller passes it, and only for the one or two countries those tiers name, the same rule as here-city.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * import type { PlaceCities } from "@johnmorrisdotca/chikyu-earth/place";
98
+ *
99
+ * const cities: PlaceCities = { nearest: () => null };
100
+ * console.log(cities.nearest({ lat: 0, lon: 0 }));
101
+ * ```
102
+ */
103
+ export interface PlaceCities {
104
+ nearest(point: {
105
+ lat: number;
106
+ lon: number;
107
+ }, options?: {
108
+ maxKm?: number;
109
+ }): {
110
+ city: {
111
+ id: string;
112
+ name: {
113
+ en: string;
114
+ ja?: string;
115
+ jaShort?: string;
116
+ };
117
+ country?: string;
118
+ lat?: number;
119
+ lon?: number;
120
+ };
121
+ km: number;
122
+ } | null;
123
+ within?(point: {
124
+ lat: number;
125
+ lon: number;
126
+ }, maxKm: number): readonly {
127
+ city: {
128
+ country?: string;
129
+ };
130
+ km: number;
131
+ }[];
132
+ }
133
+ /**
134
+ * Options for {@link placeOf} and {@link alongTrack}. Without `cities`, `nearestCity` is null. `maxKm` defaults to 100 and is at most 20,000.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * import type { PlaceOptions } from "@johnmorrisdotca/chikyu-earth/place";
139
+ *
140
+ * const options: PlaceOptions = { maxKm: 40 };
141
+ * console.log(options.maxKm);
142
+ * ```
143
+ */
144
+ export interface PlaceOptions {
145
+ cities?: PlaceCities;
146
+ towns?: (countryCode: string) => readonly HereTown[];
147
+ maxKm?: number;
148
+ }
149
+ /**
150
+ * The place a ground track is in at one sample: a country, a country it is only near, or a sea or ocean.
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * import type { TrackPlace } from "@johnmorrisdotca/chikyu-earth/place";
155
+ *
156
+ * const france: TrackPlace = { kind: "country", id: "FR", name: "France", nameJa: "フランス" };
157
+ * console.log(france.kind);
158
+ * ```
159
+ */
160
+ export interface TrackPlace {
161
+ kind: "country" | "near" | "sea";
162
+ id: string;
163
+ name: string;
164
+ nameJa: string;
165
+ }
166
+ /**
167
+ * One change along a ground track. `t` is the sample's own time, the first sample where the new place is seen, not a time guessed between samples.
168
+ * The first sample is included, with `leaving` null. `near` is the approaching country.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * import type { TrackChange } from "@johnmorrisdotca/chikyu-earth/place";
173
+ *
174
+ * const change: TrackChange = { t: 0, entering: null, leaving: null };
175
+ * console.log(change.t);
176
+ * ```
177
+ */
178
+ export interface TrackChange {
179
+ t: number;
180
+ entering: TrackPlace | null;
181
+ leaving: TrackPlace | null;
182
+ }
183
+ /**
184
+ * The country, sea or ocean, continent and nearest city at one point.
185
+ * Countries are the line globe's outlines, Natural Earth at 1:110m. A point inside an outline is `country`.
186
+ * A point outside every outline and within {@link PLACE_COAST_KM} (50 km) of one is `near` instead: the coarse coast is not reported as a country.
187
+ * The sea is the nearest named sea, gulf, bay or ocean within its reach ({@link SEA_REACH_KM}); a smaller water's point is nearer, so it wins over the ocean. It is null while `country` is set, and null when no water is within reach.
188
+ * The continent is the country's group in the outlines, named as in the sea-names list. Australia the country uses that continent, not the Oceania group. A point whose continent point is at least 2,500 km nearer than its country's group (eastern Russia, French Guiana) uses the nearer one. The city lists are the caller's: without `cities`, `nearestCity` is null.
189
+ * Towns under 100,000 are read only when `towns` is passed. The outlines are indexed on a 15° grid so a call is cheap enough to make about once a second.
190
+ *
191
+ * @param lon - Degrees east. A value outside -180 to 180 is wrapped.
192
+ * @param lat - Degrees north, -90 to 90.
193
+ * @param options - City lists and the kilometre limit. Omit them to skip the city.
194
+ * @returns The place. Every field is null when `lat` is not a latitude.
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * import { placeOf } from "@johnmorrisdotca/chikyu-earth/place";
199
+ *
200
+ * console.log(placeOf(2.35, 48.86).country?.code);
201
+ * console.log(placeOf(-150, 0).sea?.id);
202
+ * console.log(placeOf(0, 90).country);
203
+ * ```
204
+ */
205
+ export declare function placeOf(lon: number, lat: number, options?: PlaceOptions): PlaceOf;
206
+ /**
207
+ * The ordered changes of place along a ground track. Each point is `{ lon, lat, t }`.
208
+ * A change is reported at the sample that entered the new place. `near` is how a page says the track is approaching a country; the next sample inside it is the country itself.
209
+ *
210
+ * @param points - The track, in order. A point with no latitude is skipped.
211
+ * @param options - The same city lists as {@link placeOf}. They do not change the country or the sea.
212
+ * @returns One row for the first sample, then one row each time the country, the nearby country or the sea changes.
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * import { alongTrack } from "@johnmorrisdotca/chikyu-earth/place";
217
+ *
218
+ * const rows = alongTrack([
219
+ * { lon: -40, lat: 40, t: 0 },
220
+ * { lon: -3.7, lat: 40.4, t: 1 },
221
+ * { lon: 2.35, lat: 48.86, t: 2 },
222
+ * ]);
223
+ * console.log(rows.map((row) => row.entering?.id).join(" "));
224
+ * ```
225
+ */
226
+ export declare function alongTrack(points: readonly {
227
+ lon: number;
228
+ lat: number;
229
+ t: number;
230
+ }[], options?: PlaceOptions): TrackChange[];