Skip to content

Commit 8d824a4

Browse files
MVTLayer and TerrainLayer switch to use worker-only loaders (visgl#5946)
1 parent 3e6b152 commit 8d824a4

File tree

8 files changed

+79
-35
lines changed

8 files changed

+79
-35
lines changed

docs/api-reference/geo-layers/mvt-layer.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ On top of the [default options](/docs/api-reference/core/layer.md#loadoptions),
117117

118118
- [MVTLoader](https://loaders.gl/modules/mvt/docs/api-reference/mvt-loader)
119119

120+
Note that by default, the `MVTLoader` parses data using web workers, with code loaded from a [CDN](https://unpkg.com). To change this behavior, see [loaders and workers](/docs/developer-guide/loading-data.md#loaders-and-web-workers).
121+
120122
##### `binary` (Boolean, optional)
121123

122124
* Default: true

docs/api-reference/geo-layers/terrain-layer.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,15 @@ Must be supplied when using non-tiled elevation data.
145145
- Default: `null`
146146

147147

148-
##### `workerUrl` (String, optional)
149-
150-
**Advanced** Supply url to local terrain worker bundle. By default, it points to the latest published `@loaders.gl/terrain` NPM module on [unpkg.com](unpkg.com). Custom `workerUrl` may be desirable if the application wishes to serve the worker code itself without relying on the CDN. The worker bundle can be located locally in `"node_modules/@loaders.gl/terrain/dist/terrain-loader.worker.js"`.
151-
152-
Set `workerUrl` to an empty string to disable worker during debugging (improves error messages).
153-
154-
- Default: `null`
155-
156-
157148
##### `loadOptions` (Object, optional)
158149

159150
On top of the [default options](/docs/api-reference/core/layer.md#loadoptions), also accepts options for the following loaders:
160151

152+
- [TerrainLoader](https://loaders.gl/modules/terrain/docs/api-reference/terrain-loader)
161153
- [ImageLoader](https://loaders.gl/modules/images/docs/api-reference/image-loader) if the `texture` prop is supplied
162154

155+
Note that by default, the `TerrainLoader` parses data using web workers, with code loaded from a [CDN](https://unpkg.com). To change this behavior, see [loaders and workers](/docs/developer-guide/loading-data.md#loaders-and-web-workers).
156+
163157

164158
### Render Options
165159

docs/developer-guide/loading-data.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,36 @@ setInterval(() => {
136136
}, 5 * 60 * 1000);
137137
```
138138

139+
140+
## Loaders and Web Workers
141+
142+
For the best performance, some specialized loaders parse data using web workers, for example `TerrainLoader` in the [TerrainLayer](/docs/api-reference/geo-layers/terrain-layer.md) and `MVTLoader` in the [MVTLayer](/docs/api-reference/geo-layers/mvt-layer.md). By default, the worker code is loaded from from the latest published NPM module on [unpkg.com](https://unpkg.com).
143+
144+
It might be desirable for some applications to serve the worker code itself without relying on the CDN. To do this, locate the worker bundle locally in `node_modules/@loaders.gl/<module>/dist/<name>-loader.worker.js` and serve it as a static asset with your server. Point the loader to use this alternative URL using `loadOptions.<name>.workerUrl`:
145+
146+
```js
147+
new MVTLayer({
148+
loadOptions: {
149+
mvt: {
150+
workerUrl: <my_worker_url>
151+
}
152+
}
153+
}
154+
```
155+
156+
If the layer is used in an environment that does not support web workers, or you need to debug the loader code on the main thread, you may import the full loader like this:
157+
158+
```js
159+
import {MVTLoader} from '@loaders.gl/mvt';
160+
new MVTLayer({
161+
loaders: [MVTLoader],
162+
loadOptions: {worker: false}
163+
});
164+
```
165+
166+
Refer to each specific layer's documentation to see which loaders are used.
167+
168+
139169
## Load Resource Without an URL
140170
141171
In some use cases, resources do not exist at a static URL. For example, some applications construct images dynamically based on user input. Some applications receive arbitrary binary blobs from a server via a WebSocket connection.

docs/upgrade-guide.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ The module entry point is now only lightly transpiled for the most commonly used
2525
- `CartoSQLLayer` will be deprecated in 8.6. Use `CartoLayer` instead with `type` set to `MAP_TYPES.QUERY`.
2626
- `GeoJsonLayer`'s `getRadius` props is deprecated, replaced by `getPointRadius`.
2727
- It is recommended to use `zoomOffset` in the `TileLayer` when trying to affect the `zoom` resolution at which tiles are fetched.
28+
- `MVTLayer` and `TerrainLayer`'s default loaders no longer support parsing on the main thread. This is the same behavior as before, just dropping unused code from the bundle. Should you need to use the layers in an environment where web worker is not available, or debug the loaders, you can supply the full loader as such:
29+
30+
```js
31+
import {MVTLoader} from '@loaders.gl/mvt';
32+
new MVTLayer({
33+
loaders: [MVTLoader],
34+
loadOptions: {worker: false}
35+
});
36+
```
37+
38+
```js
39+
import {TerrainLoader} from '@loaders.gl/terrain';
40+
new TerrainLayer({
41+
loaders: [TerrainLoader],
42+
loadOptions: {worker: false}
43+
});
44+
```
45+
- `TerrainLayer`'s `workerUrl` prop is removed, use `loadOptions.terrain.workerUrl` instead.
46+
2847

2948
### onError Callback
3049

modules/geo-layers/src/mvt-layer/mvt-layer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {log} from '@deck.gl/core';
22
import {Matrix4} from 'math.gl';
3-
import {MVTLoader} from '@loaders.gl/mvt';
3+
import {MVTWorkerLoader} from '@loaders.gl/mvt';
44
import {binaryToGeoJson} from '@loaders.gl/gis';
55
import {COORDINATE_SYSTEM} from '@deck.gl/core';
66
import {_binaryToFeature, _findIndexBinary} from '@deck.gl/layers';
@@ -17,7 +17,7 @@ const WORLD_SIZE = 512;
1717
const defaultProps = {
1818
uniqueIdProperty: {type: 'string', value: ''},
1919
highlightedFeatureId: null,
20-
loaders: [MVTLoader],
20+
loaders: [MVTWorkerLoader],
2121
binary: true
2222
};
2323

modules/geo-layers/src/terrain-layer/terrain-layer.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
import {CompositeLayer} from '@deck.gl/core';
21+
import {CompositeLayer, log} from '@deck.gl/core';
2222
import {SimpleMeshLayer} from '@deck.gl/mesh-layers';
2323
import {WebMercatorViewport, COORDINATE_SYSTEM} from '@deck.gl/core';
24-
import {TerrainLoader} from '@loaders.gl/terrain';
24+
import {TerrainWorkerLoader} from '@loaders.gl/terrain';
2525
import TileLayer from '../tile-layer/tile-layer';
2626
import {urlType, getURLFromTemplate} from '../tile-layer/utils';
2727

@@ -55,7 +55,7 @@ const defaultProps = {
5555
wireframe: false,
5656
material: true,
5757

58-
loaders: [TerrainLoader]
58+
loaders: [TerrainWorkerLoader]
5959
};
6060

6161
// Turns array of templates into a single string to work around shallow change
@@ -95,29 +95,33 @@ export default class TerrainLayer extends CompositeLayer {
9595
const terrain = this.loadTerrain(props);
9696
this.setState({terrain});
9797
}
98+
99+
// TODO - remove in v9
100+
if (props.workerUrl) {
101+
log.removed('workerUrl', 'loadOptions.terrain.workerUrl')();
102+
}
98103
}
99104

100-
loadTerrain({elevationData, bounds, elevationDecoder, meshMaxError, signal, workerUrl}) {
105+
loadTerrain({elevationData, bounds, elevationDecoder, meshMaxError, signal}) {
101106
if (!elevationData) {
102107
return null;
103108
}
104-
const loadOptions = {
105-
...this.getLoadOptions(),
109+
let loadOptions = this.getLoadOptions();
110+
loadOptions = {
111+
...loadOptions,
106112
terrain: {
113+
...loadOptions?.terrain,
107114
bounds,
108115
meshMaxError,
109116
elevationDecoder
110117
}
111118
};
112-
if (workerUrl !== null) {
113-
loadOptions.terrain.workerUrl = workerUrl;
114-
}
115119
const {fetch} = this.props;
116120
return fetch(elevationData, {propName: 'elevationData', layer: this, loadOptions, signal});
117121
}
118122

119123
getTiledTerrainData(tile) {
120-
const {elevationData, fetch, texture, elevationDecoder, meshMaxError, workerUrl} = this.props;
124+
const {elevationData, fetch, texture, elevationDecoder, meshMaxError} = this.props;
121125
const dataUrl = getURLFromTemplate(elevationData, tile);
122126
const textureUrl = getURLFromTemplate(texture, tile);
123127

@@ -136,8 +140,7 @@ export default class TerrainLayer extends CompositeLayer {
136140
bounds,
137141
elevationDecoder,
138142
meshMaxError,
139-
signal,
140-
workerUrl
143+
signal
141144
});
142145
const surface = textureUrl
143146
? // If surface image fails to load, the tile should still be displayed

test/modules/geo-layers/mvt-layer.spec.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {ClipExtension} from '@deck.gl/extensions';
55
import {transform} from '@deck.gl/geo-layers/mvt-layer/coordinate-transform';
66
import {GeoJsonLayer} from '@deck.gl/layers';
77
import {geojsonToBinary} from '@loaders.gl/gis';
8+
import {MVTLoader} from '@loaders.gl/mvt';
89

910
import {ScatterplotLayer} from '@deck.gl/layers';
1011
import {WebMercatorViewport} from '@deck.gl/core';
@@ -442,11 +443,7 @@ test('MVTLayer#triangulation', async t => {
442443
throw error;
443444
}
444445
},
445-
loadOptions: {
446-
mvt: {
447-
workerUrl: null
448-
}
449-
}
446+
loaders: [MVTLoader]
450447
};
451448
const testCases = [{props, onAfterUpdate}];
452449

@@ -499,11 +496,7 @@ for (const tileset of ['mvt-tiles', 'mvt-with-hole']) {
499496
throw error;
500497
}
501498
},
502-
loadOptions: {
503-
mvt: {
504-
workerUrl: null
505-
}
506-
}
499+
loaders: [MVTLoader]
507500
};
508501
const testCases = [
509502
{props: {binary: false, data: url1, ...props}, onAfterUpdate},

test/modules/geo-layers/terrain-layer.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ import test from 'tape-catch';
2222
import {generateLayerTests, testLayerAsync} from '@deck.gl/test-utils';
2323
import {TerrainLayer, TileLayer} from '@deck.gl/geo-layers';
2424
import {SimpleMeshLayer} from '@deck.gl/mesh-layers';
25+
import {TerrainLoader} from '@loaders.gl/terrain';
2526

2627
test('TerrainLayer', async t => {
2728
const testCases = generateLayerTests({
2829
Layer: TerrainLayer,
2930
sampleProps: {
3031
elevationData: 'https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png',
31-
texture: 'https://wms.chartbundle.com/tms/1.0.0/sec/{z}/{x}/{y}.png?origin=nw'
32+
texture: 'https://wms.chartbundle.com/tms/1.0.0/sec/{z}/{x}/{y}.png?origin=nw',
33+
loaders: [TerrainLoader]
3234
},
3335
assert: t.ok,
3436
onBeforeUpdate: ({testCase}) => t.comment(testCase.title),
@@ -44,7 +46,8 @@ test('TerrainLayer', async t => {
4446
Layer: TerrainLayer,
4547
sampleProps: {
4648
elevationData: 'https://s3.amazonaws.com/elevation-tiles-prod/terrarium/1/0/0.png',
47-
bounds: [-180, 85, 0, 0]
49+
bounds: [-180, 85, 0, 0],
50+
loaders: [TerrainLoader]
4851
},
4952
assert: t.ok,
5053
onBeforeUpdate: ({testCase}) => t.comment(testCase.title),

0 commit comments

Comments
 (0)