@spacexr/tiles 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +57 -2
- package/dist/addressing/index.d.ts +16 -0
- package/dist/addressing/index.js +4 -0
- package/dist/addressing/index.js.map +1 -0
- package/dist/bake/index.d.ts +91 -0
- package/dist/bake/index.js +3 -0
- package/dist/bake/index.js.map +1 -0
- package/dist/celestial-dataset-CUIsplKO.d.ts +99 -0
- package/dist/chunk-2HLQXRDD.js +246 -0
- package/dist/chunk-2HLQXRDD.js.map +1 -0
- package/dist/chunk-6YNCRDEE.js +170 -0
- package/dist/chunk-6YNCRDEE.js.map +1 -0
- package/dist/chunk-7AWV2IEK.js +79 -0
- package/dist/chunk-7AWV2IEK.js.map +1 -0
- package/dist/chunk-ESLGXWQR.js +219 -0
- package/dist/chunk-ESLGXWQR.js.map +1 -0
- package/dist/chunk-FB57CZDK.js +312 -0
- package/dist/chunk-FB57CZDK.js.map +1 -0
- package/dist/chunk-GRTG4WNA.js +44 -0
- package/dist/chunk-GRTG4WNA.js.map +1 -0
- package/dist/chunk-HVYN4AIT.js +45 -0
- package/dist/chunk-HVYN4AIT.js.map +1 -0
- package/dist/chunk-IGNRXAW3.js +77 -0
- package/dist/chunk-IGNRXAW3.js.map +1 -0
- package/dist/chunk-QGTREKHS.js +145 -0
- package/dist/chunk-QGTREKHS.js.map +1 -0
- package/dist/chunk-SPDRAY37.js +56 -0
- package/dist/chunk-SPDRAY37.js.map +1 -0
- package/dist/chunk-TA4BEVHL.js +81 -0
- package/dist/chunk-TA4BEVHL.js.map +1 -0
- package/dist/chunk-TGOB5HT5.js +149 -0
- package/dist/chunk-TGOB5HT5.js.map +1 -0
- package/dist/chunk-X6ZJ2PDY.js +104 -0
- package/dist/chunk-X6ZJ2PDY.js.map +1 -0
- package/dist/chunk-XTY4HCPY.js +22 -0
- package/dist/chunk-XTY4HCPY.js.map +1 -0
- package/dist/chunk-Y66PNDG2.js +107 -0
- package/dist/chunk-Y66PNDG2.js.map +1 -0
- package/dist/chunk-YEACOJOC.js +51 -0
- package/dist/chunk-YEACOJOC.js.map +1 -0
- package/dist/cube/index.d.ts +160 -0
- package/dist/cube/index.js +9 -0
- package/dist/cube/index.js.map +1 -0
- package/dist/datasets/index.d.ts +75 -0
- package/dist/datasets/index.js +11 -0
- package/dist/datasets/index.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/mercator/index.d.ts +24 -0
- package/dist/mercator/index.js +8 -0
- package/dist/mercator/index.js.map +1 -0
- package/dist/metrics-HGbzqj6L.d.ts +45 -0
- package/dist/pyramid/index.d.ts +156 -0
- package/dist/pyramid/index.js +4 -0
- package/dist/pyramid/index.js.map +1 -0
- package/dist/raster-registration-CgBxrhDB.d.ts +19 -0
- package/dist/reference-surface-CsTtrNsc.d.ts +49 -0
- package/dist/reprojection/index.d.ts +255 -0
- package/dist/reprojection/index.js +6 -0
- package/dist/reprojection/index.js.map +1 -0
- package/dist/sources/index.d.ts +55 -0
- package/dist/sources/index.js +7 -0
- package/dist/sources/index.js.map +1 -0
- package/dist/surface/index.d.ts +15 -0
- package/dist/surface/index.js +4 -0
- package/dist/surface/index.js.map +1 -0
- package/dist/tile-D-jjIRRA.d.ts +53 -0
- package/dist/tile-address-BkfSftp6.d.ts +66 -0
- package/dist/tile-http-client-BHZOtGX7.d.ts +73 -0
- package/dist/tile-neighbors-LfvV7oiX.d.ts +48 -0
- package/dist/tile-surface-s9Zi6X9_.d.ts +40 -0
- package/dist/tile-topology-B6xHYqPb.d.ts +51 -0
- package/dist/web-mercator-dataset-Dmou8byO.d.ts +125 -0
- package/dist/web-tile-source-DrZDBIm6.d.ts +92 -0
- package/package.json +122 -5
- package/src/addressing/index.ts +8 -0
- package/src/addressing/metrics.ts +48 -0
- package/src/addressing/raster-registration.ts +22 -0
- package/src/addressing/tile-address.ts +130 -0
- package/src/addressing/tile-edge-rotation.ts +13 -0
- package/src/addressing/tile-neighbors.ts +73 -0
- package/src/addressing/tile-surface.ts +44 -0
- package/src/addressing/tile-topology.ts +86 -0
- package/src/addressing/tile.ts +73 -0
- package/src/bake/bake-pyramid.ts +106 -0
- package/src/bake/index.ts +2 -0
- package/src/bake/pyramid-levels.ts +63 -0
- package/src/cube/celestial-dataset.ts +182 -0
- package/src/cube/cube-face.ts +51 -0
- package/src/cube/cube-geometric-error.ts +30 -0
- package/src/cube/cube-mapping.ts +101 -0
- package/src/cube/cube-neighbors.ts +198 -0
- package/src/cube/cube-tile-surface.ts +30 -0
- package/src/cube/index.ts +6 -0
- package/src/datasets/index.ts +1 -0
- package/src/datasets/tile-dataset.ts +121 -0
- package/src/index.ts +9 -0
- package/src/mercator/index.ts +3 -0
- package/src/mercator/web-mercator-dataset.ts +215 -0
- package/src/mercator/web-mercator-tile-metrics.ts +119 -0
- package/src/mercator/web-mercator-tile-surface.ts +52 -0
- package/src/pyramid/index.ts +2 -0
- package/src/pyramid/resident-tiles.ts +81 -0
- package/src/pyramid/tile-pyramid.ts +285 -0
- package/src/reprojection/cube-resampling.ts +159 -0
- package/src/reprojection/index.ts +4 -0
- package/src/reprojection/source-projection.ts +196 -0
- package/src/reprojection/source-tile-matrix.ts +100 -0
- package/src/reprojection/wmts-capabilities.ts +112 -0
- package/src/sources/index.ts +4 -0
- package/src/sources/tile-http-client.ts +154 -0
- package/src/sources/tile-layer-changes.ts +88 -0
- package/src/sources/url-template.ts +96 -0
- package/src/sources/web-tile-source.ts +73 -0
- package/src/surface/directions.ts +27 -0
- package/src/surface/index.ts +2 -0
- package/src/surface/reference-surface.ts +88 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { ancestorTileAddress, tileAddressKey, tileNeighborAddress } from './chunk-Y66PNDG2.js';
|
|
2
|
+
|
|
3
|
+
// src/pyramid/tile-pyramid.ts
|
|
4
|
+
function tileAreaInAncestor(address, levels) {
|
|
5
|
+
const ancestor = ancestorTileAddress(address, levels);
|
|
6
|
+
const scale = 2 ** (address.lod - ancestor.lod);
|
|
7
|
+
return {
|
|
8
|
+
ancestor,
|
|
9
|
+
west: (address.x - ancestor.x * scale) / scale,
|
|
10
|
+
north: (address.y - ancestor.y * scale) / scale,
|
|
11
|
+
size: 1 / scale
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
var TilePyramid = class {
|
|
15
|
+
maxLOD;
|
|
16
|
+
options;
|
|
17
|
+
cacheEntries;
|
|
18
|
+
entries = /* @__PURE__ */ new Map();
|
|
19
|
+
/** Retain counts of the pyramid tiles that must stay in the cache. */
|
|
20
|
+
retained = /* @__PURE__ */ new Map();
|
|
21
|
+
constructor(options) {
|
|
22
|
+
if (!Number.isInteger(options.maxLOD) || options.maxLOD < 0) throw new RangeError("maxLOD must be a non-negative integer.");
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.maxLOD = options.maxLOD;
|
|
25
|
+
this.cacheEntries = options.cacheEntries ?? 256;
|
|
26
|
+
}
|
|
27
|
+
/** Number of tiles in memory, loaded or loading. */
|
|
28
|
+
get size() {
|
|
29
|
+
return this.entries.size;
|
|
30
|
+
}
|
|
31
|
+
/** Level of the pyramid tile serving a tile level. */
|
|
32
|
+
sourceLevel(lod) {
|
|
33
|
+
return Math.min(lod, this.maxLOD);
|
|
34
|
+
}
|
|
35
|
+
/** The pyramid tile serving a tile and the part of it the tile covers. */
|
|
36
|
+
areaOf(address) {
|
|
37
|
+
const level = this.options.servingLevel ? Math.min(this.options.servingLevel(address), this.sourceLevel(address.lod)) : this.sourceLevel(address.lod);
|
|
38
|
+
return tileAreaInAncestor(address, address.lod - level);
|
|
39
|
+
}
|
|
40
|
+
/** Loaded content of a pyramid tile, without loading it. */
|
|
41
|
+
get(address) {
|
|
42
|
+
return this.entries.get(tileAddressKey(address))?.content;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Loads the pyramid tile serving a tile, with its prefetched neighbours.
|
|
46
|
+
*
|
|
47
|
+
* @param signal - Withdraws this request, which then rejects with its reason. A load every request has withdrawn from
|
|
48
|
+
* is aborted and leaves the cache; a load another request still waits for goes on.
|
|
49
|
+
* @returns The content and the part of it the tile covers, or `undefined` when the pyramid has no tile there.
|
|
50
|
+
*/
|
|
51
|
+
async load(address, signal) {
|
|
52
|
+
const area = this.areaOf(address);
|
|
53
|
+
const content = await this.loadTile(area.ancestor, signal);
|
|
54
|
+
return content === void 0 ? void 0 : { ...area, address: { ...address }, content };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Loads one tile of the pyramid itself, with its prefetched neighbours, whatever tile `servingLevel` would serve at
|
|
58
|
+
* its address: for contents made from other tiles of the pyramid, such as a tile generated from its parent.
|
|
59
|
+
*
|
|
60
|
+
* @param signal - Withdraws this request; see {@link load}.
|
|
61
|
+
* @returns Its content, or `undefined` when the pyramid has no tile there.
|
|
62
|
+
*/
|
|
63
|
+
async loadTile(address, signal) {
|
|
64
|
+
const { prefetch = [] } = this.options;
|
|
65
|
+
const neighbors = (typeof prefetch === "function" ? prefetch(address) : prefetch).map((position) => tileNeighborAddress(address, position, { wrapX: this.options.wrapX ?? false })).filter((neighbor) => neighbor !== void 0);
|
|
66
|
+
const [content] = await Promise.all([this.tile(address, signal), ...neighbors.map((neighbor) => this.tile(neighbor, signal).catch(() => void 0))]);
|
|
67
|
+
return content;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Keeps the pyramid tile serving a tile in the cache until it is released as many times, for example while a
|
|
71
|
+
* renderer displays data sampled from it. A retained tile still has to be loaded.
|
|
72
|
+
*/
|
|
73
|
+
retain(address) {
|
|
74
|
+
const tileKey = tileAddressKey(this.areaOf(address).ancestor);
|
|
75
|
+
this.retained.set(tileKey, (this.retained.get(tileKey) ?? 0) + 1);
|
|
76
|
+
}
|
|
77
|
+
/** Releases a tile retained with {@link retain}; the cache then evicts it like any other once it is full. */
|
|
78
|
+
release(address) {
|
|
79
|
+
const tileKey = tileAddressKey(this.areaOf(address).ancestor);
|
|
80
|
+
const count = (this.retained.get(tileKey) ?? 0) - 1;
|
|
81
|
+
if (count > 0) this.retained.set(tileKey, count);
|
|
82
|
+
else this.retained.delete(tileKey);
|
|
83
|
+
this.evictOverflow();
|
|
84
|
+
}
|
|
85
|
+
/** Removes a tile from the cache. */
|
|
86
|
+
delete(address) {
|
|
87
|
+
const deleted = this.entries.delete(tileAddressKey(address));
|
|
88
|
+
if (deleted) this.options.onEvict?.(address);
|
|
89
|
+
return deleted;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Forgets the tiles whose data changed, such as those meeting an edited area: the next request of each loads it
|
|
93
|
+
* again, the other tiles stay cached. A load under way for one of them still answers its requests, but its content
|
|
94
|
+
* is not kept: it may predate the change.
|
|
95
|
+
*
|
|
96
|
+
* @returns The tiles forgotten.
|
|
97
|
+
*/
|
|
98
|
+
invalidate(affects) {
|
|
99
|
+
const forgotten = [];
|
|
100
|
+
for (const entry of [...this.entries.values()]) {
|
|
101
|
+
if (!affects(entry.address)) continue;
|
|
102
|
+
this.delete(entry.address);
|
|
103
|
+
forgotten.push({ ...entry.address });
|
|
104
|
+
}
|
|
105
|
+
return forgotten;
|
|
106
|
+
}
|
|
107
|
+
tile(address, signal) {
|
|
108
|
+
const tileKey = tileAddressKey(address);
|
|
109
|
+
const cached = this.entries.get(tileKey);
|
|
110
|
+
if (cached) {
|
|
111
|
+
this.entries.delete(tileKey);
|
|
112
|
+
this.entries.set(tileKey, cached);
|
|
113
|
+
return this.wait(cached, signal);
|
|
114
|
+
}
|
|
115
|
+
const controller = new AbortController();
|
|
116
|
+
const loading = this.options.load(address, controller.signal).then((content) => {
|
|
117
|
+
entry.settled = true;
|
|
118
|
+
if (this.entries.get(tileKey) === entry) {
|
|
119
|
+
entry.content = content;
|
|
120
|
+
if (content !== void 0) this.options.onLoad?.(entry.address, content);
|
|
121
|
+
}
|
|
122
|
+
return content;
|
|
123
|
+
});
|
|
124
|
+
const entry = { address: { ...address }, loading, content: void 0, controller, waiting: 0, pinned: false, settled: false, abortTimer: void 0 };
|
|
125
|
+
loading.catch(() => {
|
|
126
|
+
entry.settled = true;
|
|
127
|
+
if (this.entries.get(tileKey) === entry) this.entries.delete(tileKey);
|
|
128
|
+
});
|
|
129
|
+
this.entries.set(tileKey, entry);
|
|
130
|
+
this.evictOverflow();
|
|
131
|
+
return this.wait(entry, signal);
|
|
132
|
+
}
|
|
133
|
+
/** Aborts a load and forgets it, unless a request came back or it ended in the meantime. */
|
|
134
|
+
abortUnwanted(entry, reason) {
|
|
135
|
+
entry.abortTimer = void 0;
|
|
136
|
+
if (entry.waiting > 0 || entry.pinned || entry.settled) return;
|
|
137
|
+
entry.controller.abort(reason);
|
|
138
|
+
if (this.entries.get(tileAddressKey(entry.address)) === entry) this.delete(entry.address);
|
|
139
|
+
}
|
|
140
|
+
/** Waits for a load until the signal withdraws this request: the last one to leave aborts the load. */
|
|
141
|
+
wait(entry, signal) {
|
|
142
|
+
if (entry.settled) return entry.loading;
|
|
143
|
+
clearTimeout(entry.abortTimer);
|
|
144
|
+
entry.abortTimer = void 0;
|
|
145
|
+
if (!signal) {
|
|
146
|
+
entry.pinned = true;
|
|
147
|
+
return entry.loading;
|
|
148
|
+
}
|
|
149
|
+
entry.waiting++;
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const withdraw = () => {
|
|
152
|
+
entry.waiting--;
|
|
153
|
+
reject(signal.reason ?? new DOMException("The operation was aborted.", "AbortError"));
|
|
154
|
+
if (entry.waiting > 0 || entry.pinned || entry.settled) return;
|
|
155
|
+
const delay = this.options.abortDelayMilliseconds ?? 0;
|
|
156
|
+
if (delay > 0) entry.abortTimer = setTimeout(() => this.abortUnwanted(entry, signal.reason), delay);
|
|
157
|
+
else this.abortUnwanted(entry, signal.reason);
|
|
158
|
+
};
|
|
159
|
+
if (signal.aborted) {
|
|
160
|
+
withdraw();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
signal.addEventListener("abort", withdraw, { once: true });
|
|
164
|
+
const leave = () => {
|
|
165
|
+
signal.removeEventListener("abort", withdraw);
|
|
166
|
+
entry.waiting--;
|
|
167
|
+
};
|
|
168
|
+
entry.loading.then(
|
|
169
|
+
(content) => {
|
|
170
|
+
leave();
|
|
171
|
+
resolve(content);
|
|
172
|
+
},
|
|
173
|
+
(error) => {
|
|
174
|
+
leave();
|
|
175
|
+
reject(error);
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** Evicts the least recently used tiles that are not retained while the cache exceeds its capacity. */
|
|
181
|
+
evictOverflow() {
|
|
182
|
+
if (this.entries.size <= this.cacheEntries) return;
|
|
183
|
+
for (const [tileKey, entry] of this.entries) {
|
|
184
|
+
if (this.entries.size <= this.cacheEntries) return;
|
|
185
|
+
if (!this.retained.has(tileKey)) this.delete(entry.address);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/pyramid/resident-tiles.ts
|
|
191
|
+
var ResidentTiles = class {
|
|
192
|
+
entries = /* @__PURE__ */ new Map();
|
|
193
|
+
get size() {
|
|
194
|
+
return this.entries.size;
|
|
195
|
+
}
|
|
196
|
+
get(address) {
|
|
197
|
+
return this.entries.get(tileAddressKey(address))?.value;
|
|
198
|
+
}
|
|
199
|
+
has(address) {
|
|
200
|
+
return this.entries.has(tileAddressKey(address));
|
|
201
|
+
}
|
|
202
|
+
set(address, value) {
|
|
203
|
+
const entry = this.entries.get(tileAddressKey(address));
|
|
204
|
+
if (entry) entry.value = value;
|
|
205
|
+
else this.entries.set(tileAddressKey(address), { address: { ...address }, value });
|
|
206
|
+
}
|
|
207
|
+
delete(address) {
|
|
208
|
+
return this.entries.delete(tileAddressKey(address));
|
|
209
|
+
}
|
|
210
|
+
/** Addresses of the resident tiles. */
|
|
211
|
+
*addresses() {
|
|
212
|
+
for (const entry of this.entries.values()) yield entry.address;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Deepest resident tile containing a tile, the tile itself included, and the part of it the tile covers.
|
|
216
|
+
*
|
|
217
|
+
* @param minLOD - Coarsest level searched. Defaults to 0.
|
|
218
|
+
* @returns `undefined` when no resident tile contains the tile.
|
|
219
|
+
*/
|
|
220
|
+
cover(address, minLOD = 0) {
|
|
221
|
+
for (let levels = 0; address.lod - levels >= minLOD; levels++) {
|
|
222
|
+
const area = tileAreaInAncestor(address, levels);
|
|
223
|
+
const entry = this.entries.get(tileAddressKey(area.ancestor));
|
|
224
|
+
if (entry !== void 0) return { ...area, value: entry.value };
|
|
225
|
+
}
|
|
226
|
+
return void 0;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Levels between a tile and the deepest resident tile inside it, at most `maxLevels` below: `0` when only the
|
|
230
|
+
* tile itself is resident, `undefined` when no resident tile lies inside it.
|
|
231
|
+
*/
|
|
232
|
+
depthWithin(address, maxLevels = Number.POSITIVE_INFINITY) {
|
|
233
|
+
let depth;
|
|
234
|
+
for (const { address: resident } of this.entries.values()) {
|
|
235
|
+
const levels = resident.lod - address.lod;
|
|
236
|
+
if ((resident.face ?? 0) !== (address.face ?? 0) || levels < 0 || levels > maxLevels || depth !== void 0 && levels <= depth) continue;
|
|
237
|
+
const scale = 2 ** levels;
|
|
238
|
+
if (Math.floor(resident.x / scale) === address.x && Math.floor(resident.y / scale) === address.y) depth = levels;
|
|
239
|
+
}
|
|
240
|
+
return depth;
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
export { ResidentTiles, TilePyramid, tileAreaInAncestor };
|
|
245
|
+
//# sourceMappingURL=chunk-2HLQXRDD.js.map
|
|
246
|
+
//# sourceMappingURL=chunk-2HLQXRDD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/pyramid/tile-pyramid.ts","../src/pyramid/resident-tiles.ts"],"names":[],"mappings":";;;AAoBO,SAAS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AAA,EAAuB,SAAA,GAAY,MAAA,CAAO,iBAAA,EAAuC;AAChG,IAAA,IAAI,KAAA;AACJ,IAAA,KAAA,MAAW,EAAE,OAAA,EAAS,QAAA,MAAc,IAAA,CAAK,OAAA,CAAQ,QAAO,EAAG;AACvD,MAAA,MAAM,MAAA,GAAS,QAAA,CAAS,GAAA,GAAM,OAAA,CAAQ,GAAA;AACtC,MAAA,IAAA,CAAK,QAAA,CAAS,IAAA,IAAQ,CAAA,OAAQ,OAAA,CAAQ,IAAA,IAAQ,CAAA,CAAA,IAAM,MAAA,GAAS,CAAA,IAAK,MAAA,GAAS,SAAA,IAAc,KAAA,KAAU,MAAA,IAAa,UAAU,KAAA,EAAQ;AAClI,MAAA,MAAM,QAAQ,CAAA,IAAK,MAAA;AACnB,MAAA,IAAI,KAAK,KAAA,CAAM,QAAA,CAAS,CAAA,GAAI,KAAK,MAAM,OAAA,CAAQ,CAAA,IAAK,IAAA,CAAK,KAAA,CAAM,SAAS,CAAA,GAAI,KAAK,CAAA,KAAM,OAAA,CAAQ,GAAG,KAAA,GAAQ,MAAA;AAAA,IAC9G;AACA,IAAA,OAAO,KAAA;AAAA,EACX;AACJ","file":"chunk-2HLQXRDD.js","sourcesContent":["import { ancestorTileAddress, tileAddressKey as key, type ITileAddress } from \"../addressing/tile-address\";\nimport { tileNeighborAddress, type TileNeighbor } from \"../addressing/tile-neighbors\";\n\n/**\n * Loads the content of one tile, or `undefined` when the pyramid has no such tile.\n *\n * @param signal - Aborts when nobody waits for the tile any more. A loader may ignore it.\n */\nexport type TileContentLoader<T> = (address: ITileAddress, signal?: AbortSignal) => Promise<T | undefined>;\n\n/** Part of an ancestor tile covered by a tile, in normalized ancestor coordinates, `west` and `north` from 0. */\nexport interface ITileAreaInAncestor {\n ancestor: ITileAddress;\n west: number;\n north: number;\n /** Width and height of the area, `1 / 2^levels`. */\n size: number;\n}\n\n/** The ancestor `levels` levels above a tile and the part of it the tile covers. */\nexport function tileAreaInAncestor(address: ITileAddress, levels: number): ITileAreaInAncestor {\n const ancestor = ancestorTileAddress(address, levels);\n const scale = 2 ** (address.lod - ancestor.lod);\n return {\n ancestor,\n west: (address.x - ancestor.x * scale) / scale,\n north: (address.y - ancestor.y * scale) / scale,\n size: 1 / scale,\n };\n}\n\nexport interface ITilePyramidOptions<T> {\n load: TileContentLoader<T>;\n /** Deepest level of the pyramid; deeper tiles use the sub-area of their ancestor at this level (overzoom). */\n maxLOD: number;\n /**\n * Level of the pyramid tile serving a tile, for a sparse pyramid whose depth varies from place to place. A tile deeper\n * than the data at its place then uses the sub-area of its ancestor there. Defaults to the same level everywhere.\n */\n servingLevel?: (address: ITileAddress) => number;\n /** Tiles kept in memory. Defaults to 256. */\n cacheEntries?: number;\n /**\n * Time a load nobody waits for any more goes on before it is aborted, so that a tile coming back right away, as when\n * a zoom turns around, does not load twice. Defaults to 0: at once.\n */\n abortDelayMilliseconds?: number;\n /** Wraps columns around the antimeridian, for pyramids covering every longitude. Defaults to false. */\n wrapX?: boolean;\n /**\n * Neighbours loaded with every tile, or with a given tile, for contents that need them: `[E, S, SE]` to stitch the\n * extra column and row of a raster, or every position to prepare a move. A missing or failing neighbour is ignored.\n * Defaults to none.\n */\n prefetch?: readonly TileNeighbor[] | ((address: ITileAddress) => readonly TileNeighbor[]);\n /** Called when the content of a tile is loaded and still cached. */\n onLoad?: (address: ITileAddress, content: T) => void;\n /** Called when a tile leaves the cache. */\n onEvict?: (address: ITileAddress) => void;\n}\n\n/** Content of the pyramid tile serving a tile, with the part of it the tile covers. */\nexport interface ITilePyramidRegion<T> extends ITileAreaInAncestor {\n /** Tile requested. */\n address: ITileAddress;\n /** Content of `ancestor`, the tile itself up to `maxLOD`. */\n content: T;\n}\n\ninterface ITilePyramidEntry<T> {\n readonly address: ITileAddress;\n readonly loading: Promise<T | undefined>;\n content: T | undefined;\n /** Aborts the load once nobody waits for it. */\n readonly controller: AbortController;\n /** Requests still waiting with a signal of their own. */\n waiting: number;\n /** A request without a signal, which cannot leave: the load always ends. */\n pinned: boolean;\n settled: boolean;\n /** Pending abort of a load nobody waits for, which a new request cancels. */\n abortTimer: ReturnType<typeof setTimeout> | undefined;\n}\n\n/**\n * Tiles of a pyramid loaded on demand, whatever their content: rasters, meshes, vector data.\n *\n * Each tile loads once and is shared by every request, including the requests of deeper tiles beyond `maxLOD`, which\n * use a sub-area of their ancestor. The least recently used tiles leave the cache, except the tiles retained by their\n * users, which may exceed its capacity. Neighbours listed in `prefetch` load with every tile.\n */\nexport class TilePyramid<T> {\n public readonly maxLOD: number;\n private readonly options: ITilePyramidOptions<T>;\n private readonly cacheEntries: number;\n private readonly entries = new Map<string, ITilePyramidEntry<T>>();\n /** Retain counts of the pyramid tiles that must stay in the cache. */\n private readonly retained = new Map<string, number>();\n\n public constructor(options: ITilePyramidOptions<T>) {\n if (!Number.isInteger(options.maxLOD) || options.maxLOD < 0) throw new RangeError(\"maxLOD must be a non-negative integer.\");\n this.options = options;\n this.maxLOD = options.maxLOD;\n this.cacheEntries = options.cacheEntries ?? 256;\n }\n\n /** Number of tiles in memory, loaded or loading. */\n public get size(): number {\n return this.entries.size;\n }\n\n /** Level of the pyramid tile serving a tile level. */\n public sourceLevel(lod: number): number {\n return Math.min(lod, this.maxLOD);\n }\n\n /** The pyramid tile serving a tile and the part of it the tile covers. */\n public areaOf(address: ITileAddress): ITileAreaInAncestor {\n const level = this.options.servingLevel ? Math.min(this.options.servingLevel(address), this.sourceLevel(address.lod)) : this.sourceLevel(address.lod);\n return tileAreaInAncestor(address, address.lod - level);\n }\n\n /** Loaded content of a pyramid tile, without loading it. */\n public get(address: ITileAddress): T | undefined {\n return this.entries.get(key(address))?.content;\n }\n\n /**\n * Loads the pyramid tile serving a tile, with its prefetched neighbours.\n *\n * @param signal - Withdraws this request, which then rejects with its reason. A load every request has withdrawn from\n * is aborted and leaves the cache; a load another request still waits for goes on.\n * @returns The content and the part of it the tile covers, or `undefined` when the pyramid has no tile there.\n */\n public async load(address: ITileAddress, signal?: AbortSignal): Promise<ITilePyramidRegion<T> | undefined> {\n const area = this.areaOf(address);\n const content = await this.loadTile(area.ancestor, signal);\n return content === undefined ? undefined : { ...area, address: { ...address }, content };\n }\n\n /**\n * Loads one tile of the pyramid itself, with its prefetched neighbours, whatever tile `servingLevel` would serve at\n * its address: for contents made from other tiles of the pyramid, such as a tile generated from its parent.\n *\n * @param signal - Withdraws this request; see {@link load}.\n * @returns Its content, or `undefined` when the pyramid has no tile there.\n */\n public async loadTile(address: ITileAddress, signal?: AbortSignal): Promise<T | undefined> {\n const { prefetch = [] } = this.options;\n const neighbors = (typeof prefetch === \"function\" ? prefetch(address) : prefetch)\n .map((position) => tileNeighborAddress(address, position, { wrapX: this.options.wrapX ?? false }))\n .filter((neighbor) => neighbor !== undefined);\n const [content] = await Promise.all([this.tile(address, signal), ...neighbors.map((neighbor) => this.tile(neighbor, signal).catch(() => undefined))]);\n return content;\n }\n\n /**\n * Keeps the pyramid tile serving a tile in the cache until it is released as many times, for example while a\n * renderer displays data sampled from it. A retained tile still has to be loaded.\n */\n public retain(address: ITileAddress): void {\n const tileKey = key(this.areaOf(address).ancestor);\n this.retained.set(tileKey, (this.retained.get(tileKey) ?? 0) + 1);\n }\n\n /** Releases a tile retained with {@link retain}; the cache then evicts it like any other once it is full. */\n public release(address: ITileAddress): void {\n const tileKey = key(this.areaOf(address).ancestor);\n const count = (this.retained.get(tileKey) ?? 0) - 1;\n if (count > 0) this.retained.set(tileKey, count);\n else this.retained.delete(tileKey);\n this.evictOverflow();\n }\n\n /** Removes a tile from the cache. */\n public delete(address: ITileAddress): boolean {\n const deleted = this.entries.delete(key(address));\n if (deleted) this.options.onEvict?.(address);\n return deleted;\n }\n\n /**\n * Forgets the tiles whose data changed, such as those meeting an edited area: the next request of each loads it\n * again, the other tiles stay cached. A load under way for one of them still answers its requests, but its content\n * is not kept: it may predate the change.\n *\n * @returns The tiles forgotten.\n */\n public invalidate(affects: (address: ITileAddress) => boolean): ITileAddress[] {\n const forgotten: ITileAddress[] = [];\n for (const entry of [...this.entries.values()]) {\n if (!affects(entry.address)) continue;\n this.delete(entry.address);\n forgotten.push({ ...entry.address });\n }\n return forgotten;\n }\n\n private tile(address: ITileAddress, signal?: AbortSignal): Promise<T | undefined> {\n const tileKey = key(address);\n const cached = this.entries.get(tileKey);\n if (cached) {\n // Most recently used last.\n this.entries.delete(tileKey);\n this.entries.set(tileKey, cached);\n return this.wait(cached, signal);\n }\n const controller = new AbortController();\n const loading = this.options.load(address, controller.signal).then((content) => {\n entry.settled = true;\n // A tile evicted while loading, or replaced since, is dropped.\n if (this.entries.get(tileKey) === entry) {\n entry.content = content;\n if (content !== undefined) this.options.onLoad?.(entry.address, content);\n }\n return content;\n });\n const entry: ITilePyramidEntry<T> = { address: { ...address }, loading, content: undefined, controller, waiting: 0, pinned: false, settled: false, abortTimer: undefined };\n loading.catch(() => {\n entry.settled = true;\n if (this.entries.get(tileKey) === entry) this.entries.delete(tileKey);\n });\n this.entries.set(tileKey, entry);\n this.evictOverflow();\n return this.wait(entry, signal);\n }\n\n /** Aborts a load and forgets it, unless a request came back or it ended in the meantime. */\n private abortUnwanted(entry: ITilePyramidEntry<T>, reason: unknown): void {\n entry.abortTimer = undefined;\n if (entry.waiting > 0 || entry.pinned || entry.settled) return;\n entry.controller.abort(reason);\n if (this.entries.get(key(entry.address)) === entry) this.delete(entry.address);\n }\n\n /** Waits for a load until the signal withdraws this request: the last one to leave aborts the load. */\n private wait(entry: ITilePyramidEntry<T>, signal?: AbortSignal): Promise<T | undefined> {\n if (entry.settled) return entry.loading;\n clearTimeout(entry.abortTimer);\n entry.abortTimer = undefined;\n if (!signal) {\n entry.pinned = true;\n return entry.loading;\n }\n entry.waiting++;\n return new Promise((resolve, reject) => {\n const withdraw = (): void => {\n entry.waiting--;\n reject(signal.reason ?? new DOMException(\"The operation was aborted.\", \"AbortError\"));\n if (entry.waiting > 0 || entry.pinned || entry.settled) return;\n const delay = this.options.abortDelayMilliseconds ?? 0;\n if (delay > 0) entry.abortTimer = setTimeout(() => this.abortUnwanted(entry, signal.reason), delay);\n else this.abortUnwanted(entry, signal.reason);\n };\n if (signal.aborted) {\n withdraw();\n return;\n }\n signal.addEventListener(\"abort\", withdraw, { once: true });\n const leave = (): void => {\n signal.removeEventListener(\"abort\", withdraw);\n entry.waiting--;\n };\n entry.loading.then(\n (content) => {\n leave();\n resolve(content);\n },\n (error: unknown) => {\n leave();\n reject(error);\n }\n );\n });\n }\n\n /** Evicts the least recently used tiles that are not retained while the cache exceeds its capacity. */\n private evictOverflow(): void {\n if (this.entries.size <= this.cacheEntries) return;\n for (const [tileKey, entry] of this.entries) {\n if (this.entries.size <= this.cacheEntries) return;\n if (!this.retained.has(tileKey)) this.delete(entry.address);\n }\n }\n}\n","import { tileAddressKey as key, type ITileAddress } from \"../addressing/tile-address\";\nimport { tileAreaInAncestor, type ITileAreaInAncestor } from \"./tile-pyramid\";\n\n/** Resident tile covering a requested tile, with the part of it the requested tile covers. */\nexport interface IResidentTileCover<T> extends ITileAreaInAncestor {\n /** Value stored for `ancestor`, the resident tile. */\n value: T;\n}\n\ninterface IResidentEntry<T> {\n readonly address: ITileAddress;\n value: T;\n}\n\n/**\n * Tiles currently available to a renderer, such as texture pages or elevation pages, indexed for coverage lookups.\n *\n * A renderer binds the data of one pyramid to the tiles of another: imagery pages to terrain tiles, or elevation\n * pages to imagery tiles. `cover` returns the deepest resident tile containing a requested tile, so that a binding\n * always falls back to coarser data while finer data loads.\n */\nexport class ResidentTiles<T> {\n private readonly entries = new Map<string, IResidentEntry<T>>();\n\n public get size(): number {\n return this.entries.size;\n }\n\n public get(address: ITileAddress): T | undefined {\n return this.entries.get(key(address))?.value;\n }\n\n public has(address: ITileAddress): boolean {\n return this.entries.has(key(address));\n }\n\n public set(address: ITileAddress, value: T): void {\n const entry = this.entries.get(key(address));\n if (entry) entry.value = value;\n else this.entries.set(key(address), { address: { ...address }, value });\n }\n\n public delete(address: ITileAddress): boolean {\n return this.entries.delete(key(address));\n }\n\n /** Addresses of the resident tiles. */\n public *addresses(): IterableIterator<ITileAddress> {\n for (const entry of this.entries.values()) yield entry.address;\n }\n\n /**\n * Deepest resident tile containing a tile, the tile itself included, and the part of it the tile covers.\n *\n * @param minLOD - Coarsest level searched. Defaults to 0.\n * @returns `undefined` when no resident tile contains the tile.\n */\n public cover(address: ITileAddress, minLOD = 0): IResidentTileCover<T> | undefined {\n for (let levels = 0; address.lod - levels >= minLOD; levels++) {\n const area = tileAreaInAncestor(address, levels);\n const entry = this.entries.get(key(area.ancestor));\n if (entry !== undefined) return { ...area, value: entry.value };\n }\n return undefined;\n }\n\n /**\n * Levels between a tile and the deepest resident tile inside it, at most `maxLevels` below: `0` when only the\n * tile itself is resident, `undefined` when no resident tile lies inside it.\n */\n public depthWithin(address: ITileAddress, maxLevels = Number.POSITIVE_INFINITY): number | undefined {\n let depth: number | undefined;\n for (const { address: resident } of this.entries.values()) {\n const levels = resident.lod - address.lod;\n if ((resident.face ?? 0) !== (address.face ?? 0) || levels < 0 || levels > maxLevels || (depth !== undefined && levels <= depth)) continue;\n const scale = 2 ** levels;\n if (Math.floor(resident.x / scale) === address.x && Math.floor(resident.y / scale) === address.y) depth = levels;\n }\n return depth;\n }\n}\n"]}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { assertValidTileAddress, tileAddressToQuadkey, toTmsY } from './chunk-Y66PNDG2.js';
|
|
2
|
+
|
|
3
|
+
// src/sources/tile-http-client.ts
|
|
4
|
+
var TileHttpError = class extends Error {
|
|
5
|
+
constructor(message, url, attempts, httpStatus, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.url = url;
|
|
8
|
+
this.attempts = attempts;
|
|
9
|
+
this.httpStatus = httpStatus;
|
|
10
|
+
this.name = "TileHttpError";
|
|
11
|
+
}
|
|
12
|
+
url;
|
|
13
|
+
attempts;
|
|
14
|
+
httpStatus;
|
|
15
|
+
};
|
|
16
|
+
function abortReason(signal) {
|
|
17
|
+
return signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
18
|
+
}
|
|
19
|
+
function defaultSleep(milliseconds, signal) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
if (signal?.aborted) {
|
|
22
|
+
reject(abortReason(signal));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const timer = setTimeout(() => {
|
|
26
|
+
signal?.removeEventListener("abort", onAbort);
|
|
27
|
+
resolve();
|
|
28
|
+
}, milliseconds);
|
|
29
|
+
const onAbort = () => {
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
reject(abortReason(signal));
|
|
32
|
+
};
|
|
33
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function retryAfterMs(response) {
|
|
37
|
+
const value = response.headers.get("retry-after");
|
|
38
|
+
if (value === null) return void 0;
|
|
39
|
+
const seconds = Number(value);
|
|
40
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
|
|
41
|
+
const date = Date.parse(value);
|
|
42
|
+
return Number.isNaN(date) ? void 0 : Math.max(0, date - Date.now());
|
|
43
|
+
}
|
|
44
|
+
var TileHttpClient = class {
|
|
45
|
+
fetchFunction;
|
|
46
|
+
headers;
|
|
47
|
+
maxAttempts;
|
|
48
|
+
initialDelayMs;
|
|
49
|
+
maxDelayMs;
|
|
50
|
+
retryStatuses;
|
|
51
|
+
absentStatuses;
|
|
52
|
+
random;
|
|
53
|
+
sleep;
|
|
54
|
+
constructor(options = {}) {
|
|
55
|
+
if (!options.fetch && typeof globalThis.fetch !== "function") throw new TypeError("No fetch implementation is available; supply options.fetch.");
|
|
56
|
+
this.fetchFunction = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
57
|
+
this.headers = { ...options.headers ?? {} };
|
|
58
|
+
this.maxAttempts = options.maxAttempts ?? 4;
|
|
59
|
+
this.initialDelayMs = options.initialDelayMs ?? 500;
|
|
60
|
+
this.maxDelayMs = options.maxDelayMs ?? 3e4;
|
|
61
|
+
this.retryStatuses = new Set(options.retryStatuses ?? [408, 425, 429, 500, 502, 503, 504]);
|
|
62
|
+
this.absentStatuses = new Set(options.absentStatuses ?? [204, 404]);
|
|
63
|
+
this.random = options.random ?? Math.random;
|
|
64
|
+
this.sleep = options.sleep ?? defaultSleep;
|
|
65
|
+
if (!Number.isInteger(this.maxAttempts) || this.maxAttempts < 1) throw new RangeError("maxAttempts must be a positive integer.");
|
|
66
|
+
if (!(this.initialDelayMs >= 0) || !(this.maxDelayMs >= 0)) throw new RangeError("Backoff delays must be non-negative.");
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Fetches one tile.
|
|
70
|
+
*
|
|
71
|
+
* @param url - Expanded tile URL.
|
|
72
|
+
* @param signal - Optional abort signal.
|
|
73
|
+
* @returns The tile bytes, or an `absent` result.
|
|
74
|
+
* @throws TileHttpError When the request fails permanently or exhausts its attempts.
|
|
75
|
+
*/
|
|
76
|
+
async fetchTile(url, signal) {
|
|
77
|
+
let lastError;
|
|
78
|
+
let lastStatus;
|
|
79
|
+
for (let attempt = 1; attempt <= this.maxAttempts; attempt++) {
|
|
80
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
81
|
+
let retryDelay;
|
|
82
|
+
try {
|
|
83
|
+
const response = await this.fetchFunction(url, { headers: this.headers, ...signal ? { signal } : {} });
|
|
84
|
+
if (response.ok && response.status !== 204) {
|
|
85
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
86
|
+
return { status: "ok", url, bytes, contentType: response.headers.get("content-type") ?? void 0 };
|
|
87
|
+
}
|
|
88
|
+
if (this.absentStatuses.has(response.status)) return { status: "absent", url, httpStatus: response.status };
|
|
89
|
+
if (!this.retryStatuses.has(response.status)) {
|
|
90
|
+
throw new TileHttpError(`Tile request failed with HTTP ${response.status}: ${url}`, url, attempt, response.status);
|
|
91
|
+
}
|
|
92
|
+
lastStatus = response.status;
|
|
93
|
+
lastError = void 0;
|
|
94
|
+
retryDelay = retryAfterMs(response);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (error instanceof TileHttpError) throw error;
|
|
97
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
98
|
+
lastError = error;
|
|
99
|
+
lastStatus = void 0;
|
|
100
|
+
}
|
|
101
|
+
if (attempt < this.maxAttempts) {
|
|
102
|
+
const backoff = Math.min(this.maxDelayMs, this.initialDelayMs * 2 ** (attempt - 1)) + this.random() * this.initialDelayMs;
|
|
103
|
+
await this.sleep(Math.min(this.maxDelayMs, retryDelay ?? backoff), signal);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const reason = lastStatus === void 0 ? "a network error" : `HTTP ${lastStatus}`;
|
|
107
|
+
throw new TileHttpError(`Tile request failed after ${this.maxAttempts} attempts with ${reason}: ${url}`, url, this.maxAttempts, lastStatus, {
|
|
108
|
+
cause: lastError
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// src/sources/url-template.ts
|
|
114
|
+
var TileUrlTemplateError = class extends Error {
|
|
115
|
+
constructor(message) {
|
|
116
|
+
super(message);
|
|
117
|
+
this.name = "TileUrlTemplateError";
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
var BUILT_IN_TILE_URL_VARIABLES = ["z", "lod", "level", "x", "y", "-y", "quadkey", "face", "s"];
|
|
121
|
+
var VARIABLE_PATTERN = /\{(-?[A-Za-z_][A-Za-z0-9_]*)\}/g;
|
|
122
|
+
function tileUrlTemplateVariables(template) {
|
|
123
|
+
return [...template.matchAll(VARIABLE_PATTERN)].map((match) => match[1]);
|
|
124
|
+
}
|
|
125
|
+
function validateTileUrlTemplate(template, options = {}) {
|
|
126
|
+
const variables = options.variables ?? {};
|
|
127
|
+
for (const name of Object.keys(variables)) {
|
|
128
|
+
if (BUILT_IN_TILE_URL_VARIABLES.includes(name)) throw new TileUrlTemplateError(`Variable {${name}} is built in and cannot be redefined.`);
|
|
129
|
+
}
|
|
130
|
+
for (const name of tileUrlTemplateVariables(template)) {
|
|
131
|
+
if (name === "s") {
|
|
132
|
+
if (!options.subdomains || options.subdomains.length === 0) throw new TileUrlTemplateError(`Template "${template}" uses {s} but no subdomains were supplied.`);
|
|
133
|
+
} else if (!BUILT_IN_TILE_URL_VARIABLES.includes(name) && !Object.hasOwn(variables, name)) {
|
|
134
|
+
throw new TileUrlTemplateError(`Template "${template}" uses unknown variable {${name}}.`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function expandTileUrlTemplate(template, address, options = {}) {
|
|
139
|
+
validateTileUrlTemplate(template, options);
|
|
140
|
+
assertValidTileAddress(address);
|
|
141
|
+
const variables = options.variables ?? {};
|
|
142
|
+
return template.replace(VARIABLE_PATTERN, (_match, name) => {
|
|
143
|
+
switch (name) {
|
|
144
|
+
case "z":
|
|
145
|
+
case "lod":
|
|
146
|
+
case "level":
|
|
147
|
+
return String(address.lod);
|
|
148
|
+
case "x":
|
|
149
|
+
return String(address.x);
|
|
150
|
+
case "y":
|
|
151
|
+
return String(address.y);
|
|
152
|
+
case "-y":
|
|
153
|
+
return String(toTmsY(address.y, address.lod));
|
|
154
|
+
case "quadkey":
|
|
155
|
+
return tileAddressToQuadkey(address);
|
|
156
|
+
case "face":
|
|
157
|
+
return String(address.face ?? 0);
|
|
158
|
+
case "s": {
|
|
159
|
+
const subdomains = options.subdomains;
|
|
160
|
+
return subdomains[(address.x + address.y) % subdomains.length];
|
|
161
|
+
}
|
|
162
|
+
default:
|
|
163
|
+
return encodeURIComponent(String(variables[name]));
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export { BUILT_IN_TILE_URL_VARIABLES, TileHttpClient, TileHttpError, TileUrlTemplateError, expandTileUrlTemplate, tileUrlTemplateVariables, validateTileUrlTemplate };
|
|
169
|
+
//# sourceMappingURL=chunk-6YNCRDEE.js.map
|
|
170
|
+
//# sourceMappingURL=chunk-6YNCRDEE.js.map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Q,IAAA;AAAM,MACV,KAAK,GAAA;AAAA,MACL,KAAK,KAAA;AAAA,MACL,KAAK,OAAA;AACD,QAAA,OAAO,MAAA,CAAO,QAAQ,GAAG,CAAA;AAAA,MAC7B,KAAK,GAAA;AACD,QAAA,OAAO,MAAA,CAAO,QAAQ,CAAC,CAAA;AAAA,MAC3B,KAAK,GAAA;AACD,QAAA,OAAO,MAAA,CAAO,QAAQ,CAAC,CAAA;AAAA,MAC3B,KAAK,IAAA;AACD,QAAA,OAAO,OAAO,MAAA,CAAO,OAAA,CAAQ,CAAA,EAAG,OAAA,CAAQ,GAAG,CAAC,CAAA;AAAA,MAChD,KAAK,SAAA;AACD,QAAA,OAAO,qBAAqB,OAAO,CAAA;AAAA,MACvC,KAAK,MAAA;AACD,QAAA,OAAO,MAAA,CAAO,OAAA,CAAQ,IAAA,IAAQ,CAAC,CAAA;AAAA,MACnC,KAAK,GAAA,EAAK;AACN,QAAA,MAAM,aAAa,OAAA,CAAQ,UAAA;AAC3B,QAAA,OAAO,YAAY,OAAA,CAAQ,CAAA,GAAI,OAAA,CAAQ,CAAA,IAAK,WAAW,MAAM,CAAA;AAAA,MACjE;AAAA,MACA;AACI,QAAA,OAAO,kBAAA,CAAmB,MAAA,CAAO,SAAA,CAAU,IAAI,CAAC,CAAC,CAAA;AAAA;AACzD,EACJ,CAAC,CAAA;AACL","file":"chunk-6YNCRDEE.js","sourcesContent":["/** Fetch function compatible with `globalThis.fetch`. */\nexport type FetchFunction = (input: string, init?: RequestInit) => Promise<Response>;\n\n/** Options of {@link TileHttpClient}. */\nexport interface ITileHttpClientOptions {\n /** Defaults to `globalThis.fetch`. */\n fetch?: FetchFunction;\n /** Headers sent with every request, for example an `Authorization` header. */\n headers?: Readonly<Record<string, string>>;\n /** Total number of attempts, including the first one. Defaults to 4. */\n maxAttempts?: number;\n /** Base delay of the exponential backoff in milliseconds. Defaults to 500. */\n initialDelayMs?: number;\n /** Upper bound of a single delay in milliseconds, including `Retry-After`. Defaults to 30,000. */\n maxDelayMs?: number;\n /** HTTP statuses retried with backoff. Defaults to 408, 425, 429, 500, 502, 503 and 504. */\n retryStatuses?: readonly number[];\n /** HTTP statuses meaning that the tile does not exist. Defaults to 204 and 404. */\n absentStatuses?: readonly number[];\n /** Random source in `[0, 1)` for the jitter. Defaults to `Math.random`. */\n random?: () => number;\n /** Waits for a delay, rejecting when the signal aborts. Replaceable for tests. */\n sleep?: (milliseconds: number, signal?: AbortSignal) => Promise<void>;\n}\n\n/** Outcome of a tile request. A tile known to be missing is a result, not an error. */\nexport type TileFetchResult = { status: \"ok\"; url: string; bytes: Uint8Array; contentType: string | undefined } | { status: \"absent\"; url: string; httpStatus: number };\n\n/** Raised when a tile request fails permanently or exhausts its attempts. */\nexport class TileHttpError extends Error {\n public constructor(\n message: string,\n public readonly url: string,\n public readonly attempts: number,\n public readonly httpStatus?: number,\n options?: ErrorOptions\n ) {\n super(message, options);\n this.name = \"TileHttpError\";\n }\n}\n\nfunction abortReason(signal: AbortSignal): unknown {\n return signal.reason ?? new DOMException(\"The operation was aborted.\", \"AbortError\");\n}\n\nfunction defaultSleep(milliseconds: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(abortReason(signal));\n return;\n }\n const timer = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, milliseconds);\n const onAbort = (): void => {\n clearTimeout(timer);\n reject(abortReason(signal!));\n };\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n}\n\n/** Parses a `Retry-After` header expressed in seconds or as an HTTP date. */\nfunction retryAfterMs(response: Response): number | undefined {\n const value = response.headers.get(\"retry-after\");\n if (value === null) return undefined;\n const seconds = Number(value);\n if (Number.isFinite(seconds)) return Math.max(0, seconds * 1000);\n const date = Date.parse(value);\n return Number.isNaN(date) ? undefined : Math.max(0, date - Date.now());\n}\n\n/**\n * HTTP client for tile requests.\n *\n * - Statuses listed in `absentStatuses` resolve to `{ status: \"absent\" }`, so sparse pyramids need no error handling.\n * - Network errors and statuses listed in `retryStatuses` are retried with exponential backoff and jitter:\n * `min(maxDelayMs, initialDelayMs * 2^(attempt - 1)) + random() * initialDelayMs`, or `Retry-After` when present.\n * - Any other status fails immediately with a {@link TileHttpError}.\n * - Aborting the signal rejects with the abort reason, including during a backoff delay.\n */\nexport class TileHttpClient {\n private readonly fetchFunction: FetchFunction;\n private readonly headers: Readonly<Record<string, string>>;\n private readonly maxAttempts: number;\n private readonly initialDelayMs: number;\n private readonly maxDelayMs: number;\n private readonly retryStatuses: ReadonlySet<number>;\n private readonly absentStatuses: ReadonlySet<number>;\n private readonly random: () => number;\n private readonly sleep: (milliseconds: number, signal?: AbortSignal) => Promise<void>;\n\n public constructor(options: ITileHttpClientOptions = {}) {\n if (!options.fetch && typeof globalThis.fetch !== \"function\") throw new TypeError(\"No fetch implementation is available; supply options.fetch.\");\n // Browsers require fetch to be called on the global object, so the default is bound to it.\n this.fetchFunction = options.fetch ?? globalThis.fetch.bind(globalThis);\n this.headers = { ...(options.headers ?? {}) };\n this.maxAttempts = options.maxAttempts ?? 4;\n this.initialDelayMs = options.initialDelayMs ?? 500;\n this.maxDelayMs = options.maxDelayMs ?? 30_000;\n this.retryStatuses = new Set(options.retryStatuses ?? [408, 425, 429, 500, 502, 503, 504]);\n this.absentStatuses = new Set(options.absentStatuses ?? [204, 404]);\n this.random = options.random ?? Math.random;\n this.sleep = options.sleep ?? defaultSleep;\n if (!Number.isInteger(this.maxAttempts) || this.maxAttempts < 1) throw new RangeError(\"maxAttempts must be a positive integer.\");\n if (!(this.initialDelayMs >= 0) || !(this.maxDelayMs >= 0)) throw new RangeError(\"Backoff delays must be non-negative.\");\n }\n\n /**\n * Fetches one tile.\n *\n * @param url - Expanded tile URL.\n * @param signal - Optional abort signal.\n * @returns The tile bytes, or an `absent` result.\n * @throws TileHttpError When the request fails permanently or exhausts its attempts.\n */\n public async fetchTile(url: string, signal?: AbortSignal): Promise<TileFetchResult> {\n let lastError: unknown;\n let lastStatus: number | undefined;\n for (let attempt = 1; attempt <= this.maxAttempts; attempt++) {\n if (signal?.aborted) throw abortReason(signal);\n let retryDelay: number | undefined;\n try {\n const response = await this.fetchFunction(url, { headers: this.headers, ...(signal ? { signal } : {}) });\n if (response.ok && response.status !== 204) {\n const bytes = new Uint8Array(await response.arrayBuffer());\n return { status: \"ok\", url, bytes, contentType: response.headers.get(\"content-type\") ?? undefined };\n }\n if (this.absentStatuses.has(response.status)) return { status: \"absent\", url, httpStatus: response.status };\n if (!this.retryStatuses.has(response.status)) {\n throw new TileHttpError(`Tile request failed with HTTP ${response.status}: ${url}`, url, attempt, response.status);\n }\n lastStatus = response.status;\n lastError = undefined;\n retryDelay = retryAfterMs(response);\n } catch (error) {\n if (error instanceof TileHttpError) throw error;\n if (signal?.aborted) throw abortReason(signal);\n lastError = error;\n lastStatus = undefined;\n }\n if (attempt < this.maxAttempts) {\n const backoff = Math.min(this.maxDelayMs, this.initialDelayMs * 2 ** (attempt - 1)) + this.random() * this.initialDelayMs;\n await this.sleep(Math.min(this.maxDelayMs, retryDelay ?? backoff), signal);\n }\n }\n const reason = lastStatus === undefined ? \"a network error\" : `HTTP ${lastStatus}`;\n throw new TileHttpError(`Tile request failed after ${this.maxAttempts} attempts with ${reason}: ${url}`, url, this.maxAttempts, lastStatus, {\n cause: lastError,\n });\n }\n}\n","import { assertValidTileAddress, tileAddressToQuadkey, toTmsY, type ITileAddress } from \"../addressing\";\n\n/** Options used to expand a tile URL template. */\nexport interface ITileUrlTemplateOptions {\n /** Values substituted for `{s}`. Required when the template contains `{s}`. */\n subdomains?: readonly string[];\n /**\n * Application variables, for example `{format}` or `{accessToken}`.\n * Values are URI-encoded. Built-in variable names cannot be redefined.\n */\n variables?: Readonly<Record<string, string | number>>;\n}\n\n/** Raised when a template contains an unknown variable or misses a required option. */\nexport class TileUrlTemplateError extends Error {\n public constructor(message: string) {\n super(message);\n this.name = \"TileUrlTemplateError\";\n }\n}\n\n/**\n * Variables provided for every tile.\n *\n * - `{z}`, `{lod}`, `{level}`: level of detail.\n * - `{x}`: column, growing eastward.\n * - `{y}`: XYZ row, growing southward.\n * - `{-y}`: TMS row, growing northward.\n * - `{quadkey}`: Bing Maps quadkey, within the face.\n * - `{face}`: face of a multi-root pyramid, 0 for single-root pyramids.\n * - `{s}`: subdomain, chosen as `subdomains[(x + y) % subdomains.length]` so a tile always maps to the same host.\n */\nexport const BUILT_IN_TILE_URL_VARIABLES: readonly string[] = [\"z\", \"lod\", \"level\", \"x\", \"y\", \"-y\", \"quadkey\", \"face\", \"s\"];\n\nconst VARIABLE_PATTERN = /\\{(-?[A-Za-z_][A-Za-z0-9_]*)\\}/g;\n\n/** Lists the variable names of a template, without braces, in order of appearance. */\nexport function tileUrlTemplateVariables(template: string): string[] {\n return [...template.matchAll(VARIABLE_PATTERN)].map((match) => match[1]!);\n}\n\n/**\n * Checks that every variable of a template can be expanded with the given options.\n *\n * @throws TileUrlTemplateError When a variable is unknown, when `{s}` has no subdomains, or when an\n * application variable redefines a built-in variable.\n */\nexport function validateTileUrlTemplate(template: string, options: ITileUrlTemplateOptions = {}): void {\n const variables = options.variables ?? {};\n for (const name of Object.keys(variables)) {\n if (BUILT_IN_TILE_URL_VARIABLES.includes(name)) throw new TileUrlTemplateError(`Variable {${name}} is built in and cannot be redefined.`);\n }\n for (const name of tileUrlTemplateVariables(template)) {\n if (name === \"s\") {\n if (!options.subdomains || options.subdomains.length === 0) throw new TileUrlTemplateError(`Template \"${template}\" uses {s} but no subdomains were supplied.`);\n } else if (!BUILT_IN_TILE_URL_VARIABLES.includes(name) && !Object.hasOwn(variables, name)) {\n throw new TileUrlTemplateError(`Template \"${template}\" uses unknown variable {${name}}.`);\n }\n }\n}\n\n/**\n * Expands a tile URL template for an XYZ address.\n *\n * @throws TileUrlTemplateError See {@link validateTileUrlTemplate}.\n * @throws RangeError When the address lies outside its pyramid level.\n */\nexport function expandTileUrlTemplate(template: string, address: ITileAddress, options: ITileUrlTemplateOptions = {}): string {\n validateTileUrlTemplate(template, options);\n assertValidTileAddress(address);\n const variables = options.variables ?? {};\n return template.replace(VARIABLE_PATTERN, (_match, name: string) => {\n switch (name) {\n case \"z\":\n case \"lod\":\n case \"level\":\n return String(address.lod);\n case \"x\":\n return String(address.x);\n case \"y\":\n return String(address.y);\n case \"-y\":\n return String(toTmsY(address.y, address.lod));\n case \"quadkey\":\n return tileAddressToQuadkey(address);\n case \"face\":\n return String(address.face ?? 0);\n case \"s\": {\n const subdomains = options.subdomains!;\n return subdomains[(address.x + address.y) % subdomains.length]!;\n }\n default:\n return encodeURIComponent(String(variables[name]));\n }\n });\n}\n"]}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/surface/reference-surface.ts
|
|
2
|
+
var ReferenceSurface = class _ReferenceSurface {
|
|
3
|
+
radii;
|
|
4
|
+
inverseSquares;
|
|
5
|
+
/**
|
|
6
|
+
* @param a - Radius along `X`, in metres.
|
|
7
|
+
* @param b - Radius along `Y`, in metres.
|
|
8
|
+
* @param c - Radius along `Z`, the polar radius, in metres.
|
|
9
|
+
* @throws RangeError When a radius is not a positive finite number.
|
|
10
|
+
*/
|
|
11
|
+
constructor(a, b, c) {
|
|
12
|
+
for (const radius of [a, b, c]) {
|
|
13
|
+
if (!Number.isFinite(radius) || radius <= 0) throw new RangeError("The radii of a reference surface must be positive finite metres.");
|
|
14
|
+
}
|
|
15
|
+
this.radii = Object.freeze([a, b, c]);
|
|
16
|
+
this.inverseSquares = [1 / (a * a), 1 / (b * b), 1 / (c * c)];
|
|
17
|
+
}
|
|
18
|
+
/** A sphere of the given radius, in metres. */
|
|
19
|
+
static sphere(radius) {
|
|
20
|
+
return new _ReferenceSurface(radius, radius, radius);
|
|
21
|
+
}
|
|
22
|
+
/** The biaxial surface of an ellipsoid: its semi-major axis along `X` and `Y`, its semi-minor axis along `Z`. */
|
|
23
|
+
static fromEllipsoid(ellipsoid) {
|
|
24
|
+
return new _ReferenceSurface(ellipsoid.semiMajorAxis, ellipsoid.semiMajorAxis, ellipsoid.semiMinorAxis);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Distance from the centre to the surface along a unit direction: `1 / sqrt(x^2 / a^2 + y^2 / b^2 + z^2 / c^2)`.
|
|
28
|
+
*/
|
|
29
|
+
radius(direction) {
|
|
30
|
+
const [ia, ib, ic] = this.inverseSquares;
|
|
31
|
+
const { x, y, z } = direction;
|
|
32
|
+
return 1 / Math.sqrt(x * x * ia + y * y * ib + z * z * ic);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Point of a unit direction at a radial height: `(radius(direction) + height) * direction`.
|
|
36
|
+
*
|
|
37
|
+
* @param height - Radial height above the surface, in metres.
|
|
38
|
+
* @param target - Receives the point.
|
|
39
|
+
*/
|
|
40
|
+
point(direction, height = 0, target = { x: 0, y: 0, z: 0 }) {
|
|
41
|
+
const distance = this.radius(direction) + height;
|
|
42
|
+
target.x = direction.x * distance;
|
|
43
|
+
target.y = direction.y * distance;
|
|
44
|
+
target.z = direction.z * distance;
|
|
45
|
+
return target;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Radial height of a point above the surface: its distance to the centre minus the radius of its direction.
|
|
49
|
+
*
|
|
50
|
+
* @throws RangeError When the point is the centre or is not finite.
|
|
51
|
+
*/
|
|
52
|
+
height(point) {
|
|
53
|
+
const distance = Math.hypot(point.x, point.y, point.z);
|
|
54
|
+
if (!(distance > 0) || !Number.isFinite(distance)) throw new RangeError("The height of the centre is undefined.");
|
|
55
|
+
return distance - this.radius({ x: point.x / distance, y: point.y / distance, z: point.z / distance });
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Unit normal of the surface at a point of it, `normalize(x / a^2, y / b^2, z / c^2)`. It differs from the radial
|
|
59
|
+
* direction on a non-spherical surface. For a point off the surface it is the normal of the scaled surface through
|
|
60
|
+
* that point.
|
|
61
|
+
*
|
|
62
|
+
* @param target - Receives the normal.
|
|
63
|
+
*/
|
|
64
|
+
normal(point, target = { x: 0, y: 0, z: 0 }) {
|
|
65
|
+
const [ia, ib, ic] = this.inverseSquares;
|
|
66
|
+
const x = point.x * ia;
|
|
67
|
+
const y = point.y * ib;
|
|
68
|
+
const z = point.z * ic;
|
|
69
|
+
const length = Math.hypot(x, y, z);
|
|
70
|
+
target.x = x / length;
|
|
71
|
+
target.y = y / length;
|
|
72
|
+
target.z = z / length;
|
|
73
|
+
return target;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export { ReferenceSurface };
|
|
78
|
+
//# sourceMappingURL=chunk-7AWV2IEK.js.map
|
|
79
|
+
//# sourceMappingURL=chunk-7AWV2IEK.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/surface/reference-surface.ts"],"names":[],"mappings":";AAOO,IAAM,gBAAA,GAAN,MAAM,iBAAA,CAAiB;AAAA,EACV,KAAA;AAAA,EACC,cAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQV,WAAA,CAAY,CAAA,EAAW,CAAA,EAAW,CAAA,EAAW;AAChD,IAAA,KAAA,MAAW,MAAA,IAAU,CAAC,CAAA,EAAG,CAAA,EAAG,CAAC,CAAA,EAAG;AAC5B,MAAA,IAAI,CAAC,MAAA,CAAO,QAAA,CAAS,MAAM,CAAA,IAAK,UAAU,CAAA,EAAG,MAAM,IAAI,UAAA,CAAW,kEAAkE,CAAA;AAAA,IACxI;AACA,IAAA,IAAA,CAAK,QAAQ,MAAA,CAAO,MAAA,CAAO,CAAC,CAAA,EAAG,CAAA,EAAG,CAAC,CAAU,CAAA;AAC7C,IAAA,IAAA,CAAK,cAAA,GAAiB,CAAC,CAAA,IAAK,CAAA,GAAI,CAAA,CAAA,EAAI,KAAK,CAAA,GAAI,CAAA,CAAA,EAAI,CAAA,IAAK,CAAA,GAAI,CAAA,CAAE,CAAA;AAAA,EAChE;AAAA;AAAA,EAGA,OAAc,OAAO,MAAA,E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CAAM,GAAG,KAAA,CAAM,CAAA,EAAG,MAAM,CAAC,CAAA;AACrD,IAAA,IAAI,EAAE,QAAA,GAAW,CAAA,CAAA,IAAM,CAAC,MAAA,CAAO,QAAA,CAAS,QAAQ,CAAA,EAAG,MAAM,IAAI,UAAA,CAAW,wCAAwC,CAAA;AAChH,IAAA,OAAO,WAAW,IAAA,CAAK,MAAA,CAAO,EAAE,CAAA,EAAG,MAAM,CAAA,GAAI,QAAA,EAAU,CAAA,EAAG,KAAA,CAAM,IAAI,QAAA,EAAU,CAAA,EAAG,KAAA,CAAM,CAAA,GAAI,UAAU,CAAA;AAAA,EACzG;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,MAAA,CAAO,KAAA,EAA8B,MAAA,GAAsB,EAAE,CAAA,EAAG,GAAG,CAAA,EAAG,CAAA,EAAG,CAAA,EAAG,CAAA,EAAE,EAAgB;AACjG,IAAA,MAAM,CAAC,EAAA,EAAI,EAAA,EAAI,EAAE,IAAI,IAAA,CAAK,cAAA;AAC1B,IAAA,MAAM,CAAA,GAAI,MAAM,CAAA,GAAI,EAAA;AACpB,IAAA,MAAM,CAAA,GAAI,MAAM,CAAA,GAAI,EAAA;AACpB,IAAA,MAAM,CAAA,GAAI,MAAM,CAAA,GAAI,EAAA;AACpB,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,KAAA,CAAM,CAAA,EAAG,GAAG,CAAC,CAAA;AACjC,IAAA,MAAA,CAAO,IAAI,CAAA,GAAI,MAAA;AACf,IAAA,MAAA,CAAO,IAAI,CAAA,GAAI,MAAA;AACf,IAAA,MAAA,CAAO,IAAI,CAAA,GAAI,MAAA;AACf,IAAA,OAAO,MAAA;AAAA,EACX;AACJ","file":"chunk-7AWV2IEK.js","sourcesContent":["import type { Ellipsoid, ICartesian3 } from \"@spacexr/geodesy\";\n\n/**\n * Reference surface of a celestial body: a triaxial ellipsoid with radii `a`, `b`, `c` along the `X`, `Y` and `Z`\n * axes of its body-fixed frame, in metres. Terrain heights are radial: the point of direction `d` and height `h` is\n * `(radius(d) + h) * d`. No geodetic latitude is involved, so triaxial bodies need no special case.\n */\nexport class ReferenceSurface {\n public readonly radii: readonly [number, number, number];\n private readonly inverseSquares: readonly [number, number, number];\n\n /**\n * @param a - Radius along `X`, in metres.\n * @param b - Radius along `Y`, in metres.\n * @param c - Radius along `Z`, the polar radius, in metres.\n * @throws RangeError When a radius is not a positive finite number.\n */\n public constructor(a: number, b: number, c: number) {\n for (const radius of [a, b, c]) {\n if (!Number.isFinite(radius) || radius <= 0) throw new RangeError(\"The radii of a reference surface must be positive finite metres.\");\n }\n this.radii = Object.freeze([a, b, c] as const);\n this.inverseSquares = [1 / (a * a), 1 / (b * b), 1 / (c * c)];\n }\n\n /** A sphere of the given radius, in metres. */\n public static sphere(radius: number): ReferenceSurface {\n return new ReferenceSurface(radius, radius, radius);\n }\n\n /** The biaxial surface of an ellipsoid: its semi-major axis along `X` and `Y`, its semi-minor axis along `Z`. */\n public static fromEllipsoid(ellipsoid: Ellipsoid): ReferenceSurface {\n return new ReferenceSurface(ellipsoid.semiMajorAxis, ellipsoid.semiMajorAxis, ellipsoid.semiMinorAxis);\n }\n\n /**\n * Distance from the centre to the surface along a unit direction: `1 / sqrt(x^2 / a^2 + y^2 / b^2 + z^2 / c^2)`.\n */\n public radius(direction: Readonly<ICartesian3>): number {\n const [ia, ib, ic] = this.inverseSquares;\n const { x, y, z } = direction;\n return 1 / Math.sqrt(x * x * ia + y * y * ib + z * z * ic);\n }\n\n /**\n * Point of a unit direction at a radial height: `(radius(direction) + height) * direction`.\n *\n * @param height - Radial height above the surface, in metres.\n * @param target - Receives the point.\n */\n public point(direction: Readonly<ICartesian3>, height = 0, target: ICartesian3 = { x: 0, y: 0, z: 0 }): ICartesian3 {\n const distance = this.radius(direction) + height;\n target.x = direction.x * distance;\n target.y = direction.y * distance;\n target.z = direction.z * distance;\n return target;\n }\n\n /**\n * Radial height of a point above the surface: its distance to the centre minus the radius of its direction.\n *\n * @throws RangeError When the point is the centre or is not finite.\n */\n public height(point: Readonly<ICartesian3>): number {\n const distance = Math.hypot(point.x, point.y, point.z);\n if (!(distance > 0) || !Number.isFinite(distance)) throw new RangeError(\"The height of the centre is undefined.\");\n return distance - this.radius({ x: point.x / distance, y: point.y / distance, z: point.z / distance });\n }\n\n /**\n * Unit normal of the surface at a point of it, `normalize(x / a^2, y / b^2, z / c^2)`. It differs from the radial\n * direction on a non-spherical surface. For a point off the surface it is the normal of the scaled surface through\n * that point.\n *\n * @param target - Receives the normal.\n */\n public normal(point: Readonly<ICartesian3>, target: ICartesian3 = { x: 0, y: 0, z: 0 }): ICartesian3 {\n const [ia, ib, ic] = this.inverseSquares;\n const x = point.x * ia;\n const y = point.y * ib;\n const z = point.z * ic;\n const length = Math.hypot(x, y, z);\n target.x = x / length;\n target.y = y / length;\n target.z = z / length;\n return target;\n }\n}\n"]}
|