@tangram.gl/basemap-layers 0.0.1 → 10.0.0-alpha.3

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.
Files changed (139) hide show
  1. package/README.md +12 -7
  2. package/THIRD-PARTY-NOTICES.md +7 -5
  3. package/dist/geojson-task-worker.js +3166 -0
  4. package/dist/geojson-task-worker.js.map +6 -0
  5. package/dist/geojson-worker.js +1089 -7
  6. package/dist/geojson-worker.js.map +3 -3
  7. package/dist/index.cjs +9588 -2093
  8. package/dist/index.cjs.map +3 -3
  9. package/dist/index.js +9599 -2090
  10. package/dist/index.js.map +3 -3
  11. package/dist/map-style.cjs +456 -20
  12. package/dist/map-style.cjs.map +3 -3
  13. package/dist/map-style.js +456 -19
  14. package/dist/map-style.js.map +2 -2
  15. package/dist/types/basemap-error.d.ts +52 -0
  16. package/dist/types/basemap-layer.d.ts +378 -25
  17. package/dist/types/basemap-protocol.d.ts +91 -0
  18. package/dist/types/basemap-request.d.ts +73 -0
  19. package/dist/types/circle-layer.d.ts +65 -8
  20. package/dist/types/color-relief-layer.d.ts +184 -0
  21. package/dist/types/dem-pixels.d.ts +40 -0
  22. package/dist/types/feature-state.d.ts +55 -0
  23. package/dist/types/fill-extrusion-lighting-extension.d.ts +1 -2
  24. package/dist/types/fill-outline-extension.d.ts +2 -3
  25. package/dist/types/fill-pattern.d.ts +35 -3
  26. package/dist/types/fill-style-extensions.d.ts +11 -59
  27. package/dist/types/geojson-resource.d.ts +18 -0
  28. package/dist/types/geojson-source.d.ts +13 -2
  29. package/dist/types/geojson-task-handler.d.ts +2 -0
  30. package/dist/types/geojson-task.d.ts +26 -0
  31. package/dist/types/geojson-tiler.d.ts +29 -2
  32. package/dist/types/geojson-worker.d.ts +5 -1
  33. package/dist/types/globe-horizon-extension.d.ts +2 -45
  34. package/dist/types/globe-layers.d.ts +141 -45
  35. package/dist/types/globe-line-angle-extension.d.ts +20 -17
  36. package/dist/types/glyph-store.d.ts +10 -1
  37. package/dist/types/heatmap-layer.d.ts +99 -0
  38. package/dist/types/hillshade-layer.d.ts +159 -27
  39. package/dist/types/icon-text-fit.d.ts +92 -0
  40. package/dist/types/image-source-layer.d.ts +142 -0
  41. package/dist/types/index.d.ts +25 -1
  42. package/dist/types/line-blur-extension.d.ts +3 -11
  43. package/dist/types/line-dash-extension.d.ts +53 -0
  44. package/dist/types/line-gradient-extension.d.ts +69 -0
  45. package/dist/types/line-metrics.d.ts +24 -0
  46. package/dist/types/line-pattern-extension.d.ts +50 -0
  47. package/dist/types/line-shape-extension.d.ts +76 -0
  48. package/dist/types/map-style-loader.d.ts +1 -1
  49. package/dist/types/map-style.d.ts +2 -2
  50. package/dist/types/mvt-label-layer.d.ts +111 -20
  51. package/dist/types/pmtiles-source.d.ts +38 -0
  52. package/dist/types/projected-background-layer.d.ts +63 -0
  53. package/dist/types/projected-bitmap-layer.d.ts +58 -0
  54. package/dist/types/projected-tile-data.d.ts +16 -0
  55. package/dist/types/projected-tile-selection.d.ts +72 -0
  56. package/dist/types/projected-tileset.d.ts +92 -0
  57. package/dist/types/projected-viewport.d.ts +76 -0
  58. package/dist/types/raster-color-extension.d.ts +4 -7
  59. package/dist/types/raster-fade.d.ts +155 -0
  60. package/dist/types/raster-tile-fingerprint.d.ts +11 -0
  61. package/dist/types/raster-tile-loader.d.ts +80 -0
  62. package/dist/types/shader-injection-plugin.d.ts +8 -0
  63. package/dist/types/source-features.d.ts +58 -0
  64. package/dist/types/source-tiles.d.ts +32 -0
  65. package/dist/types/sprite.d.ts +37 -3
  66. package/dist/types/stretch-icon-layer.d.ts +28 -0
  67. package/dist/types/style-accessor.d.ts +50 -12
  68. package/dist/types/style-expression.d.ts +22 -2
  69. package/dist/types/style-resolver.d.ts +47 -2
  70. package/dist/types/terrain-query.d.ts +39 -0
  71. package/dist/types/zoom-load-gate.d.ts +22 -0
  72. package/dist/types/zoom-paint-extension.d.ts +80 -11
  73. package/package.json +24 -21
  74. package/src/basemap-error.ts +82 -0
  75. package/src/basemap-layer.ts +1289 -101
  76. package/src/basemap-protocol.ts +353 -0
  77. package/src/basemap-request.ts +212 -0
  78. package/src/circle-layer.ts +430 -54
  79. package/src/color-relief-layer.ts +572 -0
  80. package/src/dem-pixels.ts +174 -0
  81. package/src/feature-state.ts +164 -0
  82. package/src/fill-extrusion-lighting-extension.ts +126 -9
  83. package/src/fill-outline-extension.ts +4 -4
  84. package/src/fill-pattern.ts +87 -9
  85. package/src/fill-style-extensions.ts +114 -44
  86. package/src/geojson-resource.ts +58 -0
  87. package/src/geojson-source.ts +106 -85
  88. package/src/geojson-task-handler.ts +11 -0
  89. package/src/geojson-task-worker.ts +8 -0
  90. package/src/geojson-task.ts +34 -0
  91. package/src/geojson-tiler.ts +270 -4
  92. package/src/geojson-worker.ts +36 -6
  93. package/src/globe-horizon-extension.ts +14 -4
  94. package/src/globe-layers.ts +1905 -406
  95. package/src/globe-line-angle-extension.ts +183 -23
  96. package/src/glyph-store.ts +28 -6
  97. package/src/heatmap-layer.ts +591 -0
  98. package/src/hillshade-layer.ts +309 -88
  99. package/src/icon-text-fit.ts +358 -0
  100. package/src/image-source-layer.ts +380 -0
  101. package/src/index.ts +58 -1
  102. package/src/line-blur-extension.ts +14 -12
  103. package/src/line-dash-extension.ts +303 -0
  104. package/src/line-gradient-extension.ts +191 -0
  105. package/src/line-metrics.ts +90 -0
  106. package/src/line-pattern-extension.ts +173 -0
  107. package/src/line-shape-extension.ts +272 -0
  108. package/src/map-style-loader.ts +5 -4
  109. package/src/map-style.ts +2 -1
  110. package/src/mvt-label-layer.ts +568 -144
  111. package/src/pmtiles-source.ts +276 -0
  112. package/src/projected-background-layer.ts +202 -0
  113. package/src/projected-bitmap-layer.ts +262 -0
  114. package/src/projected-tile-data.ts +132 -0
  115. package/src/projected-tile-selection.ts +605 -0
  116. package/src/projected-tileset.ts +183 -0
  117. package/src/projected-viewport.ts +254 -0
  118. package/src/raster-color-extension.ts +43 -18
  119. package/src/raster-fade.ts +319 -0
  120. package/src/raster-tile-fingerprint.ts +31 -0
  121. package/src/raster-tile-loader.ts +211 -0
  122. package/src/shader-injection-plugin.ts +25 -0
  123. package/src/source-features.ts +170 -0
  124. package/src/source-tiles.ts +102 -0
  125. package/src/sprite.ts +122 -15
  126. package/src/stretch-icon-layer.ts +87 -0
  127. package/src/style-accessor.ts +105 -17
  128. package/src/style-expression.ts +80 -3
  129. package/src/style-resolver.ts +200 -22
  130. package/src/terrain-query.ts +86 -0
  131. package/src/text-layout.ts +2 -3
  132. package/src/zoom-load-gate.ts +57 -0
  133. package/src/zoom-paint-extension.ts +245 -47
  134. package/dist/types/arabic-shaping.d.ts +0 -7
  135. package/dist/types/bidi.d.ts +0 -8
  136. package/dist/types/polylabel.d.ts +0 -17
  137. package/src/arabic-shaping.ts +0 -366
  138. package/src/bidi.ts +0 -199
  139. package/src/polylabel.ts +0 -312
@@ -0,0 +1,605 @@
1
+ // tangram-layers
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ getMercatorZoom,
7
+ lngLatToMercatorWorld,
8
+ MAX_MERCATOR_LATITUDE,
9
+ MERCATOR_WORLD_SIZE,
10
+ mercatorWorldToLngLat,
11
+ type ProjectedViewportLike
12
+ } from './projected-viewport';
13
+
14
+ /** The index of a Web Mercator (OSM) tile. */
15
+ export type ProjectedTileIndex = {x: number; y: number; z: number};
16
+
17
+ /** Options of {@link getProjectedTileIndices}. */
18
+ export type ProjectedTileSelectionOptions = {
19
+ /** The tile zoom to select. */
20
+ z: number;
21
+ /** Longitude/latitude bounds `[west, south, east, north]` that limit the selection. */
22
+ extent?: ArrayLike<number> | null;
23
+ /**
24
+ * The most tiles to select. A view that needs more draws the tiles of a lower zoom: a projected
25
+ * view can show high latitudes, where Web Mercator tiles are small, far from its center.
26
+ */
27
+ maxTiles?: number;
28
+ /** The highest tile zoom of the source. Tiles are not divided past it. */
29
+ maxZoom?: number;
30
+ /** The lowest tile zoom of the source. No tiles of a lower zoom stand in for too many tiles. */
31
+ minZoom?: number;
32
+ /**
33
+ * How many times longer than at true scale the projection may draw a part of a tile's outline
34
+ * (see `isTileStretched`). Default `DEFAULT_MAX_STRETCH`. A projection that stretches the map
35
+ * smoothly toward the edge of its area of use, as a polar stereographic one, can take more: 16
36
+ * draws EPSG:3031 out to latitude 60N, where math.gl clamps its positions. A larger limit also
37
+ * stops seeing the places a projection cannot draw, as in a transverse Mercator projection of
38
+ * the whole world, whose tiles are then drawn across the map.
39
+ */
40
+ maxStretch?: number;
41
+ };
42
+
43
+ /** The default of `ProjectedTileSelectionOptions.maxStretch`. */
44
+ export const DEFAULT_MAX_STRETCH = 4;
45
+
46
+ /** The default of `ProjectedTileSelectionOptions.maxTiles`. */
47
+ export const DEFAULT_MAX_PROJECTED_TILES = 512;
48
+
49
+ /** The screen is sampled at the corners of a grid of this many cells along each side. */
50
+ const SCREEN_GRID_CELLS = 16;
51
+ /** A tile's edge is sampled at this many points along each side. */
52
+ const TILE_GRID_CELLS = 8;
53
+ /**
54
+ * The scale of the map plane at true scale: common units per degree of arc. deck.gl's common
55
+ * space is `MERCATOR_WORLD_SIZE` units around the equator, in a projected view too.
56
+ */
57
+ const TRUE_PLANE_SCALE = MERCATOR_WORLD_SIZE / 360;
58
+ /**
59
+ * A view center that the projection draws up to this many times its true scale does not change
60
+ * the stretch limit. A view of a place drawn larger than that, far outside the area of use,
61
+ * measures the stretch against half the scale at its center, so that it still draws the tiles
62
+ * at its center.
63
+ */
64
+ const MAX_CENTER_STRETCH = 2;
65
+ /**
66
+ * The zoom at which the edge of the drawn area is decided. A stretched tile of a lower zoom is
67
+ * divided down to this zoom, and the parts still stretched there are left out. A tile of a higher
68
+ * zoom is drawn only where its ancestor at this zoom is not stretched. So the edge is the same at
69
+ * every zoom of the view.
70
+ */
71
+ const EDGE_ZOOM = 5;
72
+ /** The step, in degrees, that measures the scale of the projection at the view center. */
73
+ const SCALE_STEP_DEGREES = 1e-3;
74
+ /** Pixels by which a sample may miss its own pixel when it is projected back. */
75
+ const ROUND_TRIP_PIXELS = 1;
76
+ /** The share of a tile's size by which a tile sample may miss itself when it is projected back. */
77
+ const ROUND_TRIP_TILE_SHARE = 1e-3;
78
+
79
+ /** A point of the screen that shows the map, in tile pyramid units: [0, 1) across the world. */
80
+ type ScreenSample = {u: number; v: number};
81
+
82
+ type TileNode = ProjectedTileIndex & {
83
+ /** The screen samples that lie in the tile. */
84
+ samples: ScreenSample[];
85
+ };
86
+
87
+ function wrapLongitude(longitude: number): number {
88
+ return ((((longitude + 180) % 360) + 360) % 360) - 180;
89
+ }
90
+
91
+ function toPyramid(lngLat: ArrayLike<number>): ScreenSample {
92
+ const world = lngLatToMercatorWorld([wrapLongitude(lngLat[0]), lngLat[1]]);
93
+ return {u: world[0] / MERCATOR_WORLD_SIZE, v: 1 - world[1] / MERCATOR_WORLD_SIZE};
94
+ }
95
+
96
+ /** The longitude/latitude of a point of the tile pyramid. */
97
+ function fromPyramid(u: number, v: number): [number, number] {
98
+ return mercatorWorldToLngLat([u * MERCATOR_WORLD_SIZE, (1 - v) * MERCATOR_WORLD_SIZE]);
99
+ }
100
+
101
+ /**
102
+ * The pixel of a longitude/latitude, with its depth, or null where the view has none. deck.gl
103
+ * throws for a position that its projection converts to no number, as far outside the area of
104
+ * use of a transverse Mercator projection.
105
+ */
106
+ function projectOrNull(viewport: ProjectedViewportLike, lngLat: ArrayLike<number>): number[] | null {
107
+ try {
108
+ const pixel = viewport.project([lngLat[0], lngLat[1], 0]);
109
+ return Number.isFinite(pixel[0]) && Number.isFinite(pixel[1]) ? pixel : null;
110
+ } catch {
111
+ return null;
112
+ }
113
+ }
114
+
115
+ /** The longitude/latitude of a pixel, or null where the view has none. */
116
+ function unprojectOrNull(viewport: ProjectedViewportLike, pixel: number[]): number[] | null {
117
+ try {
118
+ const lngLat = viewport.unproject([pixel[0], pixel[1]]);
119
+ return Number.isFinite(lngLat[0]) && Number.isFinite(lngLat[1]) ? lngLat : null;
120
+ } catch {
121
+ return null;
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Whether a projected position, `[x, y, depth]`, lies in front of the camera. A pitched view
127
+ * projects the ground behind its camera too, mirrored, to pixels above the horizon, with a depth
128
+ * outside the range of the view. A position without a depth counts as in front.
129
+ */
130
+ function isInFrontOfCamera(pixel: number[]): boolean {
131
+ const depth = pixel[2];
132
+ return depth === undefined || (depth >= 0 && depth <= 1);
133
+ }
134
+
135
+ /**
136
+ * The points of the screen that show the map. A pixel shows the map if it has a longitude and
137
+ * latitude inside the tile pyramid that the view draws back at that pixel: a projection draws
138
+ * positions outside its area of use elsewhere, or nowhere, and a pitched view has pixels above
139
+ * the horizon, which show the ground behind the camera, mirrored.
140
+ */
141
+ function getScreenSamples(viewport: ProjectedViewportLike): ScreenSample[] {
142
+ const samples: ScreenSample[] = [];
143
+ const moved: number[][] = [];
144
+ const addSample = (pixel: number[]): void => {
145
+ const lngLat = unprojectOrNull(viewport, pixel);
146
+ if (!lngLat || Math.abs(lngLat[1]) > MAX_MERCATOR_LATITUDE) {
147
+ return;
148
+ }
149
+ const back = projectOrNull(viewport, lngLat);
150
+ if (!back || !isInFrontOfCamera(back)) {
151
+ return;
152
+ }
153
+ if (
154
+ Math.abs(back[0] - pixel[0]) <= ROUND_TRIP_PIXELS &&
155
+ Math.abs(back[1] - pixel[1]) <= ROUND_TRIP_PIXELS
156
+ ) {
157
+ samples.push(toPyramid(lngLat));
158
+ } else if (isOnScreen(back, viewport.width, viewport.height)) {
159
+ moved.push(back);
160
+ }
161
+ };
162
+ for (let i = 0; i <= SCREEN_GRID_CELLS; i++) {
163
+ for (let j = 0; j <= SCREEN_GRID_CELLS; j++) {
164
+ addSample([
165
+ (i / SCREEN_GRID_CELLS) * viewport.width,
166
+ (j / SCREEN_GRID_CELLS) * viewport.height
167
+ ]);
168
+ }
169
+ }
170
+ // No screen point shows the map, but the view can still draw a part of it smaller than the
171
+ // gaps between them, as a zoomed-out UTM zone. A projection that moves positions outside its
172
+ // area of use to the edge of that area draws a screen point's position there: on that part.
173
+ if (!samples.length) {
174
+ for (const pixel of moved.splice(0)) {
175
+ addSample(pixel);
176
+ }
177
+ }
178
+ return samples;
179
+ }
180
+
181
+ /** Whether two segments, `a` to `b` and `c` to `d`, cross. */
182
+ function segmentsCross(a: number[], b: number[], c: number[], d: number[]): boolean {
183
+ const side = (p: number[], q: number[], r: number[]) =>
184
+ Math.sign((q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]));
185
+ return side(a, b, c) !== side(a, b, d) && side(c, d, a) !== side(c, d, b);
186
+ }
187
+
188
+ const isOnScreen = ([x, y]: number[], width: number, height: number) =>
189
+ x >= 0 && x <= width && y >= 0 && y <= height;
190
+
191
+ /**
192
+ * Whether an outline, as the view draws it, has a part on the screen: a point on the screen, or
193
+ * a segment between two points across a screen edge. A point without a pixel has no segment.
194
+ */
195
+ function isOutlineOnScreen(ring: (number[] | null)[], width: number, height: number): boolean {
196
+ const corners = [
197
+ [0, 0],
198
+ [width, 0],
199
+ [width, height],
200
+ [0, height]
201
+ ];
202
+ for (let index = 0; index < ring.length; index++) {
203
+ const a = ring[index];
204
+ const b = ring[(index + 1) % ring.length];
205
+ if (!a) {
206
+ continue;
207
+ }
208
+ if (
209
+ isOnScreen(a, width, height) ||
210
+ (b && corners.some((corner, side) => segmentsCross(a, b, corner, corners[(side + 1) % 4])))
211
+ ) {
212
+ return true;
213
+ }
214
+ }
215
+ // An outline around the whole screen is not on it: such a tile holds every screen sample.
216
+ return false;
217
+ }
218
+
219
+ /**
220
+ * Whether the view draws a part of a tile on the screen. The tile's edge is sampled, and the
221
+ * outline of its samples, as the view draws them, is tested against the screen (see
222
+ * `isOutlineOnScreen`). A sample counts only if the view draws it at a pixel that shows it again:
223
+ * positions outside the projection's area of use, or behind the camera of a pitched view, do not,
224
+ * and the outline has no segment to or from them.
225
+ *
226
+ * Most tiles that the selection tests are off the screen: the tiles around the ones the screen
227
+ * shows, at each zoom of the walk. So the outline is tested first as the view projects it, and
228
+ * the samples are projected back only for an outline that can be on the screen: to leave samples
229
+ * out can only take parts off the outline.
230
+ */
231
+ function isTileOnScreen(viewport: ProjectedViewportLike, {x, y, z}: ProjectedTileIndex): boolean {
232
+ const {width, height} = viewport;
233
+ const scale = 2 ** z;
234
+ const tolerance = (360 / scale) * ROUND_TRIP_TILE_SHARE;
235
+ const lngLats: [number, number][] = [];
236
+ const projected: (number[] | null)[] = [];
237
+ // Around the tile: the top edge, the right, the bottom and the left.
238
+ for (let side = 0; side < 4; side++) {
239
+ for (let step = 0; step < TILE_GRID_CELLS; step++) {
240
+ const t = step / TILE_GRID_CELLS;
241
+ const [i, j] = [
242
+ [t, 0],
243
+ [1, t],
244
+ [1 - t, 1],
245
+ [0, 1 - t]
246
+ ][side];
247
+ const lngLat = fromPyramid((x + i) / scale, (y + j) / scale);
248
+ const pixel = projectOrNull(viewport, lngLat);
249
+ lngLats[lngLats.length] = lngLat;
250
+ projected[projected.length] = pixel && isInFrontOfCamera(pixel) ? pixel : null;
251
+ }
252
+ }
253
+ if (!isOutlineOnScreen(projected, width, height)) {
254
+ return false;
255
+ }
256
+ const ring = projected.map((pixel, index) => {
257
+ const back = pixel && unprojectOrNull(viewport, pixel);
258
+ const lngLat = lngLats[index];
259
+ const isDrawn =
260
+ back &&
261
+ Math.abs(wrapLongitude(back[0] - lngLat[0])) <= tolerance &&
262
+ Math.abs(back[1] - lngLat[1]) <= tolerance;
263
+ return pixel && isDrawn ? [pixel[0], pixel[1]] : null;
264
+ });
265
+ return isOutlineOnScreen(ring, width, height);
266
+ }
267
+
268
+ /** The position of a longitude/latitude in the view's map plane, or null where it has none. */
269
+ function projectPositionOrNull(
270
+ viewport: ProjectedViewportLike,
271
+ lngLat: ArrayLike<number>
272
+ ): number[] | null {
273
+ try {
274
+ return viewport.projectPosition!([lngLat[0], lngLat[1], 0]);
275
+ } catch {
276
+ return null;
277
+ }
278
+ }
279
+
280
+ /**
281
+ * The length the view's map plane gives a step between two positions, per degree of arc on the
282
+ * ground: common units per degree. `from` and `to` are the two positions in the map plane (see
283
+ * `projectPositionOrNull`). Infinite where the projection has no position for an end.
284
+ */
285
+ function getPlaneScale(
286
+ a: ArrayLike<number>,
287
+ b: ArrayLike<number>,
288
+ from: number[] | null,
289
+ to: number[] | null
290
+ ): number {
291
+ if (!from || !to) {
292
+ return Infinity;
293
+ }
294
+ const cosLatitude = Math.cos((((a[1] + b[1]) / 2) * Math.PI) / 180);
295
+ const arc = Math.hypot(wrapLongitude(b[0] - a[0]) * cosLatitude, b[1] - a[1]);
296
+ const length = Math.hypot(to[0] - from[0], to[1] - from[1]);
297
+ return Number.isFinite(length) ? length / arc : Infinity;
298
+ }
299
+
300
+ /** The scale of the view's map plane at the view center (see `getPlaneScale`), or null. */
301
+ function getCenterPlaneScale(viewport: ProjectedViewportLike): number | null {
302
+ const center = unprojectOrNull(viewport, [viewport.width / 2, viewport.height / 2]);
303
+ if (!center || !viewport.projectPosition) {
304
+ return null;
305
+ }
306
+ const eastward = [center[0] + SCALE_STEP_DEGREES, center[1]];
307
+ const northward = [center[0], center[1] + SCALE_STEP_DEGREES];
308
+ const centerPlane = projectPositionOrNull(viewport, center);
309
+ const east = getPlaneScale(center, eastward, centerPlane, projectPositionOrNull(viewport, eastward));
310
+ const north = getPlaneScale(
311
+ center,
312
+ northward,
313
+ centerPlane,
314
+ projectPositionOrNull(viewport, northward)
315
+ );
316
+ const scale = Math.sqrt(east * north);
317
+ return Number.isFinite(scale) && scale > 0 ? scale : null;
318
+ }
319
+
320
+ /**
321
+ * The longest step, in common units per degree of arc, that the projection may draw along a
322
+ * tile's outline (see `isTileStretched`), or null where the view has no map plane. It does not
323
+ * depend on the camera in a view of the area of use: a limit that moves with the view center
324
+ * moves the edge of the drawn area, and the tiles at the edge come and go as the view pans.
325
+ */
326
+ function getStretchLimit(viewport: ProjectedViewportLike, maxStretch: number): number | null {
327
+ const centerScale = getCenterPlaneScale(viewport);
328
+ return centerScale === null
329
+ ? null
330
+ : maxStretch * Math.max(TRUE_PLANE_SCALE, centerScale / MAX_CENTER_STRETCH);
331
+ }
332
+
333
+ /**
334
+ * The most tiles whose outline scale is kept for one projection. Past it the oldest is dropped:
335
+ * a view shows tens of tiles, and a zoom over the whole pyramid a few hundred.
336
+ */
337
+ export const MAX_KEPT_OUTLINE_SCALES = 4096;
338
+
339
+ /** The outline scale of the tiles of a projection (see `getTileOutlineScale`), by tile. */
340
+ const outlineScales = new WeakMap<object, Map<string, number>>();
341
+
342
+ /**
343
+ * The largest scale the projection gives a part of a tile's outline (see `getPlaneScale`), or
344
+ * infinity where it has no position for a part. The scale is a property of the projection and
345
+ * the tile, and not of the camera: `projectPosition` converts a longitude/latitude to the map
346
+ * plane before the camera. So it is measured one time for each tile of a projection, which a
347
+ * viewport names by its `crs`, and kept: a view change tests the same tiles again. The outline is
348
+ * walked as a chain, so each of its points is projected one time.
349
+ */
350
+ function getTileOutlineScale(viewport: ProjectedViewportLike, {x, y, z}: ProjectedTileIndex): number {
351
+ const {crs} = viewport;
352
+ let scales = crs && outlineScales.get(crs);
353
+ if (crs && !scales) {
354
+ scales = new Map();
355
+ outlineScales.set(crs, scales);
356
+ }
357
+ const key = `${z}/${x}/${y}`;
358
+ const kept = scales?.get(key);
359
+ if (kept !== undefined) {
360
+ return kept;
361
+ }
362
+ const outlineScale = measureTileOutlineScale(viewport, {x, y, z});
363
+ if (scales) {
364
+ if (scales.size >= MAX_KEPT_OUTLINE_SCALES) {
365
+ scales.delete(scales.keys().next().value as string);
366
+ }
367
+ scales.set(key, outlineScale);
368
+ }
369
+ return outlineScale;
370
+ }
371
+
372
+ function measureTileOutlineScale(
373
+ viewport: ProjectedViewportLike,
374
+ {x, y, z}: ProjectedTileIndex
375
+ ): number {
376
+ const scale = 2 ** z;
377
+ let previous = fromPyramid(x / scale, y / scale);
378
+ let previousPlane = projectPositionOrNull(viewport, previous);
379
+ let outlineScale = 0;
380
+ for (let side = 0; side < 4; side++) {
381
+ for (let step = 1; step <= TILE_GRID_CELLS; step++) {
382
+ const t = step / TILE_GRID_CELLS;
383
+ const [i, j] = [
384
+ [t, 0],
385
+ [1, t],
386
+ [1 - t, 1],
387
+ [0, 1 - t]
388
+ ][side];
389
+ const lngLat = fromPyramid((x + i) / scale, (y + j) / scale);
390
+ const plane = projectPositionOrNull(viewport, lngLat);
391
+ const stepScale = getPlaneScale(previous, lngLat, previousPlane, plane);
392
+ // A step without a scale, or without a number for it, is stretched without bound.
393
+ if (!Number.isFinite(stepScale)) {
394
+ return Infinity;
395
+ }
396
+ outlineScale = Math.max(outlineScale, stepScale);
397
+ previous = lngLat;
398
+ previousPlane = plane;
399
+ }
400
+ }
401
+ return outlineScale;
402
+ }
403
+
404
+ /**
405
+ * Whether the projection draws a step along a tile's outline longer than `limit` (see
406
+ * `getStretchLimit`), or has no position for it. Such a tile holds a place the projection
407
+ * cannot draw, or a seam: its geometry is drawn across the map. A projection that moves positions
408
+ * outside its area of use to the edge of that area shrinks them, and is not stretched.
409
+ */
410
+ function isTileStretched(
411
+ viewport: ProjectedViewportLike,
412
+ index: ProjectedTileIndex,
413
+ limit: number
414
+ ): boolean {
415
+ return !(getTileOutlineScale(viewport, index) <= limit);
416
+ }
417
+
418
+ function isTileInExtent({x, y, z}: ProjectedTileIndex, extent: ArrayLike<number>): boolean {
419
+ const scale = 2 ** z;
420
+ const [west, north] = fromPyramid(x / scale, y / scale);
421
+ const [east, south] = fromPyramid((x + 1) / scale, (y + 1) / scale);
422
+ return west < extent[2] && east > extent[0] && south < extent[3] && north > extent[1];
423
+ }
424
+
425
+ /**
426
+ * Divides the tiles that the projection stretches into the tiles of the next zooms, down to
427
+ * `EDGE_ZOOM` (or `maxZoom`), and leaves out the parts that are still stretched there. A tile of a
428
+ * higher zoom is left out where its ancestor at that zoom is stretched. The area drawn is then the
429
+ * same for each tile zoom: the edge tiles do not come and go as the view zooms. Where the division
430
+ * makes more than `maxTiles` tiles, the stretched tiles are left out whole.
431
+ */
432
+ function divideStretchedTiles(
433
+ viewport: ProjectedViewportLike,
434
+ tiles: TileNode[],
435
+ getShownChildren: (node: TileNode) => TileNode[],
436
+ {
437
+ maxTiles,
438
+ maxZoom = Infinity,
439
+ maxStretch
440
+ }: {maxTiles: number; maxZoom?: number; maxStretch: number}
441
+ ): TileNode[] {
442
+ const limit = getStretchLimit(viewport, maxStretch);
443
+ if (limit === null) {
444
+ return tiles;
445
+ }
446
+ const edgeZoom = Math.min(EDGE_ZOOM, maxZoom);
447
+ const edgeTiles = new Map<string, boolean>();
448
+ const isStretched = ({x, y, z}: ProjectedTileIndex): boolean => {
449
+ if (z <= edgeZoom) {
450
+ return isTileStretched(viewport, {x, y, z}, limit);
451
+ }
452
+ const shift = 2 ** (z - edgeZoom);
453
+ const edge = {x: Math.floor(x / shift), y: Math.floor(y / shift), z: edgeZoom};
454
+ const key = `${edge.x}/${edge.y}`;
455
+ let stretched = edgeTiles.get(key);
456
+ if (stretched === undefined) {
457
+ stretched = isTileStretched(viewport, edge, limit);
458
+ edgeTiles.set(key, stretched);
459
+ }
460
+ return stretched;
461
+ };
462
+ const divided: TileNode[] = [];
463
+ const divide = (node: TileNode): void => {
464
+ if (!isStretched(node)) {
465
+ divided[divided.length] = node;
466
+ } else if (node.z < edgeZoom && divided.length <= maxTiles) {
467
+ for (const child of getShownChildren(node)) {
468
+ divide(child);
469
+ }
470
+ }
471
+ };
472
+ for (const tile of tiles) {
473
+ divide(tile);
474
+ }
475
+ return divided.length <= maxTiles ? divided : tiles.filter(tile => !isStretched(tile));
476
+ }
477
+
478
+ /**
479
+ * Selects the Web Mercator tiles that a projected view shows, of one zoom where the projection
480
+ * draws them without stretch. deck.gl selects tiles
481
+ * for a Web Mercator view and a globe from the view frustum, with tiles as boxes in Web Mercator
482
+ * space; in a view of another projection a tile is neither a box nor, far from the projection's
483
+ * area of use, a shape at all.
484
+ *
485
+ * The tile pyramid is walked from its root. A tile is kept if a point of the screen lies in it,
486
+ * or if the view draws its outline across the screen: the first finds the tiles larger than the
487
+ * gaps between the screen points, the second the smaller ones. Both read only the
488
+ * view's `project` and `unproject`, so the selection holds for any projection, bearing and pitch.
489
+ *
490
+ * A selected tile that the projection stretches (see `isTileStretched`) is divided into the tiles
491
+ * of the next zooms, down to `EDGE_ZOOM`, and the parts still stretched are left out: a
492
+ * view zoomed out past the area the projection can draw shows the tiles around that area, and
493
+ * not geometry drawn across the map. Tiles come nearest the screen center first.
494
+ */
495
+ export function getProjectedTileIndices(
496
+ viewport: ProjectedViewportLike,
497
+ {
498
+ z,
499
+ extent = null,
500
+ maxTiles = DEFAULT_MAX_PROJECTED_TILES,
501
+ maxZoom,
502
+ minZoom = 0,
503
+ maxStretch = DEFAULT_MAX_STRETCH
504
+ }: ProjectedTileSelectionOptions
505
+ ): ProjectedTileIndex[] {
506
+ if (!(viewport.width > 0) || !(viewport.height > 0) || !Number.isFinite(z)) {
507
+ return [];
508
+ }
509
+ const samples = getScreenSamples(viewport);
510
+ const root: TileNode = {x: 0, y: 0, z: 0, samples};
511
+ if (!samples.length && !isTileOnScreen(viewport, root)) {
512
+ return [];
513
+ }
514
+ /** The tiles of the next zoom in a tile that the view shows, in `extent` unless it is null. */
515
+ const getShownChildren = (node: TileNode, tileExtent = extent): TileNode[] => {
516
+ const children: TileNode[] = [];
517
+ const scale = 2 ** (node.z + 1);
518
+ for (let child = 0; child < 4; child++) {
519
+ const index = {x: node.x * 2 + (child & 1), y: node.y * 2 + (child >> 1), z: node.z + 1};
520
+ if (tileExtent && !isTileInExtent(index, tileExtent)) {
521
+ continue;
522
+ }
523
+ const childSamples = node.samples.filter(
524
+ sample =>
525
+ Math.min(scale - 1, Math.floor(sample.u * scale)) === index.x &&
526
+ Math.min(scale - 1, Math.floor(sample.v * scale)) === index.y
527
+ );
528
+ if (childSamples.length || isTileOnScreen(viewport, index)) {
529
+ children[children.length] = {...index, samples: childSamples};
530
+ }
531
+ }
532
+ return children;
533
+ };
534
+ let level: TileNode[] = [root];
535
+ for (let zoom = 0; zoom < z; zoom++) {
536
+ const next = level.flatMap(node => getShownChildren(node));
537
+ // The view shows tiles of this zoom, but none in the extent: the source has none to draw.
538
+ if (!next.length && extent && level.some(node => getShownChildren(node, null).length)) {
539
+ return [];
540
+ }
541
+ // A level the view shows no tile of, or too many: the tiles of the level before stand in, if
542
+ // the source serves its zoom.
543
+ if (!next.length || next.length > maxTiles) {
544
+ if (zoom < minZoom) {
545
+ return [];
546
+ }
547
+ break;
548
+ }
549
+ level = next;
550
+ }
551
+ level = divideStretchedTiles(viewport, level, getShownChildren, {
552
+ maxTiles,
553
+ maxZoom,
554
+ maxStretch
555
+ });
556
+ const centerLngLat = unprojectOrNull(viewport, [viewport.width / 2, viewport.height / 2]);
557
+ const center = centerLngLat ? toPyramid(centerLngLat) : null;
558
+ const indices = level.map(({x, y, z: tileZoom}) => ({x, y, z: tileZoom}));
559
+ if (center) {
560
+ const getDistance = (index: ProjectedTileIndex) =>
561
+ Math.hypot(
562
+ (index.x + 0.5) / 2 ** index.z - center.u,
563
+ (index.y + 0.5) / 2 ** index.z - center.v
564
+ );
565
+ indices.sort((a, b) => getDistance(a) - getDistance(b));
566
+ }
567
+ return indices;
568
+ }
569
+
570
+ /**
571
+ * The zoom of the tiles a projected view draws, as deck.gl's `TileLayer` chooses it in a Web
572
+ * Mercator view, from the zoom of the Web Mercator map of the same scale (see `getMercatorZoom`):
573
+ * `zoomOffset` is added before the zoom is rounded. Returns null below `minZoom` without an
574
+ * `extent`, where `TileLayer` draws no tiles.
575
+ */
576
+ export function getProjectedTileZoom(
577
+ viewport: ProjectedViewportLike,
578
+ {
579
+ tileSize = MERCATOR_WORLD_SIZE,
580
+ zoomOffset = 0,
581
+ minZoom,
582
+ maxZoom,
583
+ extent
584
+ }: {
585
+ tileSize?: number;
586
+ zoomOffset?: number;
587
+ minZoom?: number;
588
+ maxZoom?: number;
589
+ extent?: ArrayLike<number> | null;
590
+ }
591
+ ): number | null {
592
+ let z = Math.round(
593
+ getMercatorZoom(viewport) + Math.log2(MERCATOR_WORLD_SIZE / tileSize) + zoomOffset
594
+ );
595
+ if (typeof minZoom === 'number' && Number.isFinite(minZoom) && z < minZoom) {
596
+ if (!extent) {
597
+ return null;
598
+ }
599
+ z = minZoom;
600
+ }
601
+ if (typeof maxZoom === 'number' && Number.isFinite(maxZoom) && z > maxZoom) {
602
+ z = maxZoom;
603
+ }
604
+ return Math.max(0, z);
605
+ }