@deck.gl/geo-layers 8.9.10 → 8.9.11

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 (34) hide show
  1. package/dist.min.js +1 -1
  2. package/package.json +2 -2
  3. package/typed/tile-3d-layer/utils.d.ts +3 -0
  4. package/typed/tile-3d-layer/utils.d.ts.map +1 -0
  5. package/typed/tile-layer/tileset-2d.d.ts.map +1 -1
  6. package/typed/tile-layer/utils.d.ts +1 -1
  7. package/typed/tile-layer/utils.d.ts.map +1 -1
  8. package/typed/geo-cell-layer/GeoCellLayer.js +0 -49
  9. package/typed/great-circle-layer/great-circle-layer.js +0 -29
  10. package/typed/h3-layers/h3-cluster-layer.js +0 -33
  11. package/typed/h3-layers/h3-hexagon-layer.js +0 -251
  12. package/typed/imagery-layer/imagery-layer.d.ts +0 -53
  13. package/typed/imagery-layer/imagery-layer.d.ts.map +0 -1
  14. package/typed/index.js +0 -33
  15. package/typed/mesh-layer/mesh-layer-fragment.glsl.js +0 -55
  16. package/typed/mesh-layer/mesh-layer-vertex.glsl.js +0 -80
  17. package/typed/mesh-layer/mesh-layer.js +0 -110
  18. package/typed/mvt-layer/coordinate-transform.js +0 -41
  19. package/typed/mvt-layer/find-index-binary.js +0 -96
  20. package/typed/mvt-layer/mvt-layer.js +0 -297
  21. package/typed/quadkey-layer/quadkey-layer.js +0 -18
  22. package/typed/quadkey-layer/quadkey-utils.js +0 -26
  23. package/typed/s2-layer/s2-geometry.js +0 -123
  24. package/typed/s2-layer/s2-layer.js +0 -37
  25. package/typed/s2-layer/s2-utils.js +0 -88
  26. package/typed/terrain-layer/terrain-layer.js +0 -226
  27. package/typed/tile-3d-layer/tile-3d-layer.js +0 -294
  28. package/typed/tile-layer/tile-2d-header.js +0 -113
  29. package/typed/tile-layer/tile-2d-traversal.js +0 -181
  30. package/typed/tile-layer/tile-layer.js +0 -192
  31. package/typed/tile-layer/tileset-2d.js +0 -399
  32. package/typed/tile-layer/types.js +0 -1
  33. package/typed/tile-layer/utils.js +0 -197
  34. package/typed/trips-layer/trips-layer.js +0 -92
@@ -1,226 +0,0 @@
1
- // Copyright (c) 2015 - 2017 Uber Technologies, Inc.
2
- //
3
- // Permission is hereby granted, free of charge, to any person obtaining a copy
4
- // of this software and associated documentation files (the "Software"), to deal
5
- // in the Software without restriction, including without limitation the rights
6
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- // copies of the Software, and to permit persons to whom the Software is
8
- // furnished to do so, subject to the following conditions:
9
- //
10
- // The above copyright notice and this permission notice shall be included in
11
- // all copies or substantial portions of the Software.
12
- //
13
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- // THE SOFTWARE.
20
- import { CompositeLayer, log } from '@deck.gl/core';
21
- import { SimpleMeshLayer } from '@deck.gl/mesh-layers';
22
- import { COORDINATE_SYSTEM } from '@deck.gl/core';
23
- import { TerrainWorkerLoader } from '@loaders.gl/terrain';
24
- import TileLayer from '../tile-layer/tile-layer';
25
- import { urlType, getURLFromTemplate } from '../tile-layer/utils';
26
- const DUMMY_DATA = [1];
27
- const defaultProps = {
28
- ...TileLayer.defaultProps,
29
- // Image url that encodes height data
30
- elevationData: urlType,
31
- // Image url to use as texture
32
- texture: { ...urlType, optional: true },
33
- // Martini error tolerance in meters, smaller number -> more detailed mesh
34
- meshMaxError: { type: 'number', value: 4.0 },
35
- // Bounding box of the terrain image, [minX, minY, maxX, maxY] in world coordinates
36
- bounds: { type: 'array', value: null, optional: true, compare: true },
37
- // Color to use if texture is unavailable
38
- color: { type: 'color', value: [255, 255, 255] },
39
- // Object to decode height data, from (r, g, b) to height in meters
40
- elevationDecoder: {
41
- type: 'object',
42
- value: {
43
- rScaler: 1,
44
- gScaler: 0,
45
- bScaler: 0,
46
- offset: 0
47
- }
48
- },
49
- // Supply url to local terrain worker bundle. Only required if running offline and cannot access CDN.
50
- workerUrl: { type: 'string', value: null },
51
- // Same as SimpleMeshLayer wireframe
52
- wireframe: false,
53
- material: true,
54
- loaders: [TerrainWorkerLoader]
55
- };
56
- // Turns array of templates into a single string to work around shallow change
57
- function urlTemplateToUpdateTrigger(template) {
58
- if (Array.isArray(template)) {
59
- return template.join(';');
60
- }
61
- return template;
62
- }
63
- /**
64
- * state: {
65
- * isTiled: True renders TileLayer of many SimpleMeshLayers, false renders one SimpleMeshLayer
66
- * terrain: Mesh object. Only defined when isTiled is false.
67
- * }
68
- */
69
- export default class TerrainLayer extends CompositeLayer {
70
- updateState({ props, oldProps }) {
71
- const elevationDataChanged = props.elevationData !== oldProps.elevationData;
72
- if (elevationDataChanged) {
73
- const { elevationData } = props;
74
- const isTiled = elevationData &&
75
- (Array.isArray(elevationData) ||
76
- (elevationData.includes('{x}') && elevationData.includes('{y}')));
77
- this.setState({ isTiled });
78
- }
79
- // Reloading for single terrain mesh
80
- const shouldReload = elevationDataChanged ||
81
- props.meshMaxError !== oldProps.meshMaxError ||
82
- props.elevationDecoder !== oldProps.elevationDecoder ||
83
- props.bounds !== oldProps.bounds;
84
- if (!this.state.isTiled && shouldReload) {
85
- const terrain = this.loadTerrain(props);
86
- this.setState({ terrain });
87
- }
88
- // TODO - remove in v9
89
- if (props.workerUrl) {
90
- log.removed('workerUrl', 'loadOptions.terrain.workerUrl')();
91
- }
92
- }
93
- loadTerrain({ elevationData, bounds, elevationDecoder, meshMaxError, signal }) {
94
- if (!elevationData) {
95
- return null;
96
- }
97
- let loadOptions = this.getLoadOptions();
98
- loadOptions = {
99
- ...loadOptions,
100
- terrain: {
101
- skirtHeight: this.state.isTiled ? meshMaxError * 2 : 0,
102
- ...loadOptions?.terrain,
103
- bounds,
104
- meshMaxError,
105
- elevationDecoder
106
- }
107
- };
108
- const { fetch } = this.props;
109
- return fetch(elevationData, { propName: 'elevationData', layer: this, loadOptions, signal });
110
- }
111
- getTiledTerrainData(tile) {
112
- const { elevationData, fetch, texture, elevationDecoder, meshMaxError } = this.props;
113
- const { viewport } = this.context;
114
- const dataUrl = getURLFromTemplate(elevationData, tile);
115
- const textureUrl = getURLFromTemplate(texture, tile);
116
- const { bbox, signal } = tile;
117
- const bottomLeft = viewport.isGeospatial
118
- ? viewport.projectFlat([bbox.west, bbox.south])
119
- : [bbox.left, bbox.bottom];
120
- const topRight = viewport.isGeospatial
121
- ? viewport.projectFlat([bbox.east, bbox.north])
122
- : [bbox.right, bbox.top];
123
- const bounds = [bottomLeft[0], bottomLeft[1], topRight[0], topRight[1]];
124
- const terrain = this.loadTerrain({
125
- elevationData: dataUrl,
126
- bounds,
127
- elevationDecoder,
128
- meshMaxError,
129
- signal
130
- });
131
- const surface = textureUrl
132
- ? // If surface image fails to load, the tile should still be displayed
133
- fetch(textureUrl, { propName: 'texture', layer: this, loaders: [], signal }).catch(_ => null)
134
- : Promise.resolve(null);
135
- return Promise.all([terrain, surface]);
136
- }
137
- renderSubLayers(props) {
138
- const SubLayerClass = this.getSubLayerClass('mesh', SimpleMeshLayer);
139
- const { data, color } = props;
140
- if (!data) {
141
- return null;
142
- }
143
- const [mesh, texture] = data;
144
- return new SubLayerClass(props, {
145
- data: DUMMY_DATA,
146
- mesh,
147
- texture,
148
- _instanced: false,
149
- coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
150
- getPosition: d => [0, 0, 0],
151
- getColor: color
152
- });
153
- }
154
- // Update zRange of viewport
155
- onViewportLoad(tiles) {
156
- if (!tiles) {
157
- return;
158
- }
159
- const { zRange } = this.state;
160
- const ranges = tiles
161
- .map(tile => tile.content)
162
- .filter(Boolean)
163
- .map(arr => {
164
- const bounds = arr[0].header.boundingBox;
165
- return bounds.map(bound => bound[2]);
166
- });
167
- if (ranges.length === 0) {
168
- return;
169
- }
170
- const minZ = Math.min(...ranges.map(x => x[0]));
171
- const maxZ = Math.max(...ranges.map(x => x[1]));
172
- if (!zRange || minZ < zRange[0] || maxZ > zRange[1]) {
173
- this.setState({ zRange: [minZ, maxZ] });
174
- }
175
- }
176
- renderLayers() {
177
- const { color, material, elevationData, texture, wireframe, meshMaxError, elevationDecoder, tileSize, maxZoom, minZoom, extent, maxRequests, onTileLoad, onTileUnload, onTileError, maxCacheSize, maxCacheByteSize, refinementStrategy } = this.props;
178
- if (this.state.isTiled) {
179
- return new TileLayer(this.getSubLayerProps({
180
- id: 'tiles'
181
- }), {
182
- wireframe,
183
- color,
184
- material,
185
- getTileData: this.getTiledTerrainData.bind(this),
186
- renderSubLayers: this.renderSubLayers.bind(this),
187
- updateTriggers: {
188
- getTileData: {
189
- elevationData: urlTemplateToUpdateTrigger(elevationData),
190
- texture: urlTemplateToUpdateTrigger(texture),
191
- meshMaxError,
192
- elevationDecoder
193
- }
194
- },
195
- onViewportLoad: this.onViewportLoad.bind(this),
196
- zRange: this.state.zRange || null,
197
- tileSize,
198
- maxZoom,
199
- minZoom,
200
- extent,
201
- maxRequests,
202
- onTileLoad,
203
- onTileUnload,
204
- onTileError,
205
- maxCacheSize,
206
- maxCacheByteSize,
207
- refinementStrategy
208
- });
209
- }
210
- const SubLayerClass = this.getSubLayerClass('mesh', SimpleMeshLayer);
211
- return new SubLayerClass(this.getSubLayerProps({
212
- id: 'mesh'
213
- }), {
214
- data: DUMMY_DATA,
215
- mesh: this.state.terrain,
216
- texture,
217
- _instanced: false,
218
- getPosition: d => [0, 0, 0],
219
- getColor: color,
220
- material,
221
- wireframe
222
- });
223
- }
224
- }
225
- TerrainLayer.layerName = 'TerrainLayer';
226
- TerrainLayer.defaultProps = defaultProps;
@@ -1,294 +0,0 @@
1
- import GL from '@luma.gl/constants';
2
- import { Geometry } from '@luma.gl/core';
3
- import { COORDINATE_SYSTEM, CompositeLayer } from '@deck.gl/core';
4
- import { PointCloudLayer } from '@deck.gl/layers';
5
- import { ScenegraphLayer } from '@deck.gl/mesh-layers';
6
- import { default as _MeshLayer } from '../mesh-layer/mesh-layer';
7
- import { log } from '@deck.gl/core';
8
- import { load } from '@loaders.gl/core';
9
- import { Tileset3D, TILE_TYPE } from '@loaders.gl/tiles';
10
- import { Tiles3DLoader } from '@loaders.gl/3d-tiles';
11
- const SINGLE_DATA = [0];
12
- const defaultProps = {
13
- getPointColor: { type: 'accessor', value: [0, 0, 0, 255] },
14
- pointSize: 1.0,
15
- data: null,
16
- loader: Tiles3DLoader,
17
- onTilesetLoad: { type: 'function', value: tileset3d => { }, compare: false },
18
- onTileLoad: { type: 'function', value: tileHeader => { }, compare: false },
19
- onTileUnload: { type: 'function', value: tileHeader => { }, compare: false },
20
- onTileError: { type: 'function', value: (tile, message, url) => { }, compare: false },
21
- _getMeshColor: { type: 'function', value: tileHeader => [255, 255, 255], compare: false }
22
- };
23
- export default class Tile3DLayer extends CompositeLayer {
24
- initializeState() {
25
- if ('onTileLoadFail' in this.props) {
26
- log.removed('onTileLoadFail', 'onTileError')();
27
- }
28
- // prop verification
29
- this.state = {
30
- layerMap: {},
31
- tileset3d: null,
32
- activeViewports: {},
33
- lastUpdatedViewports: null
34
- };
35
- }
36
- get isLoaded() {
37
- const { tileset3d } = this.state;
38
- return tileset3d && tileset3d.isLoaded();
39
- }
40
- shouldUpdateState({ changeFlags }) {
41
- return changeFlags.somethingChanged;
42
- }
43
- updateState({ props, oldProps, changeFlags }) {
44
- if (props.data && props.data !== oldProps.data) {
45
- this._loadTileset(props.data);
46
- }
47
- if (changeFlags.viewportChanged) {
48
- const { activeViewports } = this.state;
49
- const viewportsNumber = Object.keys(activeViewports).length;
50
- if (viewportsNumber) {
51
- this._updateTileset(activeViewports);
52
- this.state.lastUpdatedViewports = activeViewports;
53
- this.state.activeViewports = {};
54
- }
55
- }
56
- if (changeFlags.propsChanged) {
57
- const { layerMap } = this.state;
58
- for (const key in layerMap) {
59
- layerMap[key].needsUpdate = true;
60
- }
61
- }
62
- }
63
- activateViewport(viewport) {
64
- const { activeViewports, lastUpdatedViewports } = this.state;
65
- this.internalState.viewport = viewport;
66
- activeViewports[viewport.id] = viewport;
67
- const lastViewport = lastUpdatedViewports?.[viewport.id];
68
- if (!lastViewport || !viewport.equals(lastViewport)) {
69
- this.setChangeFlags({ viewportChanged: true });
70
- this.setNeedsUpdate();
71
- }
72
- }
73
- getPickingInfo({ info, sourceLayer }) {
74
- const { layerMap } = this.state;
75
- const layerId = sourceLayer && sourceLayer.id;
76
- if (layerId) {
77
- // layerId: this.id-[scenegraph|pointcloud]-tileId
78
- const substr = layerId.substring(this.id.length + 1);
79
- const tileId = substr.substring(substr.indexOf('-') + 1);
80
- info.object = layerMap[tileId] && layerMap[tileId].tile;
81
- }
82
- return info;
83
- }
84
- filterSubLayer({ layer, viewport }) {
85
- const { tile } = layer.props;
86
- const { id: viewportId } = viewport;
87
- return tile.selected && tile.viewportIds.includes(viewportId);
88
- }
89
- _updateAutoHighlight(info) {
90
- if (info.sourceLayer) {
91
- info.sourceLayer.updateAutoHighlight(info);
92
- }
93
- }
94
- async _loadTileset(tilesetUrl) {
95
- const { loadOptions = {} } = this.props;
96
- // TODO: deprecate `loader` in v9.0
97
- let loader = this.props.loader || this.props.loaders;
98
- if (Array.isArray(loader)) {
99
- loader = loader[0];
100
- }
101
- const options = { loadOptions: { ...loadOptions } };
102
- if (loader.preload) {
103
- const preloadOptions = await loader.preload(tilesetUrl, loadOptions);
104
- if (preloadOptions.headers) {
105
- options.loadOptions.fetch = {
106
- ...options.loadOptions.fetch,
107
- headers: preloadOptions.headers
108
- };
109
- }
110
- Object.assign(options, preloadOptions);
111
- }
112
- const tilesetJson = await load(tilesetUrl, loader, options.loadOptions);
113
- const tileset3d = new Tileset3D(tilesetJson, {
114
- onTileLoad: this._onTileLoad.bind(this),
115
- onTileUnload: this._onTileUnload.bind(this),
116
- onTileLoadFail: this.props.onTileError,
117
- ...options
118
- });
119
- this.setState({
120
- tileset3d,
121
- layerMap: {}
122
- });
123
- this._updateTileset(this.state.activeViewports);
124
- this.props.onTilesetLoad(tileset3d);
125
- }
126
- _onTileLoad(tileHeader) {
127
- const { lastUpdatedViewports } = this.state;
128
- this.props.onTileLoad(tileHeader);
129
- this._updateTileset(lastUpdatedViewports);
130
- this.setNeedsUpdate();
131
- }
132
- _onTileUnload(tileHeader) {
133
- // Was cleaned up from tileset cache. We no longer need to track it.
134
- delete this.state.layerMap[tileHeader.id];
135
- this.props.onTileUnload(tileHeader);
136
- }
137
- _updateTileset(viewports) {
138
- const { tileset3d } = this.state;
139
- const { timeline } = this.context;
140
- const viewportsNumber = Object.keys(viewports).length;
141
- if (!timeline || !viewportsNumber || !tileset3d) {
142
- return;
143
- }
144
- tileset3d.selectTiles(Object.values(viewports)).then(frameNumber => {
145
- const tilesetChanged = this.state.frameNumber !== frameNumber;
146
- if (tilesetChanged) {
147
- this.setState({ frameNumber });
148
- }
149
- });
150
- }
151
- _getSubLayer(tileHeader, oldLayer) {
152
- if (!tileHeader.content) {
153
- return null;
154
- }
155
- switch (tileHeader.type) {
156
- case TILE_TYPE.POINTCLOUD:
157
- return this._makePointCloudLayer(tileHeader, oldLayer);
158
- case TILE_TYPE.SCENEGRAPH:
159
- return this._make3DModelLayer(tileHeader, oldLayer);
160
- case TILE_TYPE.MESH:
161
- return this._makeSimpleMeshLayer(tileHeader, oldLayer);
162
- default:
163
- throw new Error(`Tile3DLayer: Failed to render layer of type ${tileHeader.content.type}`);
164
- }
165
- }
166
- _makePointCloudLayer(tileHeader, oldLayer) {
167
- const { attributes, pointCount, constantRGBA, cartographicOrigin, modelMatrix } = tileHeader.content;
168
- const { positions, normals, colors } = attributes;
169
- if (!positions) {
170
- return null;
171
- }
172
- const data = (oldLayer && oldLayer.props.data) || {
173
- header: {
174
- vertexCount: pointCount
175
- },
176
- attributes: {
177
- POSITION: positions,
178
- NORMAL: normals,
179
- COLOR_0: colors
180
- }
181
- };
182
- const { pointSize, getPointColor } = this.props;
183
- const SubLayerClass = this.getSubLayerClass('pointcloud', PointCloudLayer);
184
- return new SubLayerClass({
185
- pointSize
186
- }, this.getSubLayerProps({
187
- id: 'pointcloud'
188
- }), {
189
- id: `${this.id}-pointcloud-${tileHeader.id}`,
190
- tile: tileHeader,
191
- data,
192
- coordinateSystem: COORDINATE_SYSTEM.METER_OFFSETS,
193
- coordinateOrigin: cartographicOrigin,
194
- modelMatrix,
195
- getColor: constantRGBA || getPointColor,
196
- _offset: 0
197
- });
198
- }
199
- _make3DModelLayer(tileHeader) {
200
- const { gltf, instances, cartographicOrigin, modelMatrix } = tileHeader.content;
201
- const SubLayerClass = this.getSubLayerClass('scenegraph', ScenegraphLayer);
202
- return new SubLayerClass({
203
- _lighting: 'pbr'
204
- }, this.getSubLayerProps({
205
- id: 'scenegraph'
206
- }), {
207
- id: `${this.id}-scenegraph-${tileHeader.id}`,
208
- tile: tileHeader,
209
- data: instances || SINGLE_DATA,
210
- scenegraph: gltf,
211
- coordinateSystem: COORDINATE_SYSTEM.METER_OFFSETS,
212
- coordinateOrigin: cartographicOrigin,
213
- modelMatrix,
214
- getTransformMatrix: instance => instance.modelMatrix,
215
- getPosition: [0, 0, 0],
216
- _offset: 0
217
- });
218
- }
219
- _makeSimpleMeshLayer(tileHeader, oldLayer) {
220
- const content = tileHeader.content;
221
- const { attributes, indices, modelMatrix, cartographicOrigin, coordinateSystem = COORDINATE_SYSTEM.METER_OFFSETS, material, featureIds } = content;
222
- const { _getMeshColor } = this.props;
223
- const geometry = (oldLayer && oldLayer.props.mesh) ||
224
- new Geometry({
225
- drawMode: GL.TRIANGLES,
226
- attributes: getMeshGeometry(attributes),
227
- indices
228
- });
229
- const SubLayerClass = this.getSubLayerClass('mesh', _MeshLayer);
230
- return new SubLayerClass(this.getSubLayerProps({
231
- id: 'mesh'
232
- }), {
233
- id: `${this.id}-mesh-${tileHeader.id}`,
234
- tile: tileHeader,
235
- mesh: geometry,
236
- data: SINGLE_DATA,
237
- getColor: _getMeshColor(tileHeader),
238
- pbrMaterial: material,
239
- modelMatrix,
240
- coordinateOrigin: cartographicOrigin,
241
- coordinateSystem,
242
- featureIds,
243
- _offset: 0
244
- });
245
- }
246
- renderLayers() {
247
- const { tileset3d, layerMap } = this.state;
248
- if (!tileset3d) {
249
- return null;
250
- }
251
- return tileset3d.tiles
252
- .map(tile => {
253
- const layerCache = (layerMap[tile.id] = layerMap[tile.id] || { tile });
254
- let { layer } = layerCache;
255
- if (tile.selected) {
256
- // render selected tiles
257
- if (!layer) {
258
- // create layer
259
- layer = this._getSubLayer(tile);
260
- }
261
- else if (layerCache.needsUpdate) {
262
- // props have changed, rerender layer
263
- layer = this._getSubLayer(tile, layer);
264
- layerCache.needsUpdate = false;
265
- }
266
- }
267
- layerCache.layer = layer;
268
- return layer;
269
- })
270
- .filter(Boolean);
271
- }
272
- }
273
- function getMeshGeometry(contentAttributes) {
274
- const attributes = {};
275
- attributes.positions = {
276
- ...contentAttributes.positions,
277
- value: new Float32Array(contentAttributes.positions.value)
278
- };
279
- if (contentAttributes.normals) {
280
- attributes.normals = contentAttributes.normals;
281
- }
282
- if (contentAttributes.texCoords) {
283
- attributes.texCoords = contentAttributes.texCoords;
284
- }
285
- if (contentAttributes.colors) {
286
- attributes.colors = contentAttributes.colors;
287
- }
288
- if (contentAttributes.uvRegions) {
289
- attributes.uvRegions = contentAttributes.uvRegions;
290
- }
291
- return attributes;
292
- }
293
- Tile3DLayer.layerName = 'Tile3DLayer';
294
- Tile3DLayer.defaultProps = defaultProps;
@@ -1,113 +0,0 @@
1
- /* eslint-env browser */
2
- import { log } from '@deck.gl/core';
3
- export default class Tile2DHeader {
4
- constructor(index) {
5
- this.index = index;
6
- this.isVisible = false;
7
- this.isSelected = false;
8
- this.parent = null;
9
- this.children = [];
10
- this.content = null;
11
- this._loader = undefined;
12
- this._abortController = null;
13
- this._loaderId = 0;
14
- this._isLoaded = false;
15
- this._isCancelled = false;
16
- this._needsReload = false;
17
- }
18
- get data() {
19
- return this.isLoading && this._loader ? this._loader.then(() => this.data) : this.content;
20
- }
21
- get isLoaded() {
22
- return this._isLoaded && !this._needsReload;
23
- }
24
- get isLoading() {
25
- return Boolean(this._loader) && !this._isCancelled;
26
- }
27
- get needsReload() {
28
- return this._needsReload || this._isCancelled;
29
- }
30
- get byteLength() {
31
- const result = this.content ? this.content.byteLength : 0;
32
- if (!Number.isFinite(result)) {
33
- log.error('byteLength not defined in tile data')();
34
- }
35
- return result;
36
- }
37
- /* eslint-disable max-statements */
38
- async _loadData({ getData, requestScheduler, onLoad, onError }) {
39
- const { index, id, bbox, userData, zoom } = this;
40
- const loaderId = this._loaderId;
41
- this._abortController = new AbortController();
42
- const { signal } = this._abortController;
43
- // @ts-expect-error (2345) Argument of type '(tile: any) => 1 | -1' is not assignable ...
44
- const requestToken = await requestScheduler.scheduleRequest(this, tile => {
45
- return tile.isSelected ? 1 : -1;
46
- });
47
- if (!requestToken) {
48
- this._isCancelled = true;
49
- return;
50
- }
51
- // A tile can be cancelled while being scheduled
52
- if (this._isCancelled) {
53
- requestToken.done();
54
- return;
55
- }
56
- let tileData = null;
57
- let error;
58
- try {
59
- tileData = await getData({ index, id, bbox, userData, zoom, signal });
60
- }
61
- catch (err) {
62
- error = err || true;
63
- }
64
- finally {
65
- requestToken.done();
66
- }
67
- // If loadData has been called with a newer version, discard the result from this operation
68
- if (loaderId !== this._loaderId) {
69
- return;
70
- }
71
- // Clear the `isLoading` flag
72
- this._loader = undefined;
73
- // Rewrite tile content with the result of getTileData if successful, or `null` in case of
74
- // error or cancellation
75
- this.content = tileData;
76
- // If cancelled, do not invoke the callbacks
77
- // Consider it loaded if we tried to cancel but `getTileData` still returned data
78
- if (this._isCancelled && !tileData) {
79
- this._isLoaded = false;
80
- return;
81
- }
82
- this._isLoaded = true;
83
- this._isCancelled = false;
84
- if (error) {
85
- onError(error, this);
86
- }
87
- else {
88
- onLoad(this);
89
- }
90
- }
91
- loadData(opts) {
92
- this._isLoaded = false;
93
- this._isCancelled = false;
94
- this._needsReload = false;
95
- this._loaderId++;
96
- this._loader = this._loadData(opts);
97
- return this._loader;
98
- }
99
- setNeedsReload() {
100
- if (this.isLoading) {
101
- this.abort();
102
- this._loader = undefined;
103
- }
104
- this._needsReload = true;
105
- }
106
- abort() {
107
- if (this.isLoaded) {
108
- return;
109
- }
110
- this._isCancelled = true;
111
- this._abortController?.abort();
112
- }
113
- }