@johnmorrisdotca/chikyu-earth 0.54.0 → 0.55.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 CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.55.0 - 2026-11-17
6
+
7
+ - **Real satellite pictures of the ground under a point: `@johnmorrisdotca/chikyu-earth/photos`** (MODULES-1368). Pure functions that make the address of a NASA GIBS (Worldview) true-colour snapshot of a box around a point, so a page can show the ground under a satellite or a ride. The reader's browser fetches the picture from NASA (no key, no account, no server cache); about 250 m a pixel, clouds left in.
8
+ - **Functions.** `photoUrl({ lat, lon, sizeKm, date, widthPx })` (a WMS GetMap request, EPSG:4326, JPEG; a box over the date line or a pole is moved inside the world), `photoPieces` (the exact box as one request, or two across the date line), `photoBox`, `photoLayer`, `photoFallbacks` (the days to try, newest first, when a day's picture is black) and `photoCredit(lang, full?)` (the short credit line, English and Japanese, or NASA's own acknowledgement sentence). The layer is NOAA-20 VIIRS from 2018-01-05, then Suomi NPP VIIRS (2015-11-24 to 2026-11-02, when it ends) and MODIS Terra (from 2000-02-24); before that there is no picture.
9
+ - **Demo.** The flat map page has "The ground under it": choose a satellite, pick 100, 300 or 1,000 km, press the button, and the picture of the place under it appears with the credit line. English and Japanese.
10
+ - **Docs and tests.** English and Japanese guides, a tested example `examples/node/photos.mjs`, and unit tests of every function (the address strings only: no test asks NASA), each shown to fail when its code is broken. `NOTICE.md` has the terms.
11
+
5
12
  ## 0.54.0 - 2026-11-17
6
13
 
7
14
  - **North up in the view above a satellite, a compass, and sunlit or in shadow on the ground track** (MODULES-1437). Riding above the ISS the way it is going was always at the top of the picture, so a reader could not tell its real direction (astroviewer shows it heading south-east).
package/NOTICE.md CHANGED
@@ -80,6 +80,10 @@ The default layer is `Landsat_WELD_CorrectedReflectance_TrueColor_Global_Annual`
80
80
 
81
81
  The 500 m maps could fill one more level (65536 across for the day map); the pyramid stops at level 5 (day) and level 4 (night) for now. Where the pyramid is hosted: GitHub Pages of this public repository, which costs nothing (docs/guides/earth-tiles.md). It adds 3,412 files and 35.4 MB to the site (852 files and 10.3 MB before the 500 m levels; the levels above day 4 and night 3 go out only when the repository variable EARTH_TILES_ALL is yes, see docs/guides/earth-tiles.md). The source files are downloaded by the build (688 MB, once, kept by CI's cache) and are not part of the site.
82
82
 
83
+ ## Pictures of the ground under a point
84
+
85
+ `@johnmorrisdotca/chikyu-earth/photos` makes the address of a snapshot (a WMS GetMap request, `https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi`) of NASA's Global Imagery Browse Services for a box on a day, and the demo's flat map page asks for it from the reader's browser when a button is pressed. Nothing is fetched by the package and nothing is stored or sent through this site. The terms are those of the section above (the same page, the same acknowledgement, which `photoCredit(lang, true)` returns) and of NASA's media guidelines (https://www.nasa.gov/nasa-brand-center/images-and-media/: NASA content "generally are not subject to copyright in the United States", "NASA should be acknowledged as the source of the material"); not endorsed by NASA. The layers are `VIIRS_NOAA20_CorrectedReflectance_TrueColor`, `VIIRS_SNPP_CorrectedReflectance_TrueColor` and `MODIS_Terra_CorrectedReflectance_TrueColor`, with the days NASA's WMS capabilities document (`https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0`) listed on 2026-11-17. On that day a GetMap for Suomi NPP after 2026-11-02 returned a black picture (the satellite's last day in the list), and one for NOAA-20 and MODIS Terra returned a picture; the answers carry `access-control-allow-origin: *`. NASA publishes no rate limit that this repository has read.
86
+
83
87
  ## The Moon's picture
84
88
 
85
89
  `moon-*.webp` is made by `scripts/make-textures.py` from NASA's Scientific Visualization Studio item 4720, "Moon: LROC colour mosaic" (https://svs.gsfc.nasa.gov/4720/,
package/README.md CHANGED
@@ -614,7 +614,7 @@ console.log(countryList().filter((country) => country.alpha2 === null).map((coun
614
614
 
615
615
  ## How to cite
616
616
 
617
- > Morris, J. (2026). *Chikyu Earth* (version 0.54.0) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
617
+ > Morris, J. (2026). *Chikyu Earth* (version 0.55.0) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
618
618
 
619
619
  GitHub's "Cite this repository" button reads [CITATION.cff](CITATION.cff) and gives BibTeX and APA. Cite the version you used (`npm ls @johnmorrisdotca/chikyu-earth`), and credit the sources behind the numbers and pictures: [NOTICE.md](NOTICE.md) says who made each one and under what terms.
620
620
 
@@ -0,0 +1,232 @@
1
+ /**
2
+ * The smallest side of a box, in kilometres: a `sizeKm` below it is held at it.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { PHOTO_MIN_KM } from "@johnmorrisdotca/chikyu-earth/photos";
7
+ *
8
+ * console.log(PHOTO_MIN_KM);
9
+ * ```
10
+ */
11
+ export declare const PHOTO_MIN_KM = 1;
12
+ /**
13
+ * The largest side of a box, in kilometres: a `sizeKm` above it is held at it.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { PHOTO_MAX_KM } from "@johnmorrisdotca/chikyu-earth/photos";
18
+ *
19
+ * console.log(PHOTO_MAX_KM);
20
+ * ```
21
+ */
22
+ export declare const PHOTO_MAX_KM = 4000;
23
+ /**
24
+ * The width in pixels a picture has when a page does not say.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { PHOTO_DEFAULT_PX } from "@johnmorrisdotca/chikyu-earth/photos";
29
+ *
30
+ * console.log(PHOTO_DEFAULT_PX);
31
+ * ```
32
+ */
33
+ export declare const PHOTO_DEFAULT_PX = 512;
34
+ /**
35
+ * The most pixels a picture may have across (and up): a larger `widthPx` is held at it.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { PHOTO_MAX_PX } from "@johnmorrisdotca/chikyu-earth/photos";
40
+ *
41
+ * console.log(PHOTO_MAX_PX);
42
+ * ```
43
+ */
44
+ export declare const PHOTO_MAX_PX = 2048;
45
+ /**
46
+ * How many days, counting the first, `photoFallbacks` goes back by default.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * import { PHOTO_FALLBACK_DAYS } from "@johnmorrisdotca/chikyu-earth/photos";
51
+ *
52
+ * console.log(PHOTO_FALLBACK_DAYS);
53
+ * ```
54
+ */
55
+ export declare const PHOTO_FALLBACK_DAYS = 3;
56
+ /**
57
+ * What to ask for: a point, the side of the box in kilometres, and the day.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * import type { PhotoOptions } from "@johnmorrisdotca/chikyu-earth/photos";
62
+ *
63
+ * const tokyo: PhotoOptions = { lat: 35.68, lon: 139.77, sizeKm: 200, date: "2026-11-10" };
64
+ * console.log(tokyo.sizeKm);
65
+ * ```
66
+ */
67
+ export interface PhotoOptions {
68
+ /** Degrees north (-90 to 90; a value outside is held at the pole). */
69
+ lat: number;
70
+ /** Degrees east (any number; it is wrapped into -180 to 180). */
71
+ lon: number;
72
+ /** The side of the box on the ground, in kilometres (held between `PHOTO_MIN_KM` and `PHOTO_MAX_KM`). */
73
+ sizeKm: number;
74
+ /** The UTC day as `yyyy-mm-dd`, or a Date or milliseconds since the epoch (its UTC day). */
75
+ date: string | number | Date;
76
+ /** The picture's width and height in pixels (a square). 512 when left out, and never more than `PHOTO_MAX_PX`. */
77
+ widthPx?: number;
78
+ }
79
+ /**
80
+ * A box in degrees. `west` may be below -180 or `east` above 180 when the box crosses the date line (`wraps`).
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * import type { PhotoBox } from "@johnmorrisdotca/chikyu-earth/photos";
85
+ *
86
+ * const box: PhotoBox = { south: 34, north: 36, west: 138, east: 141, wraps: false };
87
+ * console.log(box.east - box.west);
88
+ * ```
89
+ */
90
+ export interface PhotoBox {
91
+ south: number;
92
+ north: number;
93
+ west: number;
94
+ east: number;
95
+ wraps: boolean;
96
+ }
97
+ /**
98
+ * One request for a picture: its box (inside -180 to 180), its address, and where it goes in the whole picture. A box that crosses the date line is two of these, side by side.
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * import type { PhotoPiece } from "@johnmorrisdotca/chikyu-earth/photos";
103
+ *
104
+ * const place = (piece: PhotoPiece): string => `${piece.offsetPx},${piece.widthPx}`;
105
+ * console.log(typeof place);
106
+ * ```
107
+ */
108
+ export interface PhotoPiece {
109
+ url: string;
110
+ south: number;
111
+ north: number;
112
+ west: number;
113
+ east: number;
114
+ /** Pixels from the left of the whole picture. */
115
+ offsetPx: number;
116
+ widthPx: number;
117
+ heightPx: number;
118
+ }
119
+ /**
120
+ * One day to try: the day, the layer that has it, the one address of `photoUrl`, and the pieces of the exact box.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * import type { PhotoShot } from "@johnmorrisdotca/chikyu-earth/photos";
125
+ *
126
+ * const label = (shot: PhotoShot): string => `${shot.date} ${shot.layer}`;
127
+ * console.log(typeof label);
128
+ * ```
129
+ */
130
+ export interface PhotoShot {
131
+ date: string;
132
+ layer: string;
133
+ url: string;
134
+ pieces: PhotoPiece[];
135
+ }
136
+ /**
137
+ * The box a picture of `sizeKm` around a point covers, in degrees. The height is `sizeKm` of latitude; the width is as many degrees of longitude as make `sizeKm` at the box's middle latitude, so the picture is square on the ground.
138
+ * At a pole the box is moved down so it stays on the Earth (the point is then at its edge) and its width in longitude grows to about 115 degrees. Across the date line `west` is below -180 or `east` above 180 and `wraps` is true.
139
+ *
140
+ * @param options - The point and the size; the date and the width are not read.
141
+ * @returns The box.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * import { photoBox } from "@johnmorrisdotca/chikyu-earth/photos";
146
+ *
147
+ * const fiji = photoBox({ lat: -17.7, lon: 179.9, sizeKm: 200, date: "2026-11-10" });
148
+ * console.log(fiji.wraps, fiji.east > 180);
149
+ * ```
150
+ */
151
+ export declare function photoBox(options: PhotoOptions): PhotoBox;
152
+ /**
153
+ * The id of the layer `photoUrl` uses for a day: the first of NOAA-20 VIIRS, Suomi NPP VIIRS and MODIS Terra (all true colour) whose days include it, or null before 2000-02-24.
154
+ *
155
+ * @param day - The UTC day, `yyyy-mm-dd`.
156
+ * @returns The layer id, or null.
157
+ *
158
+ * @example
159
+ * ```ts
160
+ * import { photoLayer } from "@johnmorrisdotca/chikyu-earth/photos";
161
+ *
162
+ * console.log(photoLayer("2026-11-10"), photoLayer("2010-06-01"), photoLayer("1999-01-01"));
163
+ * ```
164
+ */
165
+ export declare function photoLayer(day: string): string | null;
166
+ /**
167
+ * The address of a NASA GIBS snapshot (a WMS GetMap request, EPSG:4326, JPEG) of the box around a point on a day, from the best true-colour layer that has the day (`photoLayer`: NOAA-20 VIIRS from 2018, then Suomi NPP VIIRS and MODIS Terra back to 2000-02-24). About 250 m a pixel, clouds left in.
168
+ * The picture is one request, so a box that would cross the date line or run off a pole is moved to stay inside the world: the point is then not in the middle. `photoPieces` gives the exact box in two requests.
169
+ *
170
+ * @param options - The point, the size of the box and the day.
171
+ * @returns The address, or null when no layer has a picture for the day (before 2000-02-24).
172
+ * @throws RangeError when a number is not finite or the date is not a day.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * import { photoUrl } from "@johnmorrisdotca/chikyu-earth/photos";
177
+ *
178
+ * const url = photoUrl({ lat: 35.68, lon: 139.77, sizeKm: 200, date: "2026-11-10", widthPx: 512 });
179
+ * console.log(url?.includes("TIME=2026-11-10"));
180
+ * ```
181
+ */
182
+ export declare function photoUrl(options: PhotoOptions): string | null;
183
+ /**
184
+ * The exact box of a point as one or two requests, left to right: two when it crosses the date line, so a page can lay them side by side and the point stays in the middle.
185
+ *
186
+ * @param options - The point, the size of the box and the day.
187
+ * @returns The pieces, or an empty list when no layer has a picture for the day.
188
+ * @throws RangeError when a number is not finite or the date is not a day.
189
+ *
190
+ * @example
191
+ * ```ts
192
+ * import { photoPieces } from "@johnmorrisdotca/chikyu-earth/photos";
193
+ *
194
+ * const pieces = photoPieces({ lat: -17.7, lon: 179.9, sizeKm: 200, date: "2026-11-10" });
195
+ * console.log(pieces.length, pieces.map((piece) => piece.widthPx));
196
+ * ```
197
+ */
198
+ export declare function photoPieces(options: PhotoOptions): PhotoPiece[];
199
+ /**
200
+ * The days to try, newest first: the day asked for, then each day before it, up to `days` of them, skipping a day no layer has. NASA has no picture for a day whose satellite passes it has not yet processed (and answers with a black one, not an error), so a page tries the next of these when one is blank.
201
+ *
202
+ * @param options - The point, the size of the box and the first day.
203
+ * @param days - How many days to count back, the first included (3 by default).
204
+ * @returns The shots, each with its day, layer, one address and pieces.
205
+ * @throws RangeError when a number is not finite or the date is not a day.
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * import { photoFallbacks } from "@johnmorrisdotca/chikyu-earth/photos";
210
+ *
211
+ * const shots = photoFallbacks({ lat: 35.68, lon: 139.77, sizeKm: 200, date: "2026-11-10" }, 3);
212
+ * console.log(shots.map((shot) => shot.date));
213
+ * ```
214
+ */
215
+ export declare function photoFallbacks(options: PhotoOptions, days?: number): PhotoShot[];
216
+ /**
217
+ * The credit line to show with a picture, in English or Japanese. `full` gives NASA's own acknowledgement sentence for GIBS (the Japanese is this package's translation: NASA publishes none) instead of the short line.
218
+ * The short line names NASA as the source, as NASA asks, and says it is not an endorsement.
219
+ *
220
+ * @param lang - `en` or `ja`.
221
+ * @param full - True for the whole acknowledgement sentence.
222
+ * @returns The line.
223
+ *
224
+ * @example
225
+ * ```ts
226
+ * import { photoCredit } from "@johnmorrisdotca/chikyu-earth/photos";
227
+ *
228
+ * console.log(photoCredit("en"));
229
+ * console.log(photoCredit("ja", true).length > photoCredit("ja").length);
230
+ * ```
231
+ */
232
+ export declare function photoCredit(lang: "en" | "ja", full?: boolean): string;
@@ -0,0 +1,284 @@
1
+ // Real satellite pictures of the ground under a point: the address of a NASA GIBS (Worldview) snapshot of a box around it, the days to try when a day is missing, and the credit NASA asks for.
2
+ // Pure: nothing here fetches. The reader's browser asks NASA for the picture (no key, and the service answers any page). A page that never imports this does not pay for it.
3
+ import { GIBS_CREDIT, previousUtcDay, utcDay } from "../door/gibs.js";
4
+ /** The WMS address of NASA's imagery, in the plain latitude and longitude (EPSG:4326) grid. */
5
+ const WMS = "https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi";
6
+ /** Kilometres in a degree of latitude (the mean sphere, 6,371 km). */
7
+ const KM_PER_DEGREE = 111.195;
8
+ /**
9
+ * The smallest side of a box, in kilometres: a `sizeKm` below it is held at it.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { PHOTO_MIN_KM } from "@johnmorrisdotca/chikyu-earth/photos";
14
+ *
15
+ * console.log(PHOTO_MIN_KM);
16
+ * ```
17
+ */
18
+ export const PHOTO_MIN_KM = 1;
19
+ /**
20
+ * The largest side of a box, in kilometres: a `sizeKm` above it is held at it.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { PHOTO_MAX_KM } from "@johnmorrisdotca/chikyu-earth/photos";
25
+ *
26
+ * console.log(PHOTO_MAX_KM);
27
+ * ```
28
+ */
29
+ export const PHOTO_MAX_KM = 4000;
30
+ /**
31
+ * The width in pixels a picture has when a page does not say.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { PHOTO_DEFAULT_PX } from "@johnmorrisdotca/chikyu-earth/photos";
36
+ *
37
+ * console.log(PHOTO_DEFAULT_PX);
38
+ * ```
39
+ */
40
+ export const PHOTO_DEFAULT_PX = 512;
41
+ /**
42
+ * The most pixels a picture may have across (and up): a larger `widthPx` is held at it.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { PHOTO_MAX_PX } from "@johnmorrisdotca/chikyu-earth/photos";
47
+ *
48
+ * console.log(PHOTO_MAX_PX);
49
+ * ```
50
+ */
51
+ export const PHOTO_MAX_PX = 2048;
52
+ /**
53
+ * How many days, counting the first, `photoFallbacks` goes back by default.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * import { PHOTO_FALLBACK_DAYS } from "@johnmorrisdotca/chikyu-earth/photos";
58
+ *
59
+ * console.log(PHOTO_FALLBACK_DAYS);
60
+ * ```
61
+ */
62
+ export const PHOTO_FALLBACK_DAYS = 3;
63
+ /**
64
+ * The daily true-colour layers, best first, about 250 m a pixel with the clouds left in: NOAA-20 VIIRS (from 2018-01-05), Suomi NPP VIIRS (2015-11-24 to 2026-11-02, then it ends) and MODIS Terra (from 2000-02-24).
65
+ * The first with a day is used, so a day one of them skipped is taken from the next. Read from NASA's capabilities document on 2026-11-17.
66
+ */
67
+ const LAYERS = [
68
+ {
69
+ id: "VIIRS_NOAA20_CorrectedReflectance_TrueColor",
70
+ running: true,
71
+ days: [["2018-01-05", "2018-02-21"], ["2018-02-24", "2018-03-12"], ["2018-03-14", "2019-12-31"], ["2020-01-02", "2022-01-12"], ["2022-01-14", "2026-11-16"]],
72
+ },
73
+ {
74
+ id: "VIIRS_SNPP_CorrectedReflectance_TrueColor",
75
+ running: false,
76
+ days: [["2015-11-24", "2022-07-27"], ["2022-08-10", "2024-05-31"], ["2024-06-04", "2024-07-11"], ["2024-07-16", "2024-07-24"], ["2024-07-29", "2024-11-03"], ["2024-11-06", "2026-04-28"], ["2026-04-30", "2026-07-10"], ["2026-07-16", "2026-11-02"]],
77
+ },
78
+ {
79
+ id: "MODIS_Terra_CorrectedReflectance_TrueColor",
80
+ running: true,
81
+ days: [["2000-02-24", "2000-04-25"], ["2000-04-28", "2000-08-06"], ["2000-08-18", "2001-06-15"], ["2001-07-03", "2002-03-19"], ["2002-03-28", "2003-12-16"], ["2003-12-24", "2008-12-20"], ["2008-12-23", "2016-02-18"], ["2016-02-27", "2022-10-10"], ["2022-10-23", "2026-11-17"]],
82
+ },
83
+ ];
84
+ /** Whether the layer lists the day, or is still running and the day is after its last. */
85
+ const hasDay = (layer, day) => {
86
+ const last = layer.days[layer.days.length - 1][1];
87
+ if (day > last)
88
+ return layer.running;
89
+ return layer.days.some(([start, end]) => day >= start && day <= end);
90
+ };
91
+ const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
92
+ const trim = (value) => String(Number(value.toFixed(5)));
93
+ /** The UTC day of a date option, or a RangeError when it is not one. */
94
+ function dayOf(date) {
95
+ if (typeof date === "string") {
96
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date);
97
+ const check = match ? new Date(Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3]))) : null;
98
+ if (!match || !check || utcDay(check.getTime()) !== date)
99
+ throw new RangeError(`date must be yyyy-mm-dd, a Date or milliseconds: ${date}`);
100
+ return date;
101
+ }
102
+ const ms = date instanceof Date ? date.getTime() : date;
103
+ if (!Number.isFinite(ms))
104
+ throw new RangeError("date must be yyyy-mm-dd, a Date or milliseconds");
105
+ return utcDay(ms);
106
+ }
107
+ function readOptions(options) {
108
+ const { lat, lon, sizeKm, widthPx } = options;
109
+ if (!Number.isFinite(lat) || !Number.isFinite(lon) || !Number.isFinite(sizeKm))
110
+ throw new RangeError("lat, lon and sizeKm must be finite numbers");
111
+ const width = widthPx === undefined ? PHOTO_DEFAULT_PX : widthPx;
112
+ if (!Number.isFinite(width))
113
+ throw new RangeError("widthPx must be a finite number");
114
+ return {
115
+ lat: clamp(lat, -90, 90),
116
+ lon: ((((lon + 180) % 360) + 360) % 360) - 180,
117
+ sizeKm: clamp(sizeKm, PHOTO_MIN_KM, PHOTO_MAX_KM),
118
+ day: dayOf(options.date),
119
+ widthPx: clamp(Math.round(width), 1, PHOTO_MAX_PX),
120
+ };
121
+ }
122
+ /**
123
+ * The box a picture of `sizeKm` around a point covers, in degrees. The height is `sizeKm` of latitude; the width is as many degrees of longitude as make `sizeKm` at the box's middle latitude, so the picture is square on the ground.
124
+ * At a pole the box is moved down so it stays on the Earth (the point is then at its edge) and its width in longitude grows to about 115 degrees. Across the date line `west` is below -180 or `east` above 180 and `wraps` is true.
125
+ *
126
+ * @param options - The point and the size; the date and the width are not read.
127
+ * @returns The box.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * import { photoBox } from "@johnmorrisdotca/chikyu-earth/photos";
132
+ *
133
+ * const fiji = photoBox({ lat: -17.7, lon: 179.9, sizeKm: 200, date: "2026-11-10" });
134
+ * console.log(fiji.wraps, fiji.east > 180);
135
+ * ```
136
+ */
137
+ export function photoBox(options) {
138
+ const { lat, lon, sizeKm } = readOptions(options);
139
+ const height = sizeKm / KM_PER_DEGREE;
140
+ const south = clamp(lat - height / 2, -90, 90 - height);
141
+ const north = south + height;
142
+ const cosine = Math.cos((((south + north) / 2) * Math.PI) / 180);
143
+ const span = Math.min(360, height / Math.max(cosine, 1e-9));
144
+ const west = lon - span / 2;
145
+ const east = lon + span / 2;
146
+ return { south, north, west, east, wraps: west < -180 || east > 180 };
147
+ }
148
+ /**
149
+ * The id of the layer `photoUrl` uses for a day: the first of NOAA-20 VIIRS, Suomi NPP VIIRS and MODIS Terra (all true colour) whose days include it, or null before 2000-02-24.
150
+ *
151
+ * @param day - The UTC day, `yyyy-mm-dd`.
152
+ * @returns The layer id, or null.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * import { photoLayer } from "@johnmorrisdotca/chikyu-earth/photos";
157
+ *
158
+ * console.log(photoLayer("2026-11-10"), photoLayer("2010-06-01"), photoLayer("1999-01-01"));
159
+ * ```
160
+ */
161
+ export function photoLayer(day) {
162
+ return LAYERS.find((layer) => hasDay(layer, day))?.id ?? null;
163
+ }
164
+ function requestFor(layer, west, south, east, north, widthPx, heightPx, day) {
165
+ return `${WMS}?SERVICE=WMS&REQUEST=GetMap&VERSION=1.1.1&LAYERS=${layer}&STYLES=&FORMAT=image%2Fjpeg&SRS=EPSG%3A4326&BBOX=${trim(west)},${trim(south)},${trim(east)},${trim(north)}&WIDTH=${widthPx}&HEIGHT=${heightPx}&TIME=${day}`;
166
+ }
167
+ function piecesFor(box, layer, day, widthPx) {
168
+ const piece = (west, east, offsetPx, width) => ({
169
+ url: requestFor(layer, west, box.south, east, box.north, width, widthPx, day),
170
+ south: box.south,
171
+ north: box.north,
172
+ west,
173
+ east,
174
+ offsetPx,
175
+ widthPx: width,
176
+ heightPx: widthPx,
177
+ });
178
+ if (!box.wraps)
179
+ return [piece(box.west, box.east, 0, widthPx)];
180
+ const span = box.east - box.west;
181
+ const crossesEast = box.east > 180;
182
+ const firstSpan = crossesEast ? 180 - box.west : -180 - box.west;
183
+ const left = clamp(Math.round((widthPx * firstSpan) / span), 1, widthPx - 1);
184
+ if (crossesEast)
185
+ return [piece(box.west, 180, 0, left), piece(-180, box.east - 360, left, widthPx - left)];
186
+ return [piece(box.west + 360, 180, 0, left), piece(-180, box.east, left, widthPx - left)];
187
+ }
188
+ /**
189
+ * The address of a NASA GIBS snapshot (a WMS GetMap request, EPSG:4326, JPEG) of the box around a point on a day, from the best true-colour layer that has the day (`photoLayer`: NOAA-20 VIIRS from 2018, then Suomi NPP VIIRS and MODIS Terra back to 2000-02-24). About 250 m a pixel, clouds left in.
190
+ * The picture is one request, so a box that would cross the date line or run off a pole is moved to stay inside the world: the point is then not in the middle. `photoPieces` gives the exact box in two requests.
191
+ *
192
+ * @param options - The point, the size of the box and the day.
193
+ * @returns The address, or null when no layer has a picture for the day (before 2000-02-24).
194
+ * @throws RangeError when a number is not finite or the date is not a day.
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * import { photoUrl } from "@johnmorrisdotca/chikyu-earth/photos";
199
+ *
200
+ * const url = photoUrl({ lat: 35.68, lon: 139.77, sizeKm: 200, date: "2026-11-10", widthPx: 512 });
201
+ * console.log(url?.includes("TIME=2026-11-10"));
202
+ * ```
203
+ */
204
+ export function photoUrl(options) {
205
+ const { widthPx, day } = readOptions(options);
206
+ const layer = photoLayer(day);
207
+ if (layer === null)
208
+ return null;
209
+ const box = photoBox(options);
210
+ const shift = box.west < -180 ? -180 - box.west : box.east > 180 ? 180 - box.east : 0;
211
+ return requestFor(layer, box.west + shift, box.south, box.east + shift, box.north, widthPx, widthPx, day);
212
+ }
213
+ /**
214
+ * The exact box of a point as one or two requests, left to right: two when it crosses the date line, so a page can lay them side by side and the point stays in the middle.
215
+ *
216
+ * @param options - The point, the size of the box and the day.
217
+ * @returns The pieces, or an empty list when no layer has a picture for the day.
218
+ * @throws RangeError when a number is not finite or the date is not a day.
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * import { photoPieces } from "@johnmorrisdotca/chikyu-earth/photos";
223
+ *
224
+ * const pieces = photoPieces({ lat: -17.7, lon: 179.9, sizeKm: 200, date: "2026-11-10" });
225
+ * console.log(pieces.length, pieces.map((piece) => piece.widthPx));
226
+ * ```
227
+ */
228
+ export function photoPieces(options) {
229
+ const { widthPx, day } = readOptions(options);
230
+ const layer = photoLayer(day);
231
+ return layer === null ? [] : piecesFor(photoBox(options), layer, day, widthPx);
232
+ }
233
+ /**
234
+ * The days to try, newest first: the day asked for, then each day before it, up to `days` of them, skipping a day no layer has. NASA has no picture for a day whose satellite passes it has not yet processed (and answers with a black one, not an error), so a page tries the next of these when one is blank.
235
+ *
236
+ * @param options - The point, the size of the box and the first day.
237
+ * @param days - How many days to count back, the first included (3 by default).
238
+ * @returns The shots, each with its day, layer, one address and pieces.
239
+ * @throws RangeError when a number is not finite or the date is not a day.
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * import { photoFallbacks } from "@johnmorrisdotca/chikyu-earth/photos";
244
+ *
245
+ * const shots = photoFallbacks({ lat: 35.68, lon: 139.77, sizeKm: 200, date: "2026-11-10" }, 3);
246
+ * console.log(shots.map((shot) => shot.date));
247
+ * ```
248
+ */
249
+ export function photoFallbacks(options, days = PHOTO_FALLBACK_DAYS) {
250
+ const { widthPx, day } = readOptions(options);
251
+ const shots = [];
252
+ let at = day;
253
+ const count = clamp(Math.floor(Number.isFinite(days) ? days : PHOTO_FALLBACK_DAYS), 1, 30);
254
+ for (let i = 0; i < count; i += 1) {
255
+ const layer = photoLayer(at);
256
+ if (layer !== null) {
257
+ const dated = { ...options, date: at, widthPx };
258
+ shots.push({ date: at, layer, url: photoUrl(dated), pieces: piecesFor(photoBox(dated), layer, at, widthPx) });
259
+ }
260
+ at = previousUtcDay(Date.parse(`${at}T12:00:00Z`));
261
+ }
262
+ return shots;
263
+ }
264
+ /**
265
+ * The credit line to show with a picture, in English or Japanese. `full` gives NASA's own acknowledgement sentence for GIBS (the Japanese is this package's translation: NASA publishes none) instead of the short line.
266
+ * The short line names NASA as the source, as NASA asks, and says it is not an endorsement.
267
+ *
268
+ * @param lang - `en` or `ja`.
269
+ * @param full - True for the whole acknowledgement sentence.
270
+ * @returns The line.
271
+ *
272
+ * @example
273
+ * ```ts
274
+ * import { photoCredit } from "@johnmorrisdotca/chikyu-earth/photos";
275
+ *
276
+ * console.log(photoCredit("en"));
277
+ * console.log(photoCredit("ja", true).length > photoCredit("ja").length);
278
+ * ```
279
+ */
280
+ export function photoCredit(lang, full = false) {
281
+ if (full)
282
+ return GIBS_CREDIT[lang];
283
+ return lang === "ja" ? "画像:NASA GIBS/Worldview(NASA)。NASAが承認したものではありません。" : "Imagery: NASA GIBS / Worldview (NASA). Not endorsed by NASA.";
284
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.54.0",
3
+ "version": "0.55.0",
4
4
  "description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on the place you give it, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
5
5
  "keywords": [
6
6
  "earth",
@@ -191,6 +191,11 @@
191
191
  "import": "./dist/place/index.js",
192
192
  "default": "./dist/place/index.js"
193
193
  },
194
+ "./photos": {
195
+ "types": "./dist/photos/index.d.ts",
196
+ "import": "./dist/photos/index.js",
197
+ "default": "./dist/photos/index.js"
198
+ },
194
199
  "./package.json": "./package.json"
195
200
  },
196
201
  "files": [