@tangram.gl/basemap-layers 0.0.0 → 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.
- package/README.md +12 -7
- package/THIRD-PARTY-NOTICES.md +7 -5
- package/dist/geojson-task-worker.js +3166 -0
- package/dist/geojson-task-worker.js.map +6 -0
- package/dist/geojson-worker.js +1089 -7
- package/dist/geojson-worker.js.map +3 -3
- package/dist/index.cjs +9588 -2093
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +9599 -2090
- package/dist/index.js.map +3 -3
- package/dist/map-style.cjs +456 -20
- package/dist/map-style.cjs.map +3 -3
- package/dist/map-style.js +456 -19
- package/dist/map-style.js.map +2 -2
- package/dist/types/basemap-error.d.ts +52 -0
- package/dist/types/basemap-layer.d.ts +378 -25
- package/dist/types/basemap-protocol.d.ts +91 -0
- package/dist/types/basemap-request.d.ts +73 -0
- package/dist/types/circle-layer.d.ts +65 -8
- package/dist/types/color-relief-layer.d.ts +184 -0
- package/dist/types/dem-pixels.d.ts +40 -0
- package/dist/types/feature-state.d.ts +55 -0
- package/dist/types/fill-extrusion-lighting-extension.d.ts +1 -2
- package/dist/types/fill-outline-extension.d.ts +2 -3
- package/dist/types/fill-pattern.d.ts +35 -3
- package/dist/types/fill-style-extensions.d.ts +11 -59
- package/dist/types/geojson-resource.d.ts +18 -0
- package/dist/types/geojson-source.d.ts +13 -2
- package/dist/types/geojson-task-handler.d.ts +2 -0
- package/dist/types/geojson-task.d.ts +26 -0
- package/dist/types/geojson-tiler.d.ts +29 -2
- package/dist/types/geojson-worker.d.ts +5 -1
- package/dist/types/globe-horizon-extension.d.ts +2 -45
- package/dist/types/globe-layers.d.ts +141 -45
- package/dist/types/globe-line-angle-extension.d.ts +20 -17
- package/dist/types/glyph-store.d.ts +10 -1
- package/dist/types/heatmap-layer.d.ts +99 -0
- package/dist/types/hillshade-layer.d.ts +159 -27
- package/dist/types/icon-text-fit.d.ts +92 -0
- package/dist/types/image-source-layer.d.ts +142 -0
- package/dist/types/index.d.ts +25 -1
- package/dist/types/line-blur-extension.d.ts +3 -11
- package/dist/types/line-dash-extension.d.ts +53 -0
- package/dist/types/line-gradient-extension.d.ts +69 -0
- package/dist/types/line-metrics.d.ts +24 -0
- package/dist/types/line-pattern-extension.d.ts +50 -0
- package/dist/types/line-shape-extension.d.ts +76 -0
- package/dist/types/map-style-loader.d.ts +1 -1
- package/dist/types/map-style.d.ts +2 -2
- package/dist/types/mvt-label-layer.d.ts +111 -20
- package/dist/types/pmtiles-source.d.ts +38 -0
- package/dist/types/projected-background-layer.d.ts +63 -0
- package/dist/types/projected-bitmap-layer.d.ts +58 -0
- package/dist/types/projected-tile-data.d.ts +16 -0
- package/dist/types/projected-tile-selection.d.ts +72 -0
- package/dist/types/projected-tileset.d.ts +92 -0
- package/dist/types/projected-viewport.d.ts +76 -0
- package/dist/types/raster-color-extension.d.ts +4 -7
- package/dist/types/raster-fade.d.ts +155 -0
- package/dist/types/raster-tile-fingerprint.d.ts +11 -0
- package/dist/types/raster-tile-loader.d.ts +80 -0
- package/dist/types/shader-injection-plugin.d.ts +8 -0
- package/dist/types/source-features.d.ts +58 -0
- package/dist/types/source-tiles.d.ts +32 -0
- package/dist/types/sprite.d.ts +37 -3
- package/dist/types/stretch-icon-layer.d.ts +28 -0
- package/dist/types/style-accessor.d.ts +50 -12
- package/dist/types/style-expression.d.ts +22 -2
- package/dist/types/style-resolver.d.ts +47 -2
- package/dist/types/terrain-query.d.ts +39 -0
- package/dist/types/zoom-load-gate.d.ts +22 -0
- package/dist/types/zoom-paint-extension.d.ts +80 -11
- package/package.json +24 -21
- package/src/basemap-error.ts +82 -0
- package/src/basemap-layer.ts +1289 -101
- package/src/basemap-protocol.ts +353 -0
- package/src/basemap-request.ts +212 -0
- package/src/circle-layer.ts +430 -54
- package/src/color-relief-layer.ts +572 -0
- package/src/dem-pixels.ts +174 -0
- package/src/feature-state.ts +164 -0
- package/src/fill-extrusion-lighting-extension.ts +126 -9
- package/src/fill-outline-extension.ts +4 -4
- package/src/fill-pattern.ts +87 -9
- package/src/fill-style-extensions.ts +114 -44
- package/src/geojson-resource.ts +58 -0
- package/src/geojson-source.ts +106 -85
- package/src/geojson-task-handler.ts +11 -0
- package/src/geojson-task-worker.ts +8 -0
- package/src/geojson-task.ts +34 -0
- package/src/geojson-tiler.ts +270 -4
- package/src/geojson-worker.ts +36 -6
- package/src/globe-horizon-extension.ts +14 -4
- package/src/globe-layers.ts +1905 -406
- package/src/globe-line-angle-extension.ts +183 -23
- package/src/glyph-store.ts +28 -6
- package/src/heatmap-layer.ts +591 -0
- package/src/hillshade-layer.ts +309 -88
- package/src/icon-text-fit.ts +358 -0
- package/src/image-source-layer.ts +380 -0
- package/src/index.ts +58 -1
- package/src/line-blur-extension.ts +14 -12
- package/src/line-dash-extension.ts +303 -0
- package/src/line-gradient-extension.ts +191 -0
- package/src/line-metrics.ts +90 -0
- package/src/line-pattern-extension.ts +173 -0
- package/src/line-shape-extension.ts +272 -0
- package/src/map-style-loader.ts +5 -4
- package/src/map-style.ts +2 -1
- package/src/mvt-label-layer.ts +568 -144
- package/src/pmtiles-source.ts +276 -0
- package/src/projected-background-layer.ts +202 -0
- package/src/projected-bitmap-layer.ts +262 -0
- package/src/projected-tile-data.ts +132 -0
- package/src/projected-tile-selection.ts +605 -0
- package/src/projected-tileset.ts +183 -0
- package/src/projected-viewport.ts +254 -0
- package/src/raster-color-extension.ts +43 -18
- package/src/raster-fade.ts +319 -0
- package/src/raster-tile-fingerprint.ts +31 -0
- package/src/raster-tile-loader.ts +211 -0
- package/src/shader-injection-plugin.ts +25 -0
- package/src/source-features.ts +170 -0
- package/src/source-tiles.ts +102 -0
- package/src/sprite.ts +122 -15
- package/src/stretch-icon-layer.ts +87 -0
- package/src/style-accessor.ts +105 -17
- package/src/style-expression.ts +80 -3
- package/src/style-resolver.ts +200 -22
- package/src/terrain-query.ts +86 -0
- package/src/text-layout.ts +2 -3
- package/src/zoom-load-gate.ts +57 -0
- package/src/zoom-paint-extension.ts +245 -47
- package/dist/types/arabic-shaping.d.ts +0 -7
- package/dist/types/bidi.d.ts +0 -8
- package/dist/types/polylabel.d.ts +0 -17
- package/src/arabic-shaping.ts +0 -366
- package/src/bidi.ts +0 -199
- package/src/polylabel.ts +0 -312
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {BitmapLayer} from '@deck.gl/layers';
|
|
6
|
+
import {subdivideTriangleMesh} from '@math.gl/geometry/polygon';
|
|
7
|
+
import {
|
|
8
|
+
isProjectedViewport,
|
|
9
|
+
lngLatToMercatorWorld,
|
|
10
|
+
mercatorWorldToLngLat
|
|
11
|
+
} from './projected-viewport';
|
|
12
|
+
|
|
13
|
+
/** The cells along each side of the grid a tile's mesh starts from. */
|
|
14
|
+
const SEED_GRID_CELLS = 4;
|
|
15
|
+
/** The cells along each side of the grid of a tile whose mesh cannot be refined. */
|
|
16
|
+
const FALLBACK_GRID_CELLS = 16;
|
|
17
|
+
/** The share of a texel the drawn image may lie from its place. */
|
|
18
|
+
const TEXEL_TOLERANCE = 0.5;
|
|
19
|
+
/** The texels along a side of a tile image, to turn `TEXEL_TOLERANCE` into a distance. */
|
|
20
|
+
const DEFAULT_TILE_TEXELS = 512;
|
|
21
|
+
|
|
22
|
+
/** A mesh of a Web Mercator tile image, as `BitmapLayer` draws it. */
|
|
23
|
+
export type MercatorTileMesh = {
|
|
24
|
+
/** The count of indices. */
|
|
25
|
+
vertexCount: number;
|
|
26
|
+
/** Longitude, latitude and 0 of each vertex. */
|
|
27
|
+
positions: Float64Array;
|
|
28
|
+
/** The projected position of each vertex: what `BitmapLayer` draws. */
|
|
29
|
+
projectedPositions: Float64Array;
|
|
30
|
+
indices: Uint32Array;
|
|
31
|
+
/** The image coordinates of each vertex, with v down. */
|
|
32
|
+
texCoords: Float32Array;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function createGrid(cells: number): {positions: Float64Array; indices: Uint32Array} {
|
|
36
|
+
const side = cells + 1;
|
|
37
|
+
const positions = new Float64Array(side * side * 2);
|
|
38
|
+
const indices = new Uint32Array(cells * cells * 6);
|
|
39
|
+
let index = 0;
|
|
40
|
+
for (let i = 0; i < side; i++) {
|
|
41
|
+
for (let j = 0; j < side; j++) {
|
|
42
|
+
const vertex = i * side + j;
|
|
43
|
+
positions[vertex * 2] = i / cells;
|
|
44
|
+
positions[vertex * 2 + 1] = j / cells;
|
|
45
|
+
if (i > 0 && j > 0) {
|
|
46
|
+
indices[index++] = vertex - side;
|
|
47
|
+
indices[index++] = vertex - side - 1;
|
|
48
|
+
indices[index++] = vertex - 1;
|
|
49
|
+
indices[index++] = vertex - side;
|
|
50
|
+
indices[index++] = vertex - 1;
|
|
51
|
+
indices[index++] = vertex;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return {positions, indices};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Creates the mesh that draws a Web Mercator tile image in another projection. The image is
|
|
60
|
+
* uniform in Web Mercator space, so each vertex takes the longitude/latitude of its place in that
|
|
61
|
+
* space: a mesh uniform in latitude draws the image's rows at the wrong latitudes. The mesh is
|
|
62
|
+
* refined with math.gl's `subdivideTriangleMesh` until the sampled distance between the mesh and
|
|
63
|
+
* the projection is under half a texel. Where it cannot be refined, the tile is drawn with a
|
|
64
|
+
* fixed grid.
|
|
65
|
+
*
|
|
66
|
+
* `bounds` are the tile's `[west, south, east, north]`, and `project` converts longitude/latitude
|
|
67
|
+
* to the positions drawn. With `drawnBounds` (see `getNormalizedBounds`), only the part of the tile
|
|
68
|
+
* inside them has a mesh: outside the area of use of a projection, math.gl moves positions to its
|
|
69
|
+
* edge, and a mesh across that edge folds onto it. Its refinement follows the fold to its limit
|
|
70
|
+
* of triangles, and draws nothing more.
|
|
71
|
+
*/
|
|
72
|
+
export function createMercatorTileMesh(
|
|
73
|
+
bounds: ArrayLike<number>,
|
|
74
|
+
project: (lngLat: number[]) => ArrayLike<number>,
|
|
75
|
+
texels: number = DEFAULT_TILE_TEXELS,
|
|
76
|
+
drawnBounds?: ArrayLike<number> | null
|
|
77
|
+
): MercatorTileMesh {
|
|
78
|
+
const [x0, y0] = lngLatToMercatorWorld([bounds[0], bounds[1]]);
|
|
79
|
+
const [x1, y1] = lngLatToMercatorWorld([bounds[2], bounds[3]]);
|
|
80
|
+
const [u0, v0, u1, v1] = getDrawnPart(bounds, drawnBounds);
|
|
81
|
+
if (!(u1 > u0 && v1 > v0)) {
|
|
82
|
+
return EMPTY_MESH;
|
|
83
|
+
}
|
|
84
|
+
// `u` and `v` run over the part of the tile that is drawn, and `toImage` gives their place in
|
|
85
|
+
// the tile image.
|
|
86
|
+
const toImage = (u: number, v: number) => [u0 + u * (u1 - u0), v0 + v * (v1 - v0)];
|
|
87
|
+
const toLngLat = (u: number, v: number) => {
|
|
88
|
+
const [i, j] = toImage(u, v);
|
|
89
|
+
return mercatorWorldToLngLat([x0 + i * (x1 - x0), y0 + j * (y1 - y0)]);
|
|
90
|
+
};
|
|
91
|
+
const transform = (uv: ArrayLike<number>): number[] => {
|
|
92
|
+
const projected = project([...toLngLat(uv[0], uv[1]), 0]);
|
|
93
|
+
return [projected[0], projected[1], projected[2] ?? 0];
|
|
94
|
+
};
|
|
95
|
+
/** A vertex without a projected position is not a number: its triangles are not drawn. */
|
|
96
|
+
const transformOrSkip = (uv: ArrayLike<number>): number[] => {
|
|
97
|
+
try {
|
|
98
|
+
return transform(uv);
|
|
99
|
+
} catch {
|
|
100
|
+
return [Number.NaN, Number.NaN, 0];
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const a = transformOrSkip([0, 0]);
|
|
105
|
+
const b = transformOrSkip([1, 1]);
|
|
106
|
+
const c = transformOrSkip([0, 1]);
|
|
107
|
+
const d = transformOrSkip([1, 0]);
|
|
108
|
+
const diagonal = (Math.hypot(b[0] - a[0], b[1] - a[1]) + Math.hypot(d[0] - c[0], d[1] - c[1])) / 2;
|
|
109
|
+
const tolerance = (diagonal / (texels * Math.SQRT2)) * TEXEL_TOLERANCE;
|
|
110
|
+
|
|
111
|
+
let uv: ArrayLike<number> | null = null;
|
|
112
|
+
let projected: ArrayLike<number> | null = null;
|
|
113
|
+
let indices: Uint32Array | null = null;
|
|
114
|
+
if (Number.isFinite(tolerance) && tolerance > 0) {
|
|
115
|
+
try {
|
|
116
|
+
const refined = subdivideTriangleMesh(createGrid(SEED_GRID_CELLS), {
|
|
117
|
+
size: 2,
|
|
118
|
+
targetSize: 3,
|
|
119
|
+
tolerance,
|
|
120
|
+
transform
|
|
121
|
+
});
|
|
122
|
+
uv = refined.sourcePositions;
|
|
123
|
+
projected = refined.positions;
|
|
124
|
+
indices = refined.indices;
|
|
125
|
+
} catch {
|
|
126
|
+
// The projection has no smooth form over this tile: draw the fixed grid.
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (!uv || !projected || !indices) {
|
|
130
|
+
const grid = createGrid(FALLBACK_GRID_CELLS);
|
|
131
|
+
const positions = new Float64Array((grid.positions.length / 2) * 3);
|
|
132
|
+
for (let vertex = 0; vertex < grid.positions.length / 2; vertex++) {
|
|
133
|
+
positions.set(transformOrSkip(grid.positions.subarray(vertex * 2, vertex * 2 + 2)), vertex * 3);
|
|
134
|
+
}
|
|
135
|
+
uv = grid.positions;
|
|
136
|
+
projected = positions;
|
|
137
|
+
indices = grid.indices;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const vertexCount = uv.length / 2;
|
|
141
|
+
const positions = new Float64Array(vertexCount * 3);
|
|
142
|
+
const texCoords = new Float32Array(vertexCount * 2);
|
|
143
|
+
for (let vertex = 0; vertex < vertexCount; vertex++) {
|
|
144
|
+
const u = uv[vertex * 2];
|
|
145
|
+
const v = uv[vertex * 2 + 1];
|
|
146
|
+
positions.set(toLngLat(u, v), vertex * 3);
|
|
147
|
+
const [i, j] = toImage(u, v);
|
|
148
|
+
texCoords[vertex * 2] = i;
|
|
149
|
+
texCoords[vertex * 2 + 1] = 1 - j;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
vertexCount: indices.length,
|
|
153
|
+
positions,
|
|
154
|
+
projectedPositions: Float64Array.from(projected),
|
|
155
|
+
indices,
|
|
156
|
+
texCoords
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const EMPTY_MESH: MercatorTileMesh = {
|
|
161
|
+
vertexCount: 0,
|
|
162
|
+
positions: new Float64Array(0),
|
|
163
|
+
projectedPositions: new Float64Array(0),
|
|
164
|
+
indices: new Uint32Array(0),
|
|
165
|
+
texCoords: new Float32Array(0)
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The part of a tile inside `drawnBounds`, `[west, south, east, north]` in degrees (see
|
|
170
|
+
* `getNormalizedBounds`), as `[u0, v0, u1, v1]` in the tile image, with v up. Empty where the
|
|
171
|
+
* bounds have no area, and the whole tile without bounds.
|
|
172
|
+
*/
|
|
173
|
+
function getDrawnPart(
|
|
174
|
+
bounds: ArrayLike<number>,
|
|
175
|
+
drawnBounds?: ArrayLike<number> | null
|
|
176
|
+
): [number, number, number, number] {
|
|
177
|
+
if (!drawnBounds) {
|
|
178
|
+
return [0, 0, 1, 1];
|
|
179
|
+
}
|
|
180
|
+
const [x0, y0] = lngLatToMercatorWorld([bounds[0], bounds[1]]);
|
|
181
|
+
const [x1, y1] = lngLatToMercatorWorld([bounds[2], bounds[3]]);
|
|
182
|
+
const [ax0, ay0] = lngLatToMercatorWorld([drawnBounds[0], drawnBounds[1]]);
|
|
183
|
+
const [ax1, ay1] = lngLatToMercatorWorld([drawnBounds[2], drawnBounds[3]]);
|
|
184
|
+
const clamp = (value: number) => Math.max(0, Math.min(1, value));
|
|
185
|
+
return [
|
|
186
|
+
clamp((ax0 - x0) / (x1 - x0)),
|
|
187
|
+
clamp((ay0 - y0) / (y1 - y0)),
|
|
188
|
+
clamp((ax1 - x0) / (x1 - x0)),
|
|
189
|
+
clamp((ay1 - y0) / (y1 - y0))
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The bounds, `[west, south, east, north]`, that a projection's normalization moves a tile to, or
|
|
195
|
+
* null. math.gl's normalization moves a position outside the area of use of an EPSG code, such as
|
|
196
|
+
* a UTM zone, to the nearest position on its edge: there it clamps longitude and latitude to a box.
|
|
197
|
+
* Returns that box clipped to the tile, if the normalization moves the four corners of the tile as
|
|
198
|
+
* such a clamp does, and null for another normalization, which then does not clip the tile. A
|
|
199
|
+
* tile outside the box gets bounds without area, on the edge of the box.
|
|
200
|
+
*/
|
|
201
|
+
export function getNormalizedBounds(
|
|
202
|
+
bounds: ArrayLike<number>,
|
|
203
|
+
normalize: (position: number[]) => ArrayLike<number>
|
|
204
|
+
): [number, number, number, number] | null {
|
|
205
|
+
// The normalization wraps longitudes: both edges of a tile around the world are one meridian.
|
|
206
|
+
if (!(bounds[2] - bounds[0] < 360)) {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
let corners: ArrayLike<number>[];
|
|
210
|
+
try {
|
|
211
|
+
corners = [
|
|
212
|
+
[bounds[0], bounds[1]],
|
|
213
|
+
[bounds[2], bounds[1]],
|
|
214
|
+
[bounds[2], bounds[3]],
|
|
215
|
+
[bounds[0], bounds[3]]
|
|
216
|
+
].map(([longitude, latitude]) => normalize([longitude, latitude, 0]));
|
|
217
|
+
} catch {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
const [west, south, east, north] = [corners[0][0], corners[0][1], corners[2][0], corners[2][1]];
|
|
221
|
+
const isClamp =
|
|
222
|
+
[west, south, east, north].every(Number.isFinite) &&
|
|
223
|
+
west <= east &&
|
|
224
|
+
south <= north &&
|
|
225
|
+
corners[1][0] === east &&
|
|
226
|
+
corners[1][1] === south &&
|
|
227
|
+
corners[3][0] === west &&
|
|
228
|
+
corners[3][1] === north;
|
|
229
|
+
return isClamp ? [west, south, east, north] : null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function isRectangularBounds(bounds: unknown): bounds is number[] {
|
|
233
|
+
return Array.isArray(bounds) && bounds.length === 4 && bounds.every(Number.isFinite);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* A `BitmapLayer` for Web Mercator tile images that also draws in a projected view (deck.gl's
|
|
238
|
+
* `_CustomProjectionView`), with the mesh of `createMercatorTileMesh`. `BitmapLayer` itself draws
|
|
239
|
+
* a mesh uniform in longitude and latitude there, and its `_imageCoordinateSystem` has no effect.
|
|
240
|
+
* In other views it draws as `BitmapLayer` does.
|
|
241
|
+
*/
|
|
242
|
+
export class ProjectedBitmapLayer<ExtraPropsT extends {} = {}> extends BitmapLayer<ExtraPropsT> {
|
|
243
|
+
static layerName = 'ProjectedBitmapLayer';
|
|
244
|
+
|
|
245
|
+
protected _createMesh() {
|
|
246
|
+
const {bounds} = this.props;
|
|
247
|
+
const transform = this.usePositionTransforms().transform;
|
|
248
|
+
if (!transform || !isProjectedViewport(this.context.viewport) || !isRectangularBounds(bounds)) {
|
|
249
|
+
return super._createMesh();
|
|
250
|
+
}
|
|
251
|
+
const {crs} = this.context.viewport as {
|
|
252
|
+
crs?: {converter?: {WGS84?: {normalize?: (position: number[]) => number[]}}};
|
|
253
|
+
};
|
|
254
|
+
const normalize = crs?.converter?.WGS84?.normalize;
|
|
255
|
+
return createMercatorTileMesh(
|
|
256
|
+
bounds,
|
|
257
|
+
lngLat => transform.call(this, lngLat),
|
|
258
|
+
DEFAULT_TILE_TEXELS,
|
|
259
|
+
normalize ? getNormalizedBounds(bounds, normalize) : null
|
|
260
|
+
) as ReturnType<BitmapLayer['_createMesh']>;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {MERCATOR_WORLD_SIZE, mercatorWorldToLngLat} from './projected-viewport';
|
|
6
|
+
|
|
7
|
+
type TileIndex = {x: number; y: number; z: number};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The longest step between two vertices of a line or ring, as a share of the tile's side. A
|
|
11
|
+
* straight edge in a tile is straight in Web Mercator space and curved in another projection:
|
|
12
|
+
* longer steps, such as the edges of a polygon cut at the tile's sides, get more vertices, so
|
|
13
|
+
* that the view projects the curve and not its chord.
|
|
14
|
+
*/
|
|
15
|
+
const MAX_STEP = 1 / 16;
|
|
16
|
+
/**
|
|
17
|
+
* The range of tile-local coordinates that a step is measured in: the tile and a buffer of its
|
|
18
|
+
* own size on each side. Tiles come from servers the application does not control, and a corrupt
|
|
19
|
+
* coordinate far outside the tile must not ask for millions of vertices.
|
|
20
|
+
*/
|
|
21
|
+
const MIN_COORDINATE = -1;
|
|
22
|
+
const MAX_COORDINATE = 2;
|
|
23
|
+
/** The most parts one step is divided into, whatever its length. */
|
|
24
|
+
const MAX_STEP_PARTS = 64;
|
|
25
|
+
/** The most vertices added to the features of one tile. Past it, steps are left whole. */
|
|
26
|
+
export const MAX_ADDED_VERTICES = 65536;
|
|
27
|
+
|
|
28
|
+
/** The count of vertices that the features of one tile can still get. */
|
|
29
|
+
type VertexBudget = {remaining: number};
|
|
30
|
+
|
|
31
|
+
const clampCoordinate = (value: number) =>
|
|
32
|
+
Math.max(MIN_COORDINATE, Math.min(MAX_COORDINATE, value));
|
|
33
|
+
|
|
34
|
+
/** The count of parts a step between two tile-local positions is divided into: 1 or more. */
|
|
35
|
+
function getStepParts(from: number[], to: number[]): number {
|
|
36
|
+
const length = Math.max(
|
|
37
|
+
Math.abs(clampCoordinate(to[0]) - clampCoordinate(from[0])),
|
|
38
|
+
Math.abs(clampCoordinate(to[1]) - clampCoordinate(from[1]))
|
|
39
|
+
);
|
|
40
|
+
const parts = Math.min(MAX_STEP_PARTS, Math.ceil(length / MAX_STEP));
|
|
41
|
+
// A coordinate that is not a number has no length.
|
|
42
|
+
return parts >= 1 ? parts : 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A line or ring of tile-local positions, with vertices added along its long steps. */
|
|
46
|
+
function addVertices(line: number[][], budget: VertexBudget): number[][] {
|
|
47
|
+
const result: number[][] = [];
|
|
48
|
+
line.forEach((position, index) => {
|
|
49
|
+
const previous = line[index - 1];
|
|
50
|
+
if (previous) {
|
|
51
|
+
const parts = getStepParts(previous, position);
|
|
52
|
+
// A step that the budget does not cover is left whole.
|
|
53
|
+
if (parts - 1 <= budget.remaining) {
|
|
54
|
+
budget.remaining -= parts - 1;
|
|
55
|
+
for (let part = 1; part < parts; part++) {
|
|
56
|
+
const t = part / parts;
|
|
57
|
+
result[result.length] = previous.map(
|
|
58
|
+
(value, axis) => value + (position[axis] - value) * t
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
result[result.length] = position;
|
|
64
|
+
});
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The coordinates of a geometry, with vertices added along the long steps of lines and rings. */
|
|
69
|
+
function addGeometryVertices(type: string, coordinates: any, budget: VertexBudget): any {
|
|
70
|
+
switch (type) {
|
|
71
|
+
case 'LineString':
|
|
72
|
+
return addVertices(coordinates, budget);
|
|
73
|
+
case 'MultiLineString':
|
|
74
|
+
case 'Polygon':
|
|
75
|
+
return coordinates.map((line: number[][]) => addVertices(line, budget));
|
|
76
|
+
case 'MultiPolygon':
|
|
77
|
+
return coordinates.map((polygon: number[][][]) =>
|
|
78
|
+
polygon.map(ring => addVertices(ring, budget))
|
|
79
|
+
);
|
|
80
|
+
default:
|
|
81
|
+
return coordinates;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const geographicTiles = new WeakMap<object, Map<string, any[]>>();
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The features of a vector tile with longitude/latitude geometry, for a projected view, from the
|
|
89
|
+
* tile-local features a shared tileset holds. Lines and rings get vertices along their long
|
|
90
|
+
* steps (see `MAX_STEP`). The tileset's features are not changed: views of other kinds draw them
|
|
91
|
+
* too. The result is kept per feature array and tile, so that a tile's sublayers get the same
|
|
92
|
+
* data at each update.
|
|
93
|
+
*/
|
|
94
|
+
export function getTileFeaturesInLngLat(features: any[], index: TileIndex): any[] {
|
|
95
|
+
const key = `${index.x}/${index.y}/${index.z}`;
|
|
96
|
+
let cache = geographicTiles.get(features);
|
|
97
|
+
if (!cache) {
|
|
98
|
+
cache = new Map();
|
|
99
|
+
geographicTiles.set(features, cache);
|
|
100
|
+
}
|
|
101
|
+
let converted = cache.get(key);
|
|
102
|
+
if (!converted) {
|
|
103
|
+
const scale = 2 ** index.z;
|
|
104
|
+
const budget: VertexBudget = {remaining: MAX_ADDED_VERTICES};
|
|
105
|
+
const convertCoordinates = (coordinates: any): any => {
|
|
106
|
+
if (typeof coordinates[0] === 'number') {
|
|
107
|
+
const [x, y, ...rest] = coordinates;
|
|
108
|
+
return [
|
|
109
|
+
...mercatorWorldToLngLat([
|
|
110
|
+
(MERCATOR_WORLD_SIZE * (index.x + x)) / scale,
|
|
111
|
+
MERCATOR_WORLD_SIZE * (1 - (index.y + y) / scale)
|
|
112
|
+
]),
|
|
113
|
+
...rest
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
return coordinates.map(convertCoordinates);
|
|
117
|
+
};
|
|
118
|
+
const convertGeometry = (geometry: any): any =>
|
|
119
|
+
geometry &&
|
|
120
|
+
(geometry.type === 'GeometryCollection'
|
|
121
|
+
? {...geometry, geometries: geometry.geometries.map(convertGeometry)}
|
|
122
|
+
: {
|
|
123
|
+
...geometry,
|
|
124
|
+
coordinates: convertCoordinates(
|
|
125
|
+
addGeometryVertices(geometry.type, geometry.coordinates, budget)
|
|
126
|
+
)
|
|
127
|
+
});
|
|
128
|
+
converted = features.map(feature => ({...feature, geometry: convertGeometry(feature.geometry)}));
|
|
129
|
+
cache.set(key, converted);
|
|
130
|
+
}
|
|
131
|
+
return converted;
|
|
132
|
+
}
|