@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.
- 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,170 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {compileStyleFilter} from './style-expression';
|
|
6
|
+
import type {BasemapSource} from './style-resolver';
|
|
7
|
+
|
|
8
|
+
/** The parameters of {@link BasemapLayer.querySourceFeatures}, as MapLibre GL JS's. */
|
|
9
|
+
export type QuerySourceFeaturesOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* The source layer to read. Required for a `vector` source, where no features are returned
|
|
12
|
+
* without it, and ignored for a `geojson` source.
|
|
13
|
+
*/
|
|
14
|
+
sourceLayer?: string;
|
|
15
|
+
/** A style-spec filter expression that the features must match. */
|
|
16
|
+
filter?: unknown[] | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A feature returned by {@link BasemapLayer.querySourceFeatures}: a GeoJSON feature in longitude
|
|
21
|
+
* and latitude, as MapLibre GL JS returns one, with the source it was read from and the tile it was
|
|
22
|
+
* read in.
|
|
23
|
+
*/
|
|
24
|
+
export type BasemapSourceFeature = {
|
|
25
|
+
type: 'Feature';
|
|
26
|
+
/** The feature id: the tile feature's id, or the property that the source's `promoteId` names. */
|
|
27
|
+
id?: string | number;
|
|
28
|
+
/** The feature's geometry in the tile, in longitude and latitude. */
|
|
29
|
+
geometry: {type: string; coordinates: unknown};
|
|
30
|
+
/** The feature's properties. */
|
|
31
|
+
properties: Record<string, unknown>;
|
|
32
|
+
/** The id of the style source the feature was read from. */
|
|
33
|
+
source: string;
|
|
34
|
+
/** The source layer the feature was read from, for a `vector` source. */
|
|
35
|
+
sourceLayer?: string;
|
|
36
|
+
/** The index of the tile the feature was read from. */
|
|
37
|
+
tile: {x: number; y: number; z: number};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** The part of a loaded tile that the query reads. */
|
|
41
|
+
export type QueriedTile = {
|
|
42
|
+
index: {x: number; y: number; z: number};
|
|
43
|
+
isLoaded: boolean;
|
|
44
|
+
content: unknown;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The features of a vector or GeoJSON source in `tiles`, as MapLibre GL JS's
|
|
49
|
+
* `Map#querySourceFeatures` reads them from the tiles it renders: every feature of the source
|
|
50
|
+
* layer that matches the filter, whether a style layer draws it or not. A tile that is in `tiles`
|
|
51
|
+
* more than once, as for the copies of the world, is read once. A feature that crosses a tile edge,
|
|
52
|
+
* or a point in the buffer of two tiles, is returned once per tile, as in MapLibre.
|
|
53
|
+
*
|
|
54
|
+
* `["zoom"]` in the filter is the zoom of the tile.
|
|
55
|
+
*/
|
|
56
|
+
export function querySourceFeaturesInTiles(
|
|
57
|
+
tiles: readonly QueriedTile[],
|
|
58
|
+
sourceId: string,
|
|
59
|
+
source: BasemapSource,
|
|
60
|
+
options: QuerySourceFeaturesOptions = {}
|
|
61
|
+
): BasemapSourceFeature[] {
|
|
62
|
+
const isGeoJson = source.type === 'geojson';
|
|
63
|
+
const sourceLayer = isGeoJson ? undefined : options.sourceLayer;
|
|
64
|
+
if (!isGeoJson && !sourceLayer) {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const filter = options.filter ? compileStyleFilter(options.filter) : null;
|
|
68
|
+
|
|
69
|
+
const tilesByKey = new Map<string, {index: QueriedTile['index']; features: any[]}>();
|
|
70
|
+
for (const tile of tiles) {
|
|
71
|
+
if (!tile.isLoaded || !Array.isArray(tile.content)) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const {z, y} = tile.index;
|
|
75
|
+
const size = 2 ** z;
|
|
76
|
+
const x = ((tile.index.x % size) + size) % size;
|
|
77
|
+
const key = `${z}/${x}/${y}`;
|
|
78
|
+
tilesByKey.set(key, {index: {x, y, z}, features: tile.content});
|
|
79
|
+
}
|
|
80
|
+
const sortedTiles = [...tilesByKey.values()].sort(
|
|
81
|
+
(a, b) => a.index.z - b.index.z || a.index.y - b.index.y || a.index.x - b.index.x
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const result: BasemapSourceFeature[] = [];
|
|
85
|
+
for (const {index, features} of sortedTiles) {
|
|
86
|
+
const globals = {zoom: index.z};
|
|
87
|
+
for (const feature of features) {
|
|
88
|
+
if (!feature?.geometry) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (!isGeoJson && feature.properties?.layerName !== sourceLayer) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const properties: Record<string, unknown> = {...feature.properties};
|
|
95
|
+
if (!isGeoJson) {
|
|
96
|
+
// The decoder records the source layer of each feature in its properties.
|
|
97
|
+
delete properties.layerName;
|
|
98
|
+
}
|
|
99
|
+
if (filter && !filter(globals, {...feature, properties})) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const sourceFeature: BasemapSourceFeature = {
|
|
103
|
+
type: 'Feature',
|
|
104
|
+
geometry: getGeometryInLngLat(feature.geometry, index),
|
|
105
|
+
properties,
|
|
106
|
+
source: sourceId,
|
|
107
|
+
tile: {...index}
|
|
108
|
+
};
|
|
109
|
+
const id = isGeoJson ? feature.id : getVectorFeatureId(feature, properties, source, sourceLayer!);
|
|
110
|
+
if (id !== undefined && id !== null) {
|
|
111
|
+
sourceFeature.id = id;
|
|
112
|
+
}
|
|
113
|
+
if (sourceLayer) {
|
|
114
|
+
sourceFeature.sourceLayer = sourceLayer;
|
|
115
|
+
}
|
|
116
|
+
result.push(sourceFeature);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The id of a vector tile feature, as MapLibre GL JS gives it: the property that the source's
|
|
124
|
+
* `promoteId` names (a name, or a name per source layer), else the feature's own id. A boolean id
|
|
125
|
+
* is a number.
|
|
126
|
+
*/
|
|
127
|
+
function getVectorFeatureId(
|
|
128
|
+
feature: {id?: unknown},
|
|
129
|
+
properties: Record<string, unknown>,
|
|
130
|
+
source: BasemapSource,
|
|
131
|
+
sourceLayer: string
|
|
132
|
+
): string | number | undefined {
|
|
133
|
+
const {promoteId} = source;
|
|
134
|
+
let id: unknown = feature.id;
|
|
135
|
+
if (promoteId) {
|
|
136
|
+
const name =
|
|
137
|
+
typeof promoteId === 'string'
|
|
138
|
+
? promoteId
|
|
139
|
+
: (promoteId as Record<string, unknown>)[sourceLayer];
|
|
140
|
+
id = typeof name === 'string' ? properties[name] : undefined;
|
|
141
|
+
if (typeof id === 'boolean') {
|
|
142
|
+
id = Number(id);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return typeof id === 'string' || typeof id === 'number' ? id : undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Converts tile-local geometry (0 to 1 across the tile, y down) to longitude and latitude. */
|
|
149
|
+
function getGeometryInLngLat(
|
|
150
|
+
geometry: {type: string; coordinates?: unknown; geometries?: unknown[]},
|
|
151
|
+
index: {x: number; y: number; z: number}
|
|
152
|
+
): BasemapSourceFeature['geometry'] {
|
|
153
|
+
const size = 2 ** index.z;
|
|
154
|
+
const convert = (coordinates: any): unknown => {
|
|
155
|
+
if (typeof coordinates[0] === 'number') {
|
|
156
|
+
const [x, y, ...rest] = coordinates;
|
|
157
|
+
const lng = ((index.x + x) / size) * 360 - 180;
|
|
158
|
+
const lat = (360 / Math.PI) * Math.atan(Math.exp((1 - (2 * (index.y + y)) / size) * Math.PI)) - 90;
|
|
159
|
+
return [lng, lat, ...rest];
|
|
160
|
+
}
|
|
161
|
+
return coordinates.map(convert);
|
|
162
|
+
};
|
|
163
|
+
if (geometry.type === 'GeometryCollection') {
|
|
164
|
+
return {
|
|
165
|
+
...geometry,
|
|
166
|
+
geometries: (geometry.geometries || []).map(child => getGeometryInLngLat(child as any, index))
|
|
167
|
+
} as unknown as BasemapSourceFeature['geometry'];
|
|
168
|
+
}
|
|
169
|
+
return {type: geometry.type, coordinates: convert(geometry.coordinates)};
|
|
170
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {BasemapSource} from './style-resolver';
|
|
6
|
+
|
|
7
|
+
/** A tile index of the Web Mercator tile pyramid. */
|
|
8
|
+
export type SourceTileIndex = {x: number; y: number; z: number};
|
|
9
|
+
|
|
10
|
+
/** The `bounds` of a source, `[west, south, east, north]` in degrees. */
|
|
11
|
+
export type SourceTileBounds = [number, number, number, number];
|
|
12
|
+
|
|
13
|
+
/** The `{y}` placeholder of a tile URL template. */
|
|
14
|
+
const ROW_PLACEHOLDER = /\{y\}/g;
|
|
15
|
+
|
|
16
|
+
/** The flipped tile templates of `scheme: "tms"` sources, with the `tiles` they came from. */
|
|
17
|
+
const tmsTemplates = new WeakMap<BasemapSource, {tiles: string[]; templates: string[]}>();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The tile URL templates that a source's tiles load from. For `scheme: "tms"`, every `{y}` is
|
|
21
|
+
* replaced by `{-y}`, which deck.gl expands to the row counted from the south, `2^z - y - 1`, as
|
|
22
|
+
* MapLibre GL JS writes `{y}` for a TMS source. For any other scheme, the source's own `tiles`.
|
|
23
|
+
* The same source gives the same array while its `tiles` stay the same, so the tiles are not
|
|
24
|
+
* reloaded on each render.
|
|
25
|
+
*/
|
|
26
|
+
export function getSourceTileTemplates(source: BasemapSource): string[] | undefined {
|
|
27
|
+
const {tiles} = source;
|
|
28
|
+
if (source.scheme !== 'tms' || !Array.isArray(tiles)) {
|
|
29
|
+
return tiles;
|
|
30
|
+
}
|
|
31
|
+
let entry = tmsTemplates.get(source);
|
|
32
|
+
if (!entry || entry.tiles !== tiles) {
|
|
33
|
+
entry = {tiles, templates: tiles.map(tile => tile.replace(ROW_PLACEHOLDER, '{-y}'))};
|
|
34
|
+
tmsTemplates.set(source, entry);
|
|
35
|
+
}
|
|
36
|
+
return entry.templates;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The clamped bounds of sources, with the `bounds` they came from. */
|
|
40
|
+
const clampedBounds = new WeakMap<BasemapSource, {bounds: unknown; clamped: SourceTileBounds}>();
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A source's `bounds`, clamped to longitudes -180 to 180 and latitudes -90 to 90 as MapLibre GL JS
|
|
44
|
+
* clamps them, or null if the source has no bounds of four numbers. Bounds whose west is east of
|
|
45
|
+
* their east, across the antimeridian, contain no tiles past zoom 0, as in MapLibre GL JS. The
|
|
46
|
+
* same source gives the same array while its `bounds` stay the same.
|
|
47
|
+
*/
|
|
48
|
+
export function getSourceTileBounds(source: BasemapSource): SourceTileBounds | null {
|
|
49
|
+
const {bounds} = source;
|
|
50
|
+
if (
|
|
51
|
+
!Array.isArray(bounds) ||
|
|
52
|
+
bounds.length !== 4 ||
|
|
53
|
+
!bounds.every(value => typeof value === 'number' && !Number.isNaN(value))
|
|
54
|
+
) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
let entry = clampedBounds.get(source);
|
|
58
|
+
if (!entry || entry.bounds !== bounds) {
|
|
59
|
+
const [west, south, east, north] = bounds as number[];
|
|
60
|
+
entry = {
|
|
61
|
+
bounds,
|
|
62
|
+
clamped: [Math.max(-180, west), Math.max(-90, south), Math.min(180, east), Math.min(90, north)]
|
|
63
|
+
};
|
|
64
|
+
clampedBounds.set(source, entry);
|
|
65
|
+
}
|
|
66
|
+
return entry.clamped;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getMercatorX(longitude: number): number {
|
|
70
|
+
return (180 + longitude) / 360;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getMercatorY(latitude: number): number {
|
|
74
|
+
return (
|
|
75
|
+
(180 - (180 / Math.PI) * Math.log(Math.tan(Math.PI / 4 + (latitude * Math.PI) / 360))) / 360
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Whether a tile overlaps `bounds` (see `getSourceTileBounds`), as MapLibre GL JS's
|
|
81
|
+
* `TileBounds.contains` tests it: a tile that only touches the bounds at an edge is outside. The
|
|
82
|
+
* bounds hold in every copy of the world.
|
|
83
|
+
*/
|
|
84
|
+
export function isTileInBounds({x, y, z}: SourceTileIndex, bounds: SourceTileBounds): boolean {
|
|
85
|
+
const [west, south, east, north] = bounds;
|
|
86
|
+
const worldSize = 2 ** z;
|
|
87
|
+
const column = ((x % worldSize) + worldSize) % worldSize;
|
|
88
|
+
return (
|
|
89
|
+
column >= Math.floor(getMercatorX(west) * worldSize) &&
|
|
90
|
+
column < Math.ceil(getMercatorX(east) * worldSize) &&
|
|
91
|
+
y >= Math.floor(getMercatorY(north) * worldSize) &&
|
|
92
|
+
y < Math.ceil(getMercatorY(south) * worldSize)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The tile indices that overlap `bounds`, or all of them if there are no bounds. */
|
|
97
|
+
export function filterTileIndicesByBounds<IndexT extends SourceTileIndex>(
|
|
98
|
+
indices: IndexT[],
|
|
99
|
+
bounds: SourceTileBounds | null | undefined
|
|
100
|
+
): IndexT[] {
|
|
101
|
+
return bounds ? indices.filter(index => isTileInBounds(index, bounds)) : indices;
|
|
102
|
+
}
|
package/src/sprite.ts
CHANGED
|
@@ -3,7 +3,16 @@
|
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import {log} from '@deck.gl/core';
|
|
6
|
+
import {getBasemapResourceFetch} from './basemap-request';
|
|
7
|
+
import {isBasemapProtocolUrl} from './basemap-protocol';
|
|
8
|
+
import {getIconQuads, isValidStretchMetadata} from './icon-text-fit';
|
|
9
|
+
import type {ImageTextFit} from './icon-text-fit';
|
|
6
10
|
import type {BasemapLoadOptions} from './style-resolver';
|
|
11
|
+
import {
|
|
12
|
+
BasemapFetchError,
|
|
13
|
+
getBasemapLoadError,
|
|
14
|
+
type BasemapLoadErrorHandler
|
|
15
|
+
} from './basemap-error';
|
|
7
16
|
|
|
8
17
|
/** One image in a style sprite, as listed in the sprite's JSON index. */
|
|
9
18
|
export type SpriteImage = {
|
|
@@ -15,6 +24,23 @@ export type SpriteImage = {
|
|
|
15
24
|
pixelRatio?: number;
|
|
16
25
|
/** `true` for signed-distance-field images, which are tinted with `icon-color`. */
|
|
17
26
|
sdf?: boolean;
|
|
27
|
+
} & SpriteStretchMetadata;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* How `icon-text-fit` stretches an image, in image pixels, as MapLibre GL JS reads it from the
|
|
31
|
+
* sprite's JSON index (see `icon-text-fit.ts`).
|
|
32
|
+
*/
|
|
33
|
+
export type SpriteStretchMetadata = {
|
|
34
|
+
/** Ranges `[x1, x2]` of image columns that stretch. */
|
|
35
|
+
stretchX?: [number, number][];
|
|
36
|
+
/** Ranges `[y1, y2]` of image rows that stretch. */
|
|
37
|
+
stretchY?: [number, number][];
|
|
38
|
+
/** `[x1, y1, x2, y2]`: the part of the image that the label covers. */
|
|
39
|
+
content?: [number, number, number, number];
|
|
40
|
+
/** How the width may change to keep the aspect ratio of `content`. */
|
|
41
|
+
textFitWidth?: ImageTextFit;
|
|
42
|
+
/** How the height may change to keep the aspect ratio of `content`. */
|
|
43
|
+
textFitHeight?: ImageTextFit;
|
|
18
44
|
};
|
|
19
45
|
|
|
20
46
|
/** A deck.gl `IconLayer` mapping entry for one sprite image. */
|
|
@@ -27,7 +53,7 @@ export type SpriteIconMappingEntry = {
|
|
|
27
53
|
mask: boolean;
|
|
28
54
|
/** Device pixels per CSS pixel; `icon-size` scales `height / pixelRatio`. */
|
|
29
55
|
pixelRatio: number;
|
|
30
|
-
};
|
|
56
|
+
} & SpriteStretchMetadata;
|
|
31
57
|
|
|
32
58
|
/** A loaded sprite: the atlas image and its deck.gl icon mapping. */
|
|
33
59
|
export type SpriteAtlas = {
|
|
@@ -57,11 +83,16 @@ export type SpriteLoadOptions = {
|
|
|
57
83
|
baseUrl?: string;
|
|
58
84
|
/** Device pixel ratio. At 2 or more, the `@2x` sprite is requested first. Default 1. */
|
|
59
85
|
pixelRatio?: number;
|
|
60
|
-
|
|
86
|
+
/** Called for each sprite that fails to load, in place of the warning logged without it. */
|
|
87
|
+
onError?: BasemapLoadErrorHandler;
|
|
88
|
+
} & Pick<NonNullable<BasemapLoadOptions>, 'fetch' | 'fetchOptions' | 'transformRequest'>;
|
|
61
89
|
|
|
62
90
|
/** Resolves a style's `sprite` value into sprite sources with absolute URLs. */
|
|
63
91
|
export function getSpriteSources(sprite: unknown, baseUrl?: string): SpriteSource[] {
|
|
64
92
|
const resolve = (url: string) => {
|
|
93
|
+
if (isBasemapProtocolUrl(url)) {
|
|
94
|
+
return url;
|
|
95
|
+
}
|
|
65
96
|
try {
|
|
66
97
|
return new URL(url, baseUrl).toString();
|
|
67
98
|
} catch {
|
|
@@ -79,13 +110,25 @@ export function getSpriteSources(sprite: unknown, baseUrl?: string): SpriteSourc
|
|
|
79
110
|
return [];
|
|
80
111
|
}
|
|
81
112
|
|
|
82
|
-
|
|
113
|
+
const STRETCH_METADATA_KEYS = [
|
|
114
|
+
'stretchX',
|
|
115
|
+
'stretchY',
|
|
116
|
+
'content',
|
|
117
|
+
'textFitWidth',
|
|
118
|
+
'textFitHeight'
|
|
119
|
+
] as const;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Converts a sprite JSON index into a deck.gl `IconLayer` mapping. An image's stretch metadata is
|
|
123
|
+
* kept for `icon-text-fit`. As in MapLibre, an image whose stretch zones or content do not lie in
|
|
124
|
+
* order inside it is left out, with a warning.
|
|
125
|
+
*/
|
|
83
126
|
export function getSpriteIconMapping(
|
|
84
127
|
index: Record<string, SpriteImage>
|
|
85
128
|
): Record<string, SpriteIconMappingEntry> {
|
|
86
129
|
const mapping: Record<string, SpriteIconMappingEntry> = {};
|
|
87
130
|
for (const [name, image] of Object.entries(index || {})) {
|
|
88
|
-
|
|
131
|
+
const entry: SpriteIconMappingEntry = {
|
|
89
132
|
x: image.x,
|
|
90
133
|
y: image.y,
|
|
91
134
|
width: image.width,
|
|
@@ -93,13 +136,25 @@ export function getSpriteIconMapping(
|
|
|
93
136
|
mask: Boolean(image.sdf),
|
|
94
137
|
pixelRatio: image.pixelRatio || 1
|
|
95
138
|
};
|
|
139
|
+
for (const key of STRETCH_METADATA_KEYS) {
|
|
140
|
+
if (image[key] !== undefined) {
|
|
141
|
+
(entry as any)[key] = image[key];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (!isValidStretchMetadata(entry)) {
|
|
145
|
+
log.warn(`[BasemapLayer] Sprite image "${name}" has invalid stretch metadata; skipped`)();
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
mapping[name] = entry;
|
|
96
149
|
}
|
|
97
150
|
return mapping;
|
|
98
151
|
}
|
|
99
152
|
|
|
100
153
|
/**
|
|
101
154
|
* Decodes the atlas PNG once, so the per-tile icon layers share one image instead of each
|
|
102
|
-
* requesting it. Without `createImageBitmap` (e.g. in Node), returns the URL.
|
|
155
|
+
* requesting it. Without `createImageBitmap` (e.g. in Node), returns the URL. The image is decoded
|
|
156
|
+
* unpremultiplied, as deck.gl decodes the images it loads itself: the icon layers blend its
|
|
157
|
+
* colors with their alpha, so premultiplied colors would darken every translucent pixel twice.
|
|
103
158
|
*/
|
|
104
159
|
async function loadSpriteImage(
|
|
105
160
|
url: string,
|
|
@@ -111,9 +166,13 @@ async function loadSpriteImage(
|
|
|
111
166
|
}
|
|
112
167
|
const response = await fetchFn(url, fetchOptions);
|
|
113
168
|
if (!response.ok) {
|
|
114
|
-
throw new
|
|
169
|
+
throw new BasemapFetchError(
|
|
170
|
+
`Failed to load sprite image: ${url} (${response.status})`,
|
|
171
|
+
url,
|
|
172
|
+
response.status
|
|
173
|
+
);
|
|
115
174
|
}
|
|
116
|
-
return await createImageBitmap(await response.blob());
|
|
175
|
+
return await createImageBitmap(await response.blob(), {premultiplyAlpha: 'none'});
|
|
117
176
|
}
|
|
118
177
|
|
|
119
178
|
/** Appends a suffix and extension to a sprite URL's path, keeping its query string and hash. */
|
|
@@ -132,22 +191,26 @@ async function loadSpriteAtlas(
|
|
|
132
191
|
source: SpriteSource,
|
|
133
192
|
options: SpriteLoadOptions
|
|
134
193
|
): Promise<SpriteAtlas> {
|
|
135
|
-
const
|
|
194
|
+
const fetchJsonFn = getBasemapResourceFetch('SpriteJSON', 'json', options);
|
|
136
195
|
const suffixes = (options.pixelRatio ?? 1) > 1 ? ['@2x', ''] : [''];
|
|
137
196
|
let lastError: unknown;
|
|
138
197
|
for (const suffix of suffixes) {
|
|
139
198
|
const jsonUrl = getSpriteFileUrl(source.url, suffix, '.json');
|
|
140
199
|
try {
|
|
141
|
-
const response = await
|
|
200
|
+
const response = await fetchJsonFn(jsonUrl, options.fetchOptions);
|
|
142
201
|
if (!response.ok) {
|
|
143
|
-
throw new
|
|
202
|
+
throw new BasemapFetchError(
|
|
203
|
+
`Failed to load sprite: ${jsonUrl} (${response.status})`,
|
|
204
|
+
jsonUrl,
|
|
205
|
+
response.status
|
|
206
|
+
);
|
|
144
207
|
}
|
|
145
208
|
const index = (await response.json()) as Record<string, SpriteImage>;
|
|
146
209
|
return {
|
|
147
210
|
id: source.id,
|
|
148
211
|
image: await loadSpriteImage(
|
|
149
212
|
getSpriteFileUrl(source.url, suffix, '.png'),
|
|
150
|
-
|
|
213
|
+
getBasemapResourceFetch('SpriteImage', 'image', options),
|
|
151
214
|
options.fetchOptions
|
|
152
215
|
),
|
|
153
216
|
mapping: getSpriteIconMapping(index)
|
|
@@ -171,10 +234,14 @@ export async function loadSpriteAtlases(
|
|
|
171
234
|
const atlases = await Promise.all(
|
|
172
235
|
sources.map(source =>
|
|
173
236
|
loadSpriteAtlas(source, options).catch(error => {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
237
|
+
if (options.onError) {
|
|
238
|
+
options.onError(getBasemapLoadError('sprite', error, {url: source.url}));
|
|
239
|
+
} else {
|
|
240
|
+
log.warn(
|
|
241
|
+
`[BasemapLayer] Sprite ${source.url} failed to load; its icons are skipped`,
|
|
242
|
+
error
|
|
243
|
+
)();
|
|
244
|
+
}
|
|
178
245
|
return null;
|
|
179
246
|
})
|
|
180
247
|
)
|
|
@@ -233,6 +300,46 @@ export function resolveSpriteIcon(
|
|
|
233
300
|
return null;
|
|
234
301
|
}
|
|
235
302
|
|
|
303
|
+
/** Whether `icon-text-fit` cuts an image into parts: it has `stretchX` or `stretchY` zones. */
|
|
304
|
+
export function isStretchableIcon(entry: SpriteIconMappingEntry): boolean {
|
|
305
|
+
return Boolean(entry.stretchX?.length || entry.stretchY?.length);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The mapping name of part `index` of a stretchable image (see `getStretchIconMapping`). */
|
|
309
|
+
export function getIconPartName(name: string, index: number): string {
|
|
310
|
+
return `${name}\u0000${index}`;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const stretchIconMappings = new WeakMap<SpriteAtlas, Record<string, SpriteIconMappingEntry>>();
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* A sprite's mapping with an entry for each part that `icon-text-fit` cuts a stretchable image
|
|
317
|
+
* into (`getIconPartName`), besides each image's own. The parts are the same whatever the icon is
|
|
318
|
+
* fitted to: only their sizes on screen change.
|
|
319
|
+
*/
|
|
320
|
+
export function getStretchIconMapping(atlas: SpriteAtlas): Record<string, SpriteIconMappingEntry> {
|
|
321
|
+
let mapping = stretchIconMappings.get(atlas);
|
|
322
|
+
if (!mapping) {
|
|
323
|
+
mapping = {...atlas.mapping};
|
|
324
|
+
for (const [name, entry] of Object.entries(atlas.mapping)) {
|
|
325
|
+
if (isStretchableIcon(entry)) {
|
|
326
|
+
const quads = getIconQuads(entry, {left: 0, top: 0, right: 1, bottom: 1});
|
|
327
|
+
quads.forEach(({image}, index) => {
|
|
328
|
+
mapping![getIconPartName(name, index)] = {
|
|
329
|
+
...entry,
|
|
330
|
+
x: entry.x + image.x,
|
|
331
|
+
y: entry.y + image.y,
|
|
332
|
+
width: image.width,
|
|
333
|
+
height: image.height
|
|
334
|
+
};
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
stretchIconMappings.set(atlas, mapping);
|
|
339
|
+
}
|
|
340
|
+
return mapping;
|
|
341
|
+
}
|
|
342
|
+
|
|
236
343
|
const warnedMissingIcons = new Set<string>();
|
|
237
344
|
|
|
238
345
|
/** Warns once per icon name that a style references an image no loaded sprite contains. */
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {IconLayer} from '@deck.gl/layers';
|
|
6
|
+
import type {IconLayerProps} from '@deck.gl/layers';
|
|
7
|
+
import type {Accessor, DefaultProps} from '@deck.gl/core';
|
|
8
|
+
|
|
9
|
+
/** Props of {@link StretchIconLayer}. */
|
|
10
|
+
export type StretchIconLayerProps<DataT = unknown> = IconLayerProps<DataT> & {
|
|
11
|
+
/**
|
|
12
|
+
* How much wider (x) and taller (y) each icon is drawn than its mapping's aspect ratio at
|
|
13
|
+
* `getSize`, before it is turned by `getAngle`. Default `[1, 1]`.
|
|
14
|
+
*/
|
|
15
|
+
getIconStretch?: Accessor<DataT, [number, number]>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Where the icon shaders scale the unit quad by the icon's size. The stretch multiplies it there,
|
|
20
|
+
* so that the stretched quad turns with the icon. Each source must contain its pattern.
|
|
21
|
+
*/
|
|
22
|
+
const GLSL_PATCHES: [RegExp, string][] = [
|
|
23
|
+
[/(\nin vec2 instancePixelOffset;)/, '$1\nin vec2 instanceIconStretch;'],
|
|
24
|
+
[
|
|
25
|
+
/(vec2 pixelOffset = positions \/ 2\.0 \* iconSize)( \+ instanceOffsets;)/,
|
|
26
|
+
'$1 * instanceIconStretch$2'
|
|
27
|
+
]
|
|
28
|
+
];
|
|
29
|
+
const WGSL_PATCHES: [RegExp, string][] = [
|
|
30
|
+
[
|
|
31
|
+
/(@location\(9\) instancePixelOffset: vec2<f32>,)/,
|
|
32
|
+
'$1\n @location(11) instanceIconStretch: vec2<f32>,'
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
/(var pixelOffset = \w+(?:\.\w+)? \/ 2\.0 \* iconSize)( \+ inp\.instanceOffsets;)/,
|
|
36
|
+
'$1 * inp.instanceIconStretch$2'
|
|
37
|
+
]
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/** Applies `patches` to a shader source, throwing if one does not match: deck.gl changed it. */
|
|
41
|
+
export function patchIconShader(source: string, patches: [RegExp, string][]): string {
|
|
42
|
+
return patches.reduce((patched, [pattern, replacement]) => {
|
|
43
|
+
if (!pattern.test(patched)) {
|
|
44
|
+
throw new Error(`StretchIconLayer: the IconLayer shader no longer matches ${pattern}`);
|
|
45
|
+
}
|
|
46
|
+
return patched.replace(pattern, replacement);
|
|
47
|
+
}, source);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The shader patches of {@link StretchIconLayer}, for tests. */
|
|
51
|
+
export const STRETCH_ICON_SHADER_PATCHES = {glsl: GLSL_PATCHES, wgsl: WGSL_PATCHES};
|
|
52
|
+
|
|
53
|
+
const defaultProps: DefaultProps<StretchIconLayerProps> = {
|
|
54
|
+
getIconStretch: {type: 'accessor', value: [1, 1]}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* An `IconLayer` whose icons can be drawn at another aspect ratio than their image's
|
|
59
|
+
* (`getIconStretch`), as the parts of a stretched `icon-text-fit` icon are. WebGL and WebGPU.
|
|
60
|
+
*/
|
|
61
|
+
export class StretchIconLayer<DataT = unknown, ExtraProps extends {} = {}> extends IconLayer<
|
|
62
|
+
DataT,
|
|
63
|
+
Required<Pick<StretchIconLayerProps<DataT>, 'getIconStretch'>> & ExtraProps
|
|
64
|
+
> {
|
|
65
|
+
static layerName = 'StretchIconLayer';
|
|
66
|
+
static defaultProps = defaultProps;
|
|
67
|
+
|
|
68
|
+
getShaders() {
|
|
69
|
+
const shaders = super.getShaders();
|
|
70
|
+
return {
|
|
71
|
+
...shaders,
|
|
72
|
+
vs: patchIconShader(shaders.vs, GLSL_PATCHES),
|
|
73
|
+
...(shaders.source ? {source: patchIconShader(shaders.source, WGSL_PATCHES)} : {})
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
initializeState() {
|
|
78
|
+
super.initializeState();
|
|
79
|
+
this.getAttributeManager()!.addInstanced({
|
|
80
|
+
instanceIconStretch: {
|
|
81
|
+
size: 2,
|
|
82
|
+
accessor: 'getIconStretch',
|
|
83
|
+
defaultValue: [1, 1]
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|