@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.
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,183 @@
1
+ // tangram-layers
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {sharedTile2DDeckAdapter} from '@deck.gl-community/layers/geo-layers';
6
+ import type {Viewport} from '@deck.gl/core';
7
+ import {TileLayer, Tileset2D} from '@deck.gl/geo-layers';
8
+ import {
9
+ DEFAULT_MAX_STRETCH,
10
+ getProjectedTileIndices,
11
+ getProjectedTileZoom
12
+ } from './projected-tile-selection';
13
+ import {isProjectedViewport} from './projected-viewport';
14
+ import {type SourceTileBounds, filterTileIndicesByBounds} from './source-tiles';
15
+
16
+ type DeckAdapter = typeof sharedTile2DDeckAdapter;
17
+ type TraversalContext = Parameters<DeckAdapter['getTileIndices']>[0];
18
+ type TileContext = Parameters<DeckAdapter['getTileBoundingBox']>[0];
19
+ type TileIndex = ReturnType<DeckAdapter['getTileIndices']>[number];
20
+
21
+ function createBasemapTilesetAdapter(maxStretch: number, tileBounds: SourceTileBounds | null) {
22
+ /** The tiles that cover the view, before the source's `bounds` are applied. */
23
+ function getViewTileIndices(
24
+ context: Omit<TraversalContext, 'viewState'> & {viewState: unknown}
25
+ ): TileIndex[] {
26
+ const viewport = context.viewState as Viewport;
27
+ if (!isProjectedViewport(viewport)) {
28
+ return sharedTile2DDeckAdapter.getTileIndices({...context, viewState: viewport});
29
+ }
30
+ const z = getProjectedTileZoom(viewport, context);
31
+ return z === null
32
+ ? []
33
+ : getProjectedTileIndices(viewport, {
34
+ z,
35
+ extent: context.extent,
36
+ minZoom: context.minZoom,
37
+ maxZoom: context.maxZoom,
38
+ maxStretch
39
+ });
40
+ }
41
+
42
+ return {
43
+ getTileIndices(
44
+ context: Omit<TraversalContext, 'viewState'> & {viewState: unknown}
45
+ ): TileIndex[] {
46
+ return filterTileIndicesByBounds(getViewTileIndices(context), tileBounds);
47
+ },
48
+ getTileBoundingBox(
49
+ context: Omit<TileContext, 'viewState'> & {viewState: unknown},
50
+ index: TileIndex
51
+ ) {
52
+ return sharedTile2DDeckAdapter.getTileBoundingBox(
53
+ {...context, viewState: context.viewState as Viewport},
54
+ index
55
+ );
56
+ }
57
+ };
58
+ }
59
+
60
+ type BasemapTilesetAdapter = ReturnType<typeof createBasemapTilesetAdapter>;
61
+
62
+ /**
63
+ * The adapters of each stretch limit: one without bounds, and one for each bounds array. The
64
+ * bounds arrays are held weakly, so an adapter is released with its source's bounds.
65
+ */
66
+ const adapters = new Map<
67
+ number,
68
+ {unbounded: BasemapTilesetAdapter; bounded: WeakMap<SourceTileBounds, BasemapTilesetAdapter>}
69
+ >();
70
+
71
+ /**
72
+ * The tile traversal of the basemap's shared tilesets: deck.gl's in a Web Mercator view and on a
73
+ * globe, and `getProjectedTileIndices` with `maxStretch` (see `ProjectedTileSelectionOptions`) in
74
+ * a projected view. A tileset can serve views of both kinds at once, so the view is tested at
75
+ * each traversal. With `tileBounds`, a source's `bounds` (see `getSourceTileBounds`), the tiles
76
+ * outside them are not selected, so they are never requested. The same limit and the same bounds
77
+ * array give the same adapter. The cache holds a bounds array weakly: pass the array that
78
+ * `getSourceTileBounds` returns, which stays the same while the source's `bounds` do.
79
+ */
80
+ export function getBasemapTilesetAdapter(
81
+ maxStretch = DEFAULT_MAX_STRETCH,
82
+ tileBounds: SourceTileBounds | null = null
83
+ ): BasemapTilesetAdapter {
84
+ let entry = adapters.get(maxStretch);
85
+ if (!entry) {
86
+ entry = {unbounded: createBasemapTilesetAdapter(maxStretch, null), bounded: new WeakMap()};
87
+ adapters.set(maxStretch, entry);
88
+ }
89
+ if (!tileBounds) {
90
+ return entry.unbounded;
91
+ }
92
+ let adapter = entry.bounded.get(tileBounds);
93
+ if (!adapter) {
94
+ adapter = createBasemapTilesetAdapter(maxStretch, tileBounds);
95
+ entry.bounded.set(tileBounds, adapter);
96
+ }
97
+ return adapter;
98
+ }
99
+
100
+ /** `getBasemapTilesetAdapter` with the default stretch limit. */
101
+ export const basemapTilesetAdapter = getBasemapTilesetAdapter();
102
+
103
+ /**
104
+ * A deck.gl `Tileset2D` that also selects Web Mercator tiles in a projected view. Pass it to a
105
+ * `TileLayer` as `TilesetClass`. In other views it selects as `Tileset2D` does. With a
106
+ * `tileBounds` option (see `getSourceTileBounds`), the tiles outside the bounds are not selected.
107
+ */
108
+ export class ProjectedTileset2D<DataT = any, ViewStateT = Viewport> extends Tileset2D<
109
+ DataT,
110
+ ViewStateT
111
+ > {
112
+ private selectionViewport: Viewport | null = null;
113
+
114
+ getTileIndices(options: Parameters<Tileset2D<DataT, ViewStateT>['getTileIndices']>[0]) {
115
+ const {tileBounds} = this.opts as {tileBounds?: SourceTileBounds | null};
116
+ return filterTileIndicesByBounds(this.getViewTileIndices(options), tileBounds);
117
+ }
118
+
119
+ /** The tiles that cover the view, before the `tileBounds` are applied. */
120
+ private getViewTileIndices(
121
+ options: Parameters<Tileset2D<DataT, ViewStateT>['getTileIndices']>[0]
122
+ ) {
123
+ const {viewport} = options;
124
+ this.selectionViewport = viewport || null;
125
+ if (!viewport || !isProjectedViewport(viewport)) {
126
+ return super.getTileIndices(options);
127
+ }
128
+ const {tileSize, zoomOffset, extent} = this.opts;
129
+ const {maxStretch} = this.opts as {maxStretch?: number};
130
+ const z = getProjectedTileZoom(viewport, {
131
+ tileSize,
132
+ zoomOffset,
133
+ minZoom: options.minZoom,
134
+ maxZoom: options.maxZoom,
135
+ extent
136
+ });
137
+ return z === null
138
+ ? []
139
+ : getProjectedTileIndices(viewport, {
140
+ z,
141
+ extent,
142
+ minZoom: options.minZoom,
143
+ maxZoom: options.maxZoom,
144
+ maxStretch
145
+ });
146
+ }
147
+
148
+ /**
149
+ * `Tileset2D` culls tiles outside the longitude/latitude box of the screen corners. In a
150
+ * projected view a corner can lie outside the projection, without a longitude and latitude, and
151
+ * the selection has tested each tile against the screen already.
152
+ */
153
+ isTileVisible(...parameters: Parameters<Tileset2D<DataT, ViewStateT>['isTileVisible']>): boolean {
154
+ if (this.selectionViewport && isProjectedViewport(this.selectionViewport)) {
155
+ return super.isTileVisible(parameters[0]);
156
+ }
157
+ return super.isTileVisible(...parameters);
158
+ }
159
+ }
160
+
161
+ /**
162
+ * A deck.gl `TileLayer` that hands its `maxStretch` (see `ProjectedTileSelectionOptions`) and its
163
+ * `tileBounds` (see `getSourceTileBounds`) to a `ProjectedTileset2D`, its `TilesetClass`.
164
+ */
165
+ export class ProjectedTileLayer<DataT = any, ExtraPropsT extends {} = {}> extends TileLayer<
166
+ DataT,
167
+ ExtraPropsT & {maxStretch?: number; tileBounds?: SourceTileBounds | null}
168
+ > {
169
+ static layerName = 'ProjectedTileLayer';
170
+ static defaultProps = {
171
+ TilesetClass: ProjectedTileset2D,
172
+ maxStretch: DEFAULT_MAX_STRETCH,
173
+ tileBounds: null
174
+ };
175
+
176
+ _getTilesetOptions() {
177
+ return {
178
+ ...super._getTilesetOptions(),
179
+ maxStretch: this.props.maxStretch,
180
+ tileBounds: this.props.tileBounds
181
+ };
182
+ }
183
+ }
@@ -0,0 +1,254 @@
1
+ // tangram-layers
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {isGlobeViewport} from './is-globe-viewport';
6
+
7
+ /** deck.gl's `PROJECTION_MODE.CUSTOM_GEOSPATIAL`: the mode of a `_CustomProjectionViewport`. */
8
+ const PROJECTION_MODE_CUSTOM_GEOSPATIAL = 3;
9
+
10
+ /** The size of the Web Mercator world, in deck.gl common units. */
11
+ export const MERCATOR_WORLD_SIZE = 512;
12
+
13
+ /** The latitude of the top edge of the Web Mercator tile pyramid. */
14
+ export const MAX_MERCATOR_LATITUDE = 85.051129;
15
+
16
+ /** The pixels between the two samples that measure the scale of a view. */
17
+ const SCALE_SAMPLE_PIXELS = 16;
18
+
19
+ /**
20
+ * What the projected draw mode reads of a deck.gl viewport. `project` and `unproject` convert
21
+ * between longitude/latitude and top-left pixels, through the view's projection. `project` also
22
+ * returns the depth of a position given with an elevation: 0 to 1 in front of the camera.
23
+ */
24
+ export type ProjectedViewportLike = {
25
+ width: number;
26
+ height: number;
27
+ zoom: number;
28
+ project: (lngLat: number[]) => number[];
29
+ unproject: (pixel: number[]) => number[];
30
+ /**
31
+ * Converts longitude/latitude to the view's map plane, before the camera. With it, the tile
32
+ * selection finds the tiles that the projection stretches (see `getProjectedTileIndices`).
33
+ */
34
+ projectPosition?: (lngLat: number[]) => number[];
35
+ /**
36
+ * The projection of the view: deck.gl's descriptor of its coordinate reference system. Views of
37
+ * one projection have the same object, so what the tile selection measures of the projection,
38
+ * and not of the camera, is kept by it.
39
+ */
40
+ crs?: object;
41
+ };
42
+
43
+ /**
44
+ * Whether a viewport draws longitude/latitude positions through a projected coordinate reference
45
+ * system (deck.gl's `_CustomProjectionView`), including across copies of deck.gl.
46
+ */
47
+ export function isProjectedViewport(viewport: unknown): boolean {
48
+ if (!viewport || typeof viewport !== 'object') {
49
+ return false;
50
+ }
51
+ const {projectionMode} = viewport as {projectionMode?: unknown};
52
+ return projectionMode === PROJECTION_MODE_CUSTOM_GEOSPATIAL;
53
+ }
54
+
55
+ /**
56
+ * Whether tile geometry is drawn from longitude/latitude positions in a viewport, and not from
57
+ * tile-local positions: on a globe and in a projected view. Web Mercator tiles are squares only
58
+ * in a Web Mercator view.
59
+ */
60
+ export function isGeographicViewport(viewport: unknown): boolean {
61
+ return isGlobeViewport(viewport) || isProjectedViewport(viewport);
62
+ }
63
+
64
+ /** Converts longitude/latitude to the Web Mercator world, `MERCATOR_WORLD_SIZE` wide, y up. */
65
+ export function lngLatToMercatorWorld(lngLat: ArrayLike<number>): [number, number] {
66
+ const latitude = Math.max(-MAX_MERCATOR_LATITUDE, Math.min(MAX_MERCATOR_LATITUDE, lngLat[1]));
67
+ const phi = (latitude * Math.PI) / 180;
68
+ return [
69
+ (MERCATOR_WORLD_SIZE * (lngLat[0] + 180)) / 360,
70
+ (MERCATOR_WORLD_SIZE * (Math.PI + Math.log(Math.tan(Math.PI / 4 + phi / 2)))) / (2 * Math.PI)
71
+ ];
72
+ }
73
+
74
+ /** Converts a Web Mercator world position, y up, to longitude/latitude. */
75
+ export function mercatorWorldToLngLat(world: ArrayLike<number>): [number, number] {
76
+ const lambda = (world[0] / MERCATOR_WORLD_SIZE) * 2 * Math.PI - Math.PI;
77
+ const phi = 2 * (Math.atan(Math.exp((world[1] / MERCATOR_WORLD_SIZE) * 2 * Math.PI - Math.PI)) - Math.PI / 4);
78
+ return [(lambda * 180) / Math.PI, (phi * 180) / Math.PI];
79
+ }
80
+
81
+ const mercatorZooms = new WeakMap<object, number>();
82
+
83
+ /**
84
+ * The zoom at which a Web Mercator map has the scale of a projected view at its center: the zoom
85
+ * styles are evaluated at and tiles are selected for. A projected view's own zoom counts pixels
86
+ * per unit of its coordinate reference system, and a Web Mercator zoom pixels per unit of a
87
+ * projection that stretches with the latitude: the two differ by about log2(cos(latitude)).
88
+ * Measured from two screen steps at the view center, so it holds for any projection. Returns the
89
+ * view's own zoom where the center has no longitude/latitude.
90
+ */
91
+ export function getMercatorZoom(viewport: ProjectedViewportLike): number {
92
+ const cached = mercatorZooms.get(viewport);
93
+ if (cached !== undefined) {
94
+ return cached;
95
+ }
96
+ const zoom = measureMercatorZoom(viewport);
97
+ mercatorZooms.set(viewport, zoom);
98
+ return zoom;
99
+ }
100
+
101
+ function measureMercatorZoom(viewport: ProjectedViewportLike): number {
102
+ return measureMercatorZoomInPlane(viewport) ?? measureMercatorZoomOnScreen(viewport);
103
+ }
104
+
105
+ /** The degrees of the two steps that measure the scale of the map plane at the view center. */
106
+ const PLANE_SAMPLE_DEGREES = 1e-4;
107
+
108
+ /**
109
+ * The Web Mercator zoom from the scale of the view's map plane at the view center, with steps of
110
+ * `PLANE_SAMPLE_DEGREES` east and north: pixels per unit of the map plane are `2 ** zoom`. Screen
111
+ * steps from the center can reach past the edge of the tile pyramid or across a pole, and then
112
+ * measure another place. A center past the edge of the pyramid is measured at that edge. Returns
113
+ * null where the view has no map plane, or no scale for it at the center.
114
+ */
115
+ function measureMercatorZoomInPlane(viewport: ProjectedViewportLike): number | null {
116
+ if (!viewport.projectPosition) {
117
+ return null;
118
+ }
119
+ try {
120
+ const center = viewport.unproject([viewport.width / 2, viewport.height / 2]);
121
+ const limit = MAX_MERCATOR_LATITUDE - PLANE_SAMPLE_DEGREES;
122
+ const lngLat = [center[0], Math.max(-limit, Math.min(limit, center[1]))];
123
+ const east = [lngLat[0] + PLANE_SAMPLE_DEGREES, lngLat[1]];
124
+ const north = [lngLat[0], lngLat[1] + PLANE_SAMPLE_DEGREES];
125
+ const plane = [lngLat, east, north].map(position => viewport.projectPosition!([position[0], position[1], 0]));
126
+ const world = [lngLat, east, north].map(lngLatToMercatorWorld);
127
+ const length = (points: ArrayLike<number>[], to: number) =>
128
+ Math.hypot(points[to][0] - points[0][0], points[to][1] - points[0][1]);
129
+ // Mercator units per unit of the map plane: the geometric mean of both axes, which differ in a
130
+ // projection that is not conformal.
131
+ const ratio = Math.sqrt(
132
+ (length(world, 1) / length(plane, 1)) * (length(world, 2) / length(plane, 2))
133
+ );
134
+ const zoom = viewport.zoom - Math.log2(ratio);
135
+ return ratio > 0 && Number.isFinite(zoom) ? zoom : null;
136
+ } catch {
137
+ // deck.gl throws for a position that its projection converts to no number.
138
+ return null;
139
+ }
140
+ }
141
+
142
+ function measureMercatorZoomOnScreen(viewport: ProjectedViewportLike): number {
143
+ const {width, height} = viewport;
144
+ const center = [width / 2, height / 2];
145
+ const origin = getWorldAt(viewport, center);
146
+ const right = getWorldAt(viewport, [center[0] + SCALE_SAMPLE_PIXELS, center[1]]);
147
+ const down = getWorldAt(viewport, [center[0], center[1] + SCALE_SAMPLE_PIXELS]);
148
+ if (!origin || !right || !down) {
149
+ return viewport.zoom;
150
+ }
151
+ const stepX = Math.hypot(right[0] - origin[0], right[1] - origin[1]);
152
+ const stepY = Math.hypot(down[0] - origin[0], down[1] - origin[1]);
153
+ // The geometric mean of both axes: the two differ in a projection that is not conformal.
154
+ const worldPerPixel = Math.sqrt(stepX * stepY) / SCALE_SAMPLE_PIXELS;
155
+ const zoom = -Math.log2(worldPerPixel);
156
+ return Number.isFinite(zoom) ? zoom : viewport.zoom;
157
+ }
158
+
159
+ function getWorldAt(viewport: ProjectedViewportLike, pixel: number[]): [number, number] | null {
160
+ let lngLat: number[];
161
+ try {
162
+ lngLat = viewport.unproject(pixel);
163
+ } catch {
164
+ // deck.gl throws for a pixel that its projection converts to no number.
165
+ return null;
166
+ }
167
+ if (
168
+ !Number.isFinite(lngLat[0]) ||
169
+ !Number.isFinite(lngLat[1]) ||
170
+ Math.abs(lngLat[1]) > MAX_MERCATOR_LATITUDE
171
+ ) {
172
+ return null;
173
+ }
174
+ return lngLatToMercatorWorld(lngLat);
175
+ }
176
+
177
+ /**
178
+ * The zoom styles are evaluated at in a viewport: its own zoom, or in a projected view the zoom of
179
+ * the Web Mercator map of the same scale (see `getMercatorZoom`).
180
+ */
181
+ export function getStyleZoom(viewport: unknown): number {
182
+ if (!viewport) {
183
+ return 0;
184
+ }
185
+ return isProjectedViewport(viewport)
186
+ ? getMercatorZoom(viewport as ProjectedViewportLike)
187
+ : (viewport as {zoom?: number}).zoom || 0;
188
+ }
189
+
190
+ /** The longitude step, in degrees, that measures a direction in a projected view. */
191
+ const ANGLE_SAMPLE_DEGREES = 1e-4;
192
+
193
+ /**
194
+ * The direction a projected view draws a Web Mercator direction in, in its map plane, at
195
+ * `lngLat`: `degrees` counter-clockwise from east in Web Mercator space, to degrees
196
+ * counter-clockwise from the x axis of the view's coordinate reference system. The two differ by
197
+ * the projection's convergence, and in a projection that is not conformal also with the
198
+ * direction. Returns `degrees` where the view does not project the position.
199
+ */
200
+ export function getProjectedPlaneAngle(
201
+ viewport: {projectPosition: (position: number[]) => number[]},
202
+ lngLat: ArrayLike<number>,
203
+ degrees: number
204
+ ): number {
205
+ const radians = (degrees * Math.PI) / 180;
206
+ const cosLatitude = Math.cos((lngLat[1] * Math.PI) / 180);
207
+ let dx: number;
208
+ let dy: number;
209
+ try {
210
+ const from = viewport.projectPosition([lngLat[0], lngLat[1], 0]);
211
+ // A Web Mercator step: its latitude part shrinks with the latitude.
212
+ const to = viewport.projectPosition([
213
+ lngLat[0] + ANGLE_SAMPLE_DEGREES * Math.cos(radians),
214
+ lngLat[1] + ANGLE_SAMPLE_DEGREES * Math.sin(radians) * cosLatitude,
215
+ 0
216
+ ]);
217
+ dx = to[0] - from[0];
218
+ dy = to[1] - from[1];
219
+ } catch {
220
+ return degrees;
221
+ }
222
+ if (!Number.isFinite(dx) || !Number.isFinite(dy) || (dx === 0 && dy === 0)) {
223
+ return degrees;
224
+ }
225
+ return (Math.atan2(dy, dx) * 180) / Math.PI;
226
+ }
227
+
228
+ /** The most degrees a position may lie from itself after it is projected and projected back. */
229
+ const ROUND_TRIP_DEGREES = 1e-5;
230
+
231
+ /**
232
+ * Whether a projected view draws a longitude/latitude at its own place: the position, projected
233
+ * to the map plane and back, is the same position. A projection moves a position outside its area
234
+ * of use to the edge of that area, or has no position for it. A label there is not drawn: it
235
+ * shows on the edge, far from its place. The check does not depend on the camera.
236
+ */
237
+ export function isPositionDrawn(
238
+ viewport: {
239
+ projectPosition: (position: number[]) => number[];
240
+ unprojectPosition: (position: number[]) => number[];
241
+ },
242
+ lngLat: ArrayLike<number>
243
+ ): boolean {
244
+ try {
245
+ const back = viewport.unprojectPosition(viewport.projectPosition([lngLat[0], lngLat[1], 0]));
246
+ const turn = Math.abs(back[0] - lngLat[0]) % 360;
247
+ return (
248
+ Math.min(turn, 360 - turn) <= ROUND_TRIP_DEGREES &&
249
+ Math.abs(back[1] - lngLat[1]) <= ROUND_TRIP_DEGREES
250
+ );
251
+ } catch {
252
+ return false;
253
+ }
254
+ }
@@ -163,6 +163,30 @@ vec3 rasterColor_adjust(vec3 color) {
163
163
  rgb = (rgb - 0.5) * rasterColor.contrastFactor + 0.5;
164
164
  return mix(vec3(rasterColor.brightnessLow), vec3(rasterColor.brightnessHigh), rgb);
165
165
  }
166
+ `,
167
+ source: /* wgsl */ `\
168
+ struct RasterColorUniforms {
169
+ brightnessLow: f32,
170
+ brightnessHigh: f32,
171
+ saturationFactor: f32,
172
+ contrastFactor: f32,
173
+ spinWeights: vec3<f32>,
174
+ };
175
+
176
+ @group(0) @binding(auto) var<uniform> rasterColor: RasterColorUniforms;
177
+
178
+ // The GLSL function above, in WGSL.
179
+ fn rasterColor_adjust(color: vec3<f32>) -> vec3<f32> {
180
+ var rgb = vec3<f32>(
181
+ dot(color, rasterColor.spinWeights.xyz),
182
+ dot(color, rasterColor.spinWeights.zxy),
183
+ dot(color, rasterColor.spinWeights.yzx)
184
+ );
185
+ let average = (color.r + color.g + color.b) / 3.0;
186
+ rgb += (average - rgb) * rasterColor.saturationFactor;
187
+ rgb = (rgb - 0.5) * rasterColor.contrastFactor + 0.5;
188
+ return mix(vec3<f32>(rasterColor.brightnessLow), vec3<f32>(rasterColor.brightnessHigh), rgb);
189
+ }
166
190
  `,
167
191
  uniformTypes: {
168
192
  brightnessLow: 'f32',
@@ -173,7 +197,7 @@ vec3 rasterColor_adjust(vec3 color) {
173
197
  }
174
198
  } as const;
175
199
 
176
- const injection = {
200
+ const glslInjection = {
177
201
  // MapLibre writes the adjusted color premultiplied, so a value pushed above 1 is clamped only
178
202
  // after the multiplication by alpha. Layers using this extension blend premultiplied colors
179
203
  // (`RASTER_COLOR_BLEND_PARAMETERS`) to draw the same.
@@ -182,6 +206,14 @@ const injection = {
182
206
  `
183
207
  };
184
208
 
209
+ const wgslInjection = {
210
+ // deck.gl's WGSL bitmap shader premultiplies the filtered color itself, and WebGPU draws with
211
+ // premultiplied blending, so the adjusted color is clamped after the multiplication by alpha.
212
+ 'fs:DECKGL_FILTER_COLOR': /* wgsl */ `
213
+ *color = vec4<f32>(rasterColor_adjust((*color).rgb), (*color).a);
214
+ `
215
+ };
216
+
185
217
  /** The blending of layers using `RasterColorExtension`, whose shader outputs premultiplied colors. */
186
218
  export const RASTER_COLOR_BLEND_PARAMETERS = {
187
219
  blendColorOperation: 'add',
@@ -198,20 +230,18 @@ type RasterColorExtensionProps = {
198
230
  rasterColorAdjustments?: RasterColorAdjustments;
199
231
  };
200
232
 
201
- /** Whether the extension's shader runs on a layer's device. It is written in GLSL only. */
202
- function isRasterColorSupported(layer: Layer): boolean {
203
- return layer.context.device.type !== 'webgpu';
233
+ function isWebGPU(layer: Layer): boolean {
234
+ return layer.context.device.type === 'webgpu';
204
235
  }
205
236
 
206
237
  /**
207
238
  * Applies a raster style layer's `raster-brightness-min`, `raster-brightness-max`,
208
239
  * `raster-saturation`, `raster-contrast` and `raster-hue-rotate` to a `BitmapLayer`, with the
209
- * formulas of MapLibre GL JS's raster shader. WebGL only.
240
+ * formulas of MapLibre GL JS's raster shader, on WebGL and WebGPU.
210
241
  *
211
242
  * The adjusted colors are premultiplied, so the layer must blend with
212
- * `RASTER_COLOR_BLEND_PARAMETERS` exactly where the shader runs. Added to a composite layer, the
213
- * extension passes them to its sublayers as `rasterColorBlendParameters`, decided by the same
214
- * device check as the shader; on WebGPU they are `null` and the sublayers keep their blending.
243
+ * `RASTER_COLOR_BLEND_PARAMETERS`. Added to a composite layer, the extension passes them to its
244
+ * sublayers as `rasterColorBlendParameters`. On WebGPU they are deck.gl's default blending.
215
245
  */
216
246
  export class RasterColorExtension extends LayerExtension {
217
247
  static extensionName = 'RasterColorExtension';
@@ -220,25 +250,20 @@ export class RasterColorExtension extends LayerExtension {
220
250
  };
221
251
 
222
252
  getShaders(this: Layer<RasterColorExtensionProps>) {
223
- if (!isRasterColorSupported(this)) {
224
- return {};
225
- }
226
- return {modules: [rasterColorModule], inject: injection};
253
+ return {
254
+ modules: [rasterColorModule],
255
+ inject: isWebGPU(this) ? wgslInjection : glslInjection
256
+ };
227
257
  }
228
258
 
229
259
  getSubLayerProps(this: CompositeLayer<RasterColorExtensionProps>, extension: this) {
230
260
  return {
231
261
  ...LayerExtension.prototype.getSubLayerProps.call(this, extension),
232
- rasterColorBlendParameters: isRasterColorSupported(this)
233
- ? RASTER_COLOR_BLEND_PARAMETERS
234
- : null
262
+ rasterColorBlendParameters: RASTER_COLOR_BLEND_PARAMETERS
235
263
  };
236
264
  }
237
265
 
238
266
  draw(this: Layer<RasterColorExtensionProps>) {
239
- if (!isRasterColorSupported(this)) {
240
- return;
241
- }
242
267
  const adjustments = this.props.rasterColorAdjustments ?? DEFAULT_RASTER_COLOR_ADJUSTMENTS;
243
268
  this.setShaderModuleProps({rasterColor: getRasterColorUniforms(adjustments)});
244
269
  }