@johnmorrisdotca/chikyu-earth 0.46.1 → 0.46.4
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 +8 -0
- package/NOTICE.md +10 -0
- package/README.md +2 -2
- package/dist/door/gibs.d.ts +198 -0
- package/dist/door/gibs.js +403 -0
- package/dist/door/grid.d.ts +201 -0
- package/dist/door/grid.js +251 -0
- package/dist/door/index.d.ts +2 -0
- package/dist/door/index.js +2 -0
- package/dist/door/map.d.ts +14 -0
- package/dist/realistic/element-base.d.ts +6 -0
- package/dist/realistic/element-base.js +52 -7
- package/dist/realistic/index.d.ts +1 -1
- package/dist/realistic/index.js +1 -1
- package/dist/realistic/shaders.js +5 -3
- package/dist/realistic/tile-set.d.ts +11 -1
- package/dist/realistic/tile-set.js +148 -19
- package/dist/realistic/tiles.d.ts +20 -0
- package/dist/realistic/tiles.js +94 -1
- package/dist/space/element-base.d.ts +5 -0
- package/dist/space/element-base.js +41 -5
- package/dist/space/scene.d.ts +2 -0
- package/dist/space/scene.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { tileKeyParts } from "./map.js";
|
|
2
|
+
import { gibsGrid, gridKeys, gridLevelFor, gridOnEarth } from "./grid.js";
|
|
3
|
+
/** The day this service's tile matrix, layers and terms were read. */
|
|
4
|
+
export const GIBS_READ = "2026-11-13";
|
|
5
|
+
/**
|
|
6
|
+
* The id of the source `gibsTiles` makes. A view credits it while its tiles are on the screen.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { GIBS_ID } from "@johnmorrisdotca/chikyu-earth/door";
|
|
11
|
+
*
|
|
12
|
+
* console.log(GIBS_ID);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export const GIBS_ID = "gibs";
|
|
16
|
+
/**
|
|
17
|
+
* How long a tile is given to arrive before the coarser picture is left as it is and the tile is not asked for again while it stays in view.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { GIBS_SLOW_MS } from "@johnmorrisdotca/chikyu-earth/door";
|
|
22
|
+
*
|
|
23
|
+
* console.log(GIBS_SLOW_MS);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const GIBS_SLOW_MS = 2000;
|
|
27
|
+
/** The address of one tile. The matrix, the day and the layer are the source's; the path is `z/row/col`, which is not the package's `level/column/row`. */
|
|
28
|
+
const TILE_URL = "https://gibs.earthdata.nasa.gov/wmts/epsg4326/best";
|
|
29
|
+
/**
|
|
30
|
+
* The acknowledgement NASA asks a client to show, in their words (English, read 2026-11-13). The Japanese is this package's translation of that sentence: NASA published none.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { GIBS_CREDIT } from "@johnmorrisdotca/chikyu-earth/door";
|
|
35
|
+
*
|
|
36
|
+
* console.log(GIBS_CREDIT.en.startsWith("We acknowledge"));
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export const GIBS_CREDIT = {
|
|
40
|
+
en: "We acknowledge the use of imagery provided by services from NASA's Global Imagery Browse Services (GIBS), part of NASA's Earth Science Data and Information System (ESDIS).",
|
|
41
|
+
ja: "NASAの地球科学データ情報システム(ESDIS)の一部である Global Imagery Browse Services(GIBS)が提供する画像を使用していることを記します。",
|
|
42
|
+
};
|
|
43
|
+
const INFO = {
|
|
44
|
+
id: GIBS_ID,
|
|
45
|
+
name: { en: "NASA GIBS imagery", ja: "NASA GIBSの画像" },
|
|
46
|
+
credit: { en: GIBS_CREDIT.en, ja: GIBS_CREDIT.ja },
|
|
47
|
+
licence: { name: "Public domain", nameJa: "パブリックドメイン", url: "https://www.earthdata.nasa.gov/engage/open-data-services-software/earthdata-developer-portal/gibs-api" },
|
|
48
|
+
url: "https://www.earthdata.nasa.gov/engage/open-data-services-software/earthdata-developer-portal/gibs-api",
|
|
49
|
+
retrieved: GIBS_READ,
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The layers a page may ask for. The ranges are the days the capabilities document listed on 2026-11-13; a day outside them is not requested.
|
|
53
|
+
* The two daily layers are true colour with the clouds left in. The shaded relief is the same kind of picture as the package's own level 5, and is only a stand-in.
|
|
54
|
+
*/
|
|
55
|
+
export const GIBS_LAYERS = {
|
|
56
|
+
Landsat_WELD_CorrectedReflectance_TrueColor_Global_Annual: {
|
|
57
|
+
id: "Landsat_WELD_CorrectedReflectance_TrueColor_Global_Annual",
|
|
58
|
+
matrix: "31.25m",
|
|
59
|
+
clouds: false,
|
|
60
|
+
ranges: [],
|
|
61
|
+
// as the capabilities document listed them on 2026-11-13: 1983-12-01 to 1985-12-01, 1988-12-01 to 1990-12-01, 1998-12-01 to 2000-12-01, a picture a year
|
|
62
|
+
years: ["1983-12-01", "1984-12-01", "1985-12-01", "1988-12-01", "1989-12-01", "1990-12-01", "1998-12-01", "1999-12-01", "2000-12-01"],
|
|
63
|
+
// per channel, the median over six land places of our level 5 divided by this layer's mean (NOTICE.md): the package's pictures are darker and warmer
|
|
64
|
+
gain: [0.76, 0.66, 0.55],
|
|
65
|
+
// the sea is (7, 25, 12) there and a dark wooded slope (8, 39, 6) to (22, 54, 7): clear below a luminance of 20, opaque from 32
|
|
66
|
+
clear: [20, 32],
|
|
67
|
+
credit: {
|
|
68
|
+
en: "Landsat imagery: NASA Global Web-Enabled Landsat Data (GWELD), annual 30 m mosaic, distributed by NASA's LP DAAC.",
|
|
69
|
+
ja: "Landsat画像:NASAのGlobal Web-Enabled Landsat Data(GWELD)の年間30 mモザイク。NASAのLP DAACが配布しています。",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
VIIRS_SNPP_CorrectedReflectance_TrueColor: {
|
|
73
|
+
id: "VIIRS_SNPP_CorrectedReflectance_TrueColor",
|
|
74
|
+
matrix: "250m",
|
|
75
|
+
clouds: true,
|
|
76
|
+
ranges: [
|
|
77
|
+
{ start: "2015-11-24", end: "2022-07-27" },
|
|
78
|
+
{ start: "2022-08-10", end: "2024-05-31" },
|
|
79
|
+
{ start: "2024-06-04", end: "2024-07-11" },
|
|
80
|
+
{ start: "2024-07-16", end: "2024-07-24" },
|
|
81
|
+
{ start: "2024-07-29", end: "2024-11-03" },
|
|
82
|
+
{ start: "2024-11-06", end: "2026-04-28" },
|
|
83
|
+
{ start: "2026-04-30", end: "2026-07-10" },
|
|
84
|
+
{ start: "2026-07-16", end: "2026-11-02" },
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
MODIS_Terra_CorrectedReflectance_TrueColor: {
|
|
88
|
+
id: "MODIS_Terra_CorrectedReflectance_TrueColor",
|
|
89
|
+
matrix: "250m",
|
|
90
|
+
clouds: true,
|
|
91
|
+
ranges: [
|
|
92
|
+
{ start: "2000-02-24", end: "2000-04-25" },
|
|
93
|
+
{ start: "2000-04-28", end: "2000-08-06" },
|
|
94
|
+
{ start: "2000-08-18", end: "2001-06-15" },
|
|
95
|
+
{ start: "2001-07-03", end: "2002-03-19" },
|
|
96
|
+
{ start: "2002-03-28", end: "2003-12-16" },
|
|
97
|
+
{ start: "2003-12-24", end: "2008-12-20" },
|
|
98
|
+
{ start: "2008-12-23", end: "2016-02-18" },
|
|
99
|
+
{ start: "2016-02-27", end: "2022-10-10" },
|
|
100
|
+
{ start: "2022-10-23", end: "2026-11-13" },
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
BlueMarble_ShadedRelief_Bathymetry: { id: "BlueMarble_ShadedRelief_Bathymetry", matrix: "500m", clouds: false, ranges: [] },
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* The layer a page gets without naming one: the Landsat annual true-colour mosaic (30 m, no cloud, land only; the year 2000, the newest it has). The daily layers are a named choice: `today` for MODIS Terra.
|
|
107
|
+
* A layer id is taken as it is, and these short names stand for two of them.
|
|
108
|
+
*/
|
|
109
|
+
export const GIBS_DEFAULT_LAYER = "Landsat_WELD_CorrectedReflectance_TrueColor_Global_Annual";
|
|
110
|
+
const DEFAULT_LAYER = GIBS_DEFAULT_LAYER;
|
|
111
|
+
/** The short names `tiles-finer-layer` takes besides a layer's own id: `landsat` (the default) and `today` (the daily picture of MODIS Terra, with its clouds). */
|
|
112
|
+
export const GIBS_NAMES = {
|
|
113
|
+
landsat: GIBS_DEFAULT_LAYER,
|
|
114
|
+
today: "MODIS_Terra_CorrectedReflectance_TrueColor",
|
|
115
|
+
};
|
|
116
|
+
/** How many days, counting the first, the default rule looks back for a day with imagery. */
|
|
117
|
+
export const GIBS_LOOKBACK_DAYS = 10;
|
|
118
|
+
/**
|
|
119
|
+
* True when every sampled pixel is black, which is the picture the service returns for a day it has no swath for (a 200, not a 404).
|
|
120
|
+
*
|
|
121
|
+
* @param pixels - RGBA bytes.
|
|
122
|
+
* @returns Whether every pixel is black.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* import { samplesAreBlank } from "@johnmorrisdotca/chikyu-earth/door";
|
|
127
|
+
*
|
|
128
|
+
* console.log(samplesAreBlank(new Uint8ClampedArray([0, 0, 0, 255])));
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export function samplesAreBlank(pixels) {
|
|
132
|
+
if (pixels.length < 4)
|
|
133
|
+
return false;
|
|
134
|
+
for (let i = 0; i < pixels.length; i += 4)
|
|
135
|
+
if (pixels[i] > 1 || pixels[i + 1] > 1 || pixels[i + 2] > 1)
|
|
136
|
+
return false;
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The UTC day `yyyy-mm-dd` of a moment.
|
|
141
|
+
*
|
|
142
|
+
* @param ms - Milliseconds since the epoch.
|
|
143
|
+
* @returns The day.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* import { utcDay } from "@johnmorrisdotca/chikyu-earth/door";
|
|
148
|
+
*
|
|
149
|
+
* console.log(utcDay(Date.UTC(2026, 5, 21, 12)));
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export function utcDay(ms) {
|
|
153
|
+
return new Date(ms).toISOString().slice(0, 10);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The UTC day before `ms`.
|
|
157
|
+
*
|
|
158
|
+
* @param ms - Milliseconds since the epoch.
|
|
159
|
+
* @returns The day before.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* import { previousUtcDay } from "@johnmorrisdotca/chikyu-earth/door";
|
|
164
|
+
*
|
|
165
|
+
* console.log(previousUtcDay(Date.UTC(2026, 5, 21, 12)));
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export function previousUtcDay(ms) {
|
|
169
|
+
const day = new Date(ms);
|
|
170
|
+
day.setUTCDate(day.getUTCDate() - 1);
|
|
171
|
+
return day.toISOString().slice(0, 10);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Whether the layer may have imagery for a day: inside one of the ranges listed on 2026-11-13, or after the last of them (a daily layer goes on growing, and the list does not). A day before the layer's first day, or in a gap the list shows, has none, and is not asked for.
|
|
175
|
+
*/
|
|
176
|
+
export function gibsHasDay(layer, day) {
|
|
177
|
+
if (layer.ranges.length === 0)
|
|
178
|
+
return true;
|
|
179
|
+
const first = layer.ranges[0];
|
|
180
|
+
const last = layer.ranges[layer.ranges.length - 1];
|
|
181
|
+
if (day < first.start)
|
|
182
|
+
return false;
|
|
183
|
+
if (day > last.end)
|
|
184
|
+
return true;
|
|
185
|
+
return layer.ranges.some((range) => day >= range.start && day <= range.end);
|
|
186
|
+
}
|
|
187
|
+
/** The day a fixed rule names (`previous-day`, `shown`, or one `yyyy-mm-dd`), or `undefined` for `latest`, which has to be looked for. */
|
|
188
|
+
function fixedDay(rule, ms) {
|
|
189
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(rule))
|
|
190
|
+
return rule;
|
|
191
|
+
if (rule === "shown")
|
|
192
|
+
return utcDay(ms);
|
|
193
|
+
if (rule === "previous-day")
|
|
194
|
+
return previousUtcDay(ms);
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
/** A day the layer can serve for a fixed rule (`latest`, the default, starts at the day before `ms`), or `undefined` when it lands where the layer has no imagery (nothing is requested). */
|
|
198
|
+
export function gibsDay(layer, rule, ms) {
|
|
199
|
+
if (layer.years) {
|
|
200
|
+
// one picture a year: the year a page names if the layer has it (any day of that year), else the newest
|
|
201
|
+
const named = /^\d{4}/.test(rule) ? layer.years.find((time) => time.slice(0, 4) === rule.slice(0, 4)) : undefined;
|
|
202
|
+
return named ?? layer.years[layer.years.length - 1];
|
|
203
|
+
}
|
|
204
|
+
if (layer.ranges.length === 0)
|
|
205
|
+
return "default";
|
|
206
|
+
const named = fixedDay(rule, ms) ?? previousUtcDay(ms);
|
|
207
|
+
return gibsHasDay(layer, named) ? named : undefined;
|
|
208
|
+
}
|
|
209
|
+
/** The day before `day`. */
|
|
210
|
+
const dayBefore = (day) => previousUtcDay(Date.parse(`${day}T12:00:00Z`));
|
|
211
|
+
/** The days the default rule looks at, newest first: from `start`, back, skipping days the layer is known to lack, until the layer's first day or `GIBS_LOOKBACK_DAYS` days have been counted. */
|
|
212
|
+
export function gibsCandidates(layer, start) {
|
|
213
|
+
const found = [];
|
|
214
|
+
let day = start;
|
|
215
|
+
for (let count = 0; count < GIBS_LOOKBACK_DAYS; count += 1) {
|
|
216
|
+
if (layer.ranges.length > 0 && day < layer.ranges[0].start)
|
|
217
|
+
break;
|
|
218
|
+
if (gibsHasDay(layer, day))
|
|
219
|
+
found.push(day);
|
|
220
|
+
day = dayBefore(day);
|
|
221
|
+
}
|
|
222
|
+
return found;
|
|
223
|
+
}
|
|
224
|
+
/** The days the default rule has settled, by layer and starting day: a day with imagery, or `null` for none. Kept for the session, so a view that moves asks nothing again. */
|
|
225
|
+
const settled = new Map();
|
|
226
|
+
const probing = new Set();
|
|
227
|
+
/** Forget what the default rule has settled (for a page that wants to look again, and for the tests). */
|
|
228
|
+
export function forgetGibsDays() {
|
|
229
|
+
settled.clear();
|
|
230
|
+
probing.clear();
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Where a tile's luminance (0 to 255) is at or below this, the picture has no data there (the service paints it black) and the loader draws it clear; from `NODATA_FULL` it is opaque, and between the two a share, so the edge of a gap is not a hard
|
|
234
|
+
* black line. A layer may set its own limits (`clear`).
|
|
235
|
+
*/
|
|
236
|
+
export const NODATA_BELOW = 3;
|
|
237
|
+
/** The luminance from which a pixel is wholly opaque (see `NODATA_BELOW`). */
|
|
238
|
+
export const NODATA_FULL = 14;
|
|
239
|
+
const anySignal = (signals) => (typeof AbortSignal.any === "function" ? AbortSignal.any(signals) : signals[0]);
|
|
240
|
+
async function pictureIsBlank(image) {
|
|
241
|
+
const CanvasCtor = globalThis.OffscreenCanvas;
|
|
242
|
+
if (typeof CanvasCtor !== "function")
|
|
243
|
+
return false;
|
|
244
|
+
try {
|
|
245
|
+
const canvas = new CanvasCtor(8, 8);
|
|
246
|
+
const ctx = canvas.getContext("2d");
|
|
247
|
+
if (!ctx)
|
|
248
|
+
return false;
|
|
249
|
+
ctx.drawImage(image, 0, 0, 8, 8);
|
|
250
|
+
return samplesAreBlank(ctx.getImageData(0, 0, 8, 8).data);
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* A map source that reads NASA's imagery service directly. It declares its own grid (512 pixel tiles, the Geographic matrix, which is not the package's pyramid). `need` names no tile for a day outside the layer's range.
|
|
258
|
+
* `load` gives a tile two seconds, and treats a 404 or an all-black tile as no imagery. The clouds are not taken out of the daily layers.
|
|
259
|
+
*
|
|
260
|
+
* @param options - The clock, the layer and the day rule, and the `fetch` to use.
|
|
261
|
+
* @returns The source. Its `layer` is `day`. It does not replace the package's own tiles: a view lays it over them, and only when the view is closer than those tiles serve.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* import { gibsTiles } from "@johnmorrisdotca/chikyu-earth/door";
|
|
266
|
+
*
|
|
267
|
+
* const source = gibsTiles({ time: () => new Date("2026-06-21T12:00:00Z") });
|
|
268
|
+
* console.log(source.need({ lonMin: -1, lonMax: 1, latMin: -1, latMax: 1, layer: "day" }, 50000)[0]);
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
export function gibsTiles(options) {
|
|
272
|
+
const wanted = options.layer ? (GIBS_NAMES[options.layer] ?? options.layer) : DEFAULT_LAYER;
|
|
273
|
+
const known = GIBS_LAYERS[wanted];
|
|
274
|
+
const layer = known ?? { id: wanted, matrix: "250m", clouds: true, ranges: [] };
|
|
275
|
+
const grid = gibsGrid(layer.matrix);
|
|
276
|
+
const rule = options.date ?? "latest";
|
|
277
|
+
const get = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
278
|
+
const decode = options.decode ?? ((blob) => createImageBitmap(blob, { premultiplyAlpha: "none", colorSpaceConversion: "none" }));
|
|
279
|
+
/** One tile of one day, as `load` reads it: nothing for a 404, a blank picture, a slow answer or a failure. */
|
|
280
|
+
const fetchTile = async (key, signal) => {
|
|
281
|
+
const parts = tileKeyParts(key);
|
|
282
|
+
const day = key.split("/")[0];
|
|
283
|
+
const level = parts ? grid.levels.find((one) => one.level === parts.level) : undefined;
|
|
284
|
+
if (!parts || !day || !level)
|
|
285
|
+
throw new RangeError(`"${key}" is not the key of a tile`);
|
|
286
|
+
if (!gridOnEarth(level, parts.col, parts.row, grid.extent))
|
|
287
|
+
throw new RangeError(`"${key}" is off the Earth`);
|
|
288
|
+
const url = `${TILE_URL}/${layer.id}/default/${day}/${layer.matrix}/${parts.level}/${parts.row}/${parts.col}.jpg`;
|
|
289
|
+
const timer = AbortSignal.timeout(options.slowMs && options.slowMs > 0 ? options.slowMs : GIBS_SLOW_MS);
|
|
290
|
+
let response;
|
|
291
|
+
try {
|
|
292
|
+
response = await get(url, { mode: "cors", signal: anySignal([signal, timer]) });
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
296
|
+
}
|
|
297
|
+
if (!response.ok)
|
|
298
|
+
throw new Error(`The tile ${key} could not be fetched (${response.status})`);
|
|
299
|
+
const image = await decode(await response.blob());
|
|
300
|
+
if (await pictureIsBlank(image)) {
|
|
301
|
+
image.close?.();
|
|
302
|
+
throw new Error(`The tile ${key} has no imagery`);
|
|
303
|
+
}
|
|
304
|
+
return { width: image.width > 0 ? image.width : 512, height: image.height > 0 ? image.height : 512, image };
|
|
305
|
+
};
|
|
306
|
+
/** Look for the most recent day with imagery: the tile under the middle of the view, one day at a time, newest first, each given the usual two seconds. The answer, a day or none, is kept. */
|
|
307
|
+
const lookFor = (settledKey, start, place, detail) => {
|
|
308
|
+
probing.add(settledKey);
|
|
309
|
+
void (async () => {
|
|
310
|
+
let found = null;
|
|
311
|
+
for (const day of gibsCandidates(layer, start)) {
|
|
312
|
+
const keys = gridKeys(grid, place, detail, `${day}/`);
|
|
313
|
+
if (keys.length === 0)
|
|
314
|
+
break;
|
|
315
|
+
try {
|
|
316
|
+
const tile = await fetchTile(keys[0], new AbortController().signal);
|
|
317
|
+
tile.image.close?.();
|
|
318
|
+
found = day;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
// no picture for this day here: the day before
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
settled.set(settledKey, found);
|
|
326
|
+
probing.delete(settledKey);
|
|
327
|
+
if (found !== null)
|
|
328
|
+
options.changed?.();
|
|
329
|
+
})();
|
|
330
|
+
};
|
|
331
|
+
// an unknown layer has no published range, so no day is inside it and nothing is requested
|
|
332
|
+
const dayOf = (place, detail) => {
|
|
333
|
+
if (!known)
|
|
334
|
+
return undefined;
|
|
335
|
+
const ms = options.time().getTime();
|
|
336
|
+
if (layer.years || fixedDay(rule, ms) !== undefined || layer.ranges.length === 0)
|
|
337
|
+
return gibsDay(layer, rule, ms);
|
|
338
|
+
const start = previousUtcDay(ms);
|
|
339
|
+
const settledKey = `${layer.id}|${start}`;
|
|
340
|
+
if (settled.has(settledKey))
|
|
341
|
+
return settled.get(settledKey) ?? undefined;
|
|
342
|
+
if (!probing.has(settledKey))
|
|
343
|
+
lookFor(settledKey, start, place, detail);
|
|
344
|
+
return undefined;
|
|
345
|
+
};
|
|
346
|
+
const source = {
|
|
347
|
+
kind: "tiles",
|
|
348
|
+
layer: "day",
|
|
349
|
+
info: layer.credit ? { ...INFO, credit: { en: `${GIBS_CREDIT.en} ${layer.credit.en}`, ja: `${GIBS_CREDIT.ja}${layer.credit.ja}` } } : INFO,
|
|
350
|
+
holes: true,
|
|
351
|
+
// the loader draws the tile with these on the graphics card: clear where it is dark, and each colour multiplied (nothing is done to the pixels on the processor)
|
|
352
|
+
look: { clear: layer.clear ?? [NODATA_BELOW, NODATA_FULL], ...(layer.gain ? { gain: layer.gain } : {}) },
|
|
353
|
+
grid,
|
|
354
|
+
need(place, detail) {
|
|
355
|
+
if (gridLevelFor(grid, detail, 0) === undefined)
|
|
356
|
+
return [];
|
|
357
|
+
const day = dayOf(place, detail);
|
|
358
|
+
if (day === undefined)
|
|
359
|
+
return [];
|
|
360
|
+
return gridKeys(grid, place, detail, `${day}/`);
|
|
361
|
+
},
|
|
362
|
+
levels() {
|
|
363
|
+
return { tileSize: grid.tileSize, levels: grid.levels.map((level) => ({ level: level.level, width: level.width })) };
|
|
364
|
+
},
|
|
365
|
+
load: (key, signal) => fetchTile(key, signal),
|
|
366
|
+
bytes: (tile) => Math.ceil(tile.width * tile.height * 4 * (4 / 3)),
|
|
367
|
+
};
|
|
368
|
+
return source;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* The finer source an element makes from its attributes, the same object while the attributes are unchanged, or `undefined` when finer tiles are off or the view is limited to the package's own data.
|
|
372
|
+
*
|
|
373
|
+
* @param time - The moment the element is showing.
|
|
374
|
+
* @param changed - Called when the source has found the day it will read, so the element draws again.
|
|
375
|
+
* @returns A reader of the element's attributes.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```ts
|
|
379
|
+
* import { createFinerHolder } from "@johnmorrisdotca/chikyu-earth/door";
|
|
380
|
+
*
|
|
381
|
+
* const hold = createFinerHolder(() => new Date("2026-06-21T12:00:00Z"));
|
|
382
|
+
* console.log(hold.source(() => null, false));
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
export function createFinerHolder(time, changed) {
|
|
386
|
+
let key = "";
|
|
387
|
+
let held;
|
|
388
|
+
return {
|
|
389
|
+
source(read, ownOnly) {
|
|
390
|
+
if (ownOnly || read("tiles-finer") !== "gibs")
|
|
391
|
+
return undefined;
|
|
392
|
+
const next = `${read("tiles-finer-layer") ?? ""}\n${read("tiles-finer-date") ?? ""}\n${read("tiles-finer-timeout") ?? ""}`;
|
|
393
|
+
if (held && key === next)
|
|
394
|
+
return held;
|
|
395
|
+
key = next;
|
|
396
|
+
const layer = read("tiles-finer-layer");
|
|
397
|
+
const date = read("tiles-finer-date");
|
|
398
|
+
const slow = Number(read("tiles-finer-timeout"));
|
|
399
|
+
held = gibsTiles({ time, ...(changed ? { changed } : {}), ...(slow > 0 ? { slowMs: slow } : {}), ...(layer ? { layer } : {}), ...(date ? { date } : {}) });
|
|
400
|
+
return held;
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { MapPlace } from "../data-door.ts";
|
|
2
|
+
/**
|
|
3
|
+
* One level of a source's own grid. `width` is the pixels across a full turn of longitude, which is what a view compares with the screen. `west` and `north` are the outer corner of column 0, row 0, in degrees.
|
|
4
|
+
* `tileLon` and `tileLat` are the degrees one tile covers, which may run past the Earth.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import type { MapGridLevel } from "@johnmorrisdotca/chikyu-earth/door";
|
|
9
|
+
*
|
|
10
|
+
* const level: MapGridLevel = { level: 3, width: 5120, columns: 10, rows: 5, west: -180, north: 90, tileLon: 36, tileLat: 36 };
|
|
11
|
+
* console.log(level.columns);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export interface MapGridLevel {
|
|
15
|
+
level: number;
|
|
16
|
+
width: number;
|
|
17
|
+
columns: number;
|
|
18
|
+
rows: number;
|
|
19
|
+
west: number;
|
|
20
|
+
north: number;
|
|
21
|
+
tileLon: number;
|
|
22
|
+
tileLat: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The grid a map source declares when it is not the package's pyramid: the tile size, the Earth's extent in degrees, and one entry per level.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import type { MapGrid } from "@johnmorrisdotca/chikyu-earth/door";
|
|
30
|
+
*
|
|
31
|
+
* const grid: MapGrid = { tileSize: 512, extent: { west: -180, south: -90, east: 180, north: 90 }, levels: [] };
|
|
32
|
+
* console.log(grid.tileSize);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export interface MapGrid {
|
|
36
|
+
tileSize: number;
|
|
37
|
+
extent: {
|
|
38
|
+
west: number;
|
|
39
|
+
south: number;
|
|
40
|
+
east: number;
|
|
41
|
+
north: number;
|
|
42
|
+
};
|
|
43
|
+
levels: readonly MapGridLevel[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Fold a longitude into the half-open range from -180 up to, but not including, 180. 180 and -180 are the same meridian, and both come back as -180.
|
|
47
|
+
*
|
|
48
|
+
* @param lon - A longitude in degrees, which may fall outside one turn.
|
|
49
|
+
* @returns The longitude in `[-180, 180)`.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { wrapLon } from "@johnmorrisdotca/chikyu-earth/door";
|
|
54
|
+
*
|
|
55
|
+
* console.log(wrapLon(180), wrapLon(-180), wrapLon(190));
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function wrapLon(lon: number): number;
|
|
59
|
+
/**
|
|
60
|
+
* The level of a grid whose width is at least `detail` pixels across a full turn, or the finest level when none is, or `undefined` when the grid has no level wider than `minWidth`.
|
|
61
|
+
*
|
|
62
|
+
* @param grid - The source's grid.
|
|
63
|
+
* @param detail - The pixels across a full turn the view wants.
|
|
64
|
+
* @param minWidth - Levels no wider than this are left out (the picture already underneath).
|
|
65
|
+
* @returns The level, or `undefined` when nothing in the grid is finer than `minWidth`.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { gibsGrid, gridLevelFor } from "@johnmorrisdotca/chikyu-earth/door";
|
|
70
|
+
*
|
|
71
|
+
* console.log(gridLevelFor(gibsGrid("250m"), 40000, 32768)?.level);
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function gridLevelFor(grid: MapGrid, detail: number, minWidth: number): MapGridLevel | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* The longitude and latitude a tile covers, before it is cut to the Earth. The south and east edges may fall past the Earth on a coarse level.
|
|
77
|
+
*
|
|
78
|
+
* @param level - The level the tile is on.
|
|
79
|
+
* @param col - The column, from the west.
|
|
80
|
+
* @param row - The row, from the north.
|
|
81
|
+
* @returns The tile's bounds in degrees.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { gibsGrid, gridSpan } from "@johnmorrisdotca/chikyu-earth/door";
|
|
86
|
+
*
|
|
87
|
+
* console.log(gridSpan(gibsGrid("250m").levels[0]!, 0, 0).latMin < -90);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function gridSpan(level: MapGridLevel, col: number, row: number): {
|
|
91
|
+
lonMin: number;
|
|
92
|
+
lonMax: number;
|
|
93
|
+
latMin: number;
|
|
94
|
+
latMax: number;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* True when the tile runs past the Earth's extent (a coarse level's tile is wider than the Earth).
|
|
98
|
+
*
|
|
99
|
+
* @param level - The level.
|
|
100
|
+
* @param col - The column.
|
|
101
|
+
* @param row - The row.
|
|
102
|
+
* @returns Whether any edge falls outside -180 to 180 or -90 to 90.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { gibsGrid, gridPastExtent } from "@johnmorrisdotca/chikyu-earth/door";
|
|
107
|
+
*
|
|
108
|
+
* const coarse = gibsGrid("250m").levels[0]!;
|
|
109
|
+
* console.log(gridPastExtent(coarse, 0, 0));
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare function gridPastExtent(level: MapGridLevel, col: number, row: number): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* The part of a tile that lies on the Earth, or `undefined` when the tile misses the Earth entirely.
|
|
115
|
+
*
|
|
116
|
+
* @param level - The level.
|
|
117
|
+
* @param col - The column.
|
|
118
|
+
* @param row - The row.
|
|
119
|
+
* @param extent - The Earth's extent in degrees.
|
|
120
|
+
* @returns The overlap, or `undefined`.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { gibsGrid, gridOnEarth } from "@johnmorrisdotca/chikyu-earth/door";
|
|
125
|
+
*
|
|
126
|
+
* console.log(gridOnEarth(gibsGrid("250m").levels[3]!, 0, 0, { west: -180, south: -90, east: 180, north: 90 }));
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export declare function gridOnEarth(level: MapGridLevel, col: number, row: number, extent: MapGrid["extent"]): MapPlace | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* The column and row of the tile that holds a point, or `undefined` when the point is off the Earth or the tile that would hold it misses the Earth.
|
|
132
|
+
* A longitude of 180 is the same meridian as -180, and both fall in column 0. A latitude of -90 falls in the last row that still meets the Earth.
|
|
133
|
+
*
|
|
134
|
+
* @param level - The level.
|
|
135
|
+
* @param lon - The longitude in degrees.
|
|
136
|
+
* @param lat - The latitude in degrees.
|
|
137
|
+
* @param extent - The Earth's extent.
|
|
138
|
+
* @returns The tile, or `undefined`.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* import { gibsGrid, gridTileAt } from "@johnmorrisdotca/chikyu-earth/door";
|
|
143
|
+
*
|
|
144
|
+
* const level = gibsGrid("250m").levels[3]!;
|
|
145
|
+
* console.log(gridTileAt(level, 180, 0, gibsGrid("250m").extent));
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export declare function gridTileAt(level: MapGridLevel, lon: number, lat: number, extent: MapGrid["extent"]): {
|
|
149
|
+
col: number;
|
|
150
|
+
row: number;
|
|
151
|
+
} | undefined;
|
|
152
|
+
/**
|
|
153
|
+
* The key of the tile that holds a point, `[<prefix>]level/column/row`, or `undefined` when no tile holds it.
|
|
154
|
+
*
|
|
155
|
+
* @param grid - The grid.
|
|
156
|
+
* @param level - The level number.
|
|
157
|
+
* @param lon - The longitude in degrees.
|
|
158
|
+
* @param lat - The latitude in degrees.
|
|
159
|
+
* @param prefix - Put before the key.
|
|
160
|
+
* @returns The key, or `undefined`.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* import { gibsGrid, gridKey } from "@johnmorrisdotca/chikyu-earth/door";
|
|
165
|
+
*
|
|
166
|
+
* console.log(gridKey(gibsGrid("250m"), 3, 0, 0));
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
export declare function gridKey(grid: MapGrid, level: number, lon: number, lat: number, prefix?: string): string | undefined;
|
|
170
|
+
/**
|
|
171
|
+
* The keys of the tiles that cover a place at a detail. The longitude may pass 180. A place with no area gives none.
|
|
172
|
+
*
|
|
173
|
+
* @param grid - The grid.
|
|
174
|
+
* @param place - The part of the map, in degrees.
|
|
175
|
+
* @param detail - The pixels across a full turn wanted.
|
|
176
|
+
* @param prefix - Put before each key.
|
|
177
|
+
* @returns The keys, row by row.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* import { gibsGrid, gridKeys } from "@johnmorrisdotca/chikyu-earth/door";
|
|
182
|
+
*
|
|
183
|
+
* console.log(gridKeys(gibsGrid("250m"), { lonMin: 170, lonMax: 190, latMin: -5, latMax: 5 }, 5120));
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export declare function gridKeys(grid: MapGrid, place: MapPlace, detail: number, prefix?: string): string[];
|
|
187
|
+
/**
|
|
188
|
+
* The Geographic (EPSG:4326) tile matrix NASA's imagery service publishes, read from its capabilities on 2026-11-13.
|
|
189
|
+
* A pixel is `0.5625 / 2^level` degrees. Tiles are 512 pixels, the corner of column 0 and row 0 is longitude -180 and latitude 90, and a level has as many tiles as it takes to cover the Earth, so levels 0, 1 and 2 run past it.
|
|
190
|
+
*
|
|
191
|
+
* @param matrix - `250m` (levels 0 to 8), `500m` (levels 0 to 7) or `31.25m` (levels 0 to 11, the Landsat and shaded-relief layers).
|
|
192
|
+
* @returns The grid.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* import { gibsGrid } from "@johnmorrisdotca/chikyu-earth/door";
|
|
197
|
+
*
|
|
198
|
+
* console.log(gibsGrid("250m").levels[3]);
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export declare function gibsGrid(matrix: "31.25m" | "250m" | "500m"): MapGrid;
|