-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtile-layer.ts
406 lines (351 loc) · 10.8 KB
/
tile-layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {
CompositeLayer,
CompositeLayerProps,
Layer,
LayerProps,
UpdateParameters,
PickingInfo,
GetPickingInfoParams,
DefaultProps,
FilterContext,
_flatten as flatten
} from '@deck.gl/core';
import {GeoJsonLayer} from '@deck.gl/layers';
import {LayersList} from '@deck.gl/core';
import type {TileLoadProps, ZRange} from '../tileset-2d/index';
import {
Tileset2D,
Tile2DHeader,
RefinementStrategy,
STRATEGY_DEFAULT,
Tileset2DProps
} from '../tileset-2d/index';
import {urlType, URLTemplate, getURLFromTemplate} from '../tileset-2d/index';
const defaultProps: DefaultProps<TileLayerProps> = {
TilesetClass: Tileset2D,
data: {type: 'data', value: []},
dataComparator: urlType.equal,
renderSubLayers: {type: 'function', value: (props: any) => new GeoJsonLayer(props)},
getTileData: {type: 'function', optional: true, value: null},
// TODO - change to onViewportLoad to align with Tile3DLayer
onViewportLoad: {type: 'function', optional: true, value: null},
onTileLoad: {type: 'function', value: tile => {}},
onTileUnload: {type: 'function', value: tile => {}},
// eslint-disable-next-line
onTileError: {type: 'function', value: err => console.error(err)},
extent: {type: 'array', optional: true, value: null, compare: true},
tileSize: 512,
maxZoom: null,
minZoom: 0,
maxCacheSize: null,
maxCacheByteSize: null,
refinementStrategy: STRATEGY_DEFAULT,
zRange: null,
maxRequests: 6,
debounceTime: 0,
zoomOffset: 0
};
/** All props supported by the TileLayer */
export type TileLayerProps<DataT = unknown> = CompositeLayerProps & _TileLayerProps<DataT>;
/** Props added by the TileLayer */
type _TileLayerProps<DataT> = {
data: URLTemplate;
/**
* Optionally implement a custom indexing scheme.
*/
TilesetClass?: typeof Tileset2D;
/**
* Renders one or an array of Layer instances.
*/
renderSubLayers?: (
props: TileLayerProps<DataT> & {
id: string;
data: DataT;
_offset: number;
tile: Tile2DHeader<DataT>;
}
) => Layer | null | LayersList;
/**
* If supplied, `getTileData` is called to retrieve the data of each tile.
*/
getTileData?: ((props: TileLoadProps) => Promise<DataT> | DataT) | null;
/** Called when all tiles in the current viewport are loaded. */
onViewportLoad?: ((tiles: Tile2DHeader<DataT>[]) => void) | null;
/** Called when a tile successfully loads. */
onTileLoad?: (tile: Tile2DHeader<DataT>) => void;
/** Called when a tile is cleared from cache. */
onTileUnload?: (tile: Tile2DHeader<DataT>) => void;
/** Called when a tile failed to load. */
onTileError?: (err: any, tile?) => void;
/** The bounding box of the layer's data. */
extent?: number[] | null;
/** The pixel dimension of the tiles, usually a power of 2. */
tileSize?: number;
/** The max zoom level of the layer's data.
* @default null
*/
maxZoom?: number | null;
/** The min zoom level of the layer's data.
* @default 0
*/
minZoom?: number | null;
/** The maximum number of tiles that can be cached. */
maxCacheSize?: number | null;
/**
* The maximum memory used for caching tiles.
*
* @default null
*/
maxCacheByteSize?: number | null;
/**
* How the tile layer refines the visibility of tiles.
*
* @default 'best-available'
*/
refinementStrategy?: RefinementStrategy;
/** Range of minimum and maximum heights in the tile. */
zRange?: ZRange | null;
/**
* The maximum number of concurrent getTileData calls.
*
* @default 6
*/
maxRequests?: number;
/**
* Queue tile requests until no new tiles have been requested for at least `debounceTime` milliseconds.
*
* @default 0
*/
debounceTime?: number;
/**
* This offset changes the zoom level at which the tiles are fetched.
*
* Needs to be an integer.
*
* @default 0
*/
zoomOffset?: number;
};
export type TileLayerPickingInfo<
DataT = any,
SubLayerPickingInfo = PickingInfo
> = SubLayerPickingInfo & {
/** The picked tile */
tile?: Tile2DHeader<DataT>;
/** the tile that emitted the picking event */
sourceTile: Tile2DHeader<DataT>;
/** a layer created by props.renderSubLayer() that emitted the picking event */
sourceTileSubLayer: Layer;
};
/**
* The TileLayer is a composite layer that makes it possible to visualize very large datasets.
*
* Instead of fetching the entire dataset, it only loads and renders what's visible in the current viewport.
*/
export default class TileLayer<DataT = any, ExtraPropsT extends {} = {}> extends CompositeLayer<
ExtraPropsT & Required<_TileLayerProps<DataT>>
> {
static defaultProps: DefaultProps = defaultProps;
static layerName = 'TileLayer';
state!: {
tileset: Tileset2D | null;
isLoaded: boolean;
frameNumber?: number;
};
initializeState() {
this.state = {
tileset: null,
isLoaded: false
};
}
finalizeState() {
this.state?.tileset?.finalize();
}
get isLoaded(): boolean {
return Boolean(
this.state?.tileset?.selectedTiles?.every(
tile => tile.isLoaded && tile.layers && tile.layers.every(layer => layer.isLoaded)
)
);
}
shouldUpdateState({changeFlags}): boolean {
return changeFlags.somethingChanged;
}
updateState({changeFlags}: UpdateParameters<this>) {
let {tileset} = this.state;
const propsChanged = changeFlags.propsOrDataChanged || changeFlags.updateTriggersChanged;
const dataChanged =
changeFlags.dataChanged ||
(changeFlags.updateTriggersChanged &&
(changeFlags.updateTriggersChanged.all || changeFlags.updateTriggersChanged.getTileData));
if (!tileset) {
tileset = new this.props.TilesetClass(this._getTilesetOptions());
this.setState({tileset});
} else if (propsChanged) {
tileset.setOptions(this._getTilesetOptions());
if (dataChanged) {
// reload all tiles
// use cached layers until new content is loaded
tileset.reloadAll();
} else {
// some render options changed, regenerate sub layers now
tileset.tiles.forEach(tile => {
tile.layers = null;
});
}
}
this._updateTileset();
}
_getTilesetOptions(): Tileset2DProps {
const {
tileSize,
maxCacheSize,
maxCacheByteSize,
refinementStrategy,
extent,
maxZoom,
minZoom,
maxRequests,
debounceTime,
zoomOffset
} = this.props;
return {
maxCacheSize,
maxCacheByteSize,
maxZoom,
minZoom,
tileSize,
refinementStrategy,
extent,
maxRequests,
debounceTime,
zoomOffset,
getTileData: this.getTileData.bind(this),
onTileLoad: this._onTileLoad.bind(this),
onTileError: this._onTileError.bind(this),
onTileUnload: this._onTileUnload.bind(this)
};
}
private _updateTileset(): void {
const tileset = this.state.tileset!;
const {zRange, modelMatrix} = this.props;
const frameNumber = tileset.update(this.context.viewport, {zRange, modelMatrix});
const {isLoaded} = tileset;
const loadingStateChanged = this.state.isLoaded !== isLoaded;
const tilesetChanged = this.state.frameNumber !== frameNumber;
if (isLoaded && (loadingStateChanged || tilesetChanged)) {
this._onViewportLoad();
}
if (tilesetChanged) {
// Save the tileset frame number - trigger a rerender
this.setState({frameNumber});
}
// Save the loaded state - should not trigger a rerender
this.state.isLoaded = isLoaded;
}
_onViewportLoad(): void {
const {tileset} = this.state;
const {onViewportLoad} = this.props;
if (onViewportLoad) {
// This method can only be called when tileset is defined and updated
onViewportLoad(tileset!.selectedTiles!);
}
}
_onTileLoad(tile: Tile2DHeader<DataT>): void {
this.props.onTileLoad(tile);
tile.layers = null;
this.setNeedsUpdate();
}
_onTileError(error: any, tile: Tile2DHeader<DataT>) {
this.props.onTileError(error);
tile.layers = null;
this.setNeedsUpdate();
}
_onTileUnload(tile: Tile2DHeader<DataT>) {
this.props.onTileUnload(tile);
}
// Methods for subclass to override
getTileData(tile: TileLoadProps): Promise<DataT> | DataT | null {
const {data, getTileData, fetch} = this.props;
const {signal} = tile;
tile.url =
typeof data === 'string' || Array.isArray(data) ? getURLFromTemplate(data, tile) : null;
if (getTileData) {
return getTileData(tile);
}
if (fetch && tile.url) {
return fetch(tile.url, {propName: 'data', layer: this, signal});
}
return null;
}
renderSubLayers(
props: TileLayer['props'] & {
id: string;
data: DataT;
_offset: number;
tile: Tile2DHeader<DataT>;
}
): Layer | null | LayersList {
return this.props.renderSubLayers(props);
}
getSubLayerPropsByTile(tile: Tile2DHeader): Partial<LayerProps> | null {
return null;
}
getPickingInfo(params: GetPickingInfoParams): TileLayerPickingInfo<DataT> {
// TileLayer does not directly render anything, sourceLayer cannot be null
const sourceLayer = params.sourceLayer!;
const sourceTile: Tile2DHeader<DataT> = (sourceLayer.props as any).tile;
const info = params.info as TileLayerPickingInfo<DataT>;
if (info.picked) {
info.tile = sourceTile;
}
info.sourceTile = sourceTile;
info.sourceTileSubLayer = sourceLayer;
return info;
}
protected _updateAutoHighlight(info: TileLayerPickingInfo<DataT>): void {
info.sourceTileSubLayer.updateAutoHighlight(info);
}
renderLayers(): Layer | null | LayersList {
return this.state.tileset!.tiles.map((tile: Tile2DHeader) => {
const subLayerProps = this.getSubLayerPropsByTile(tile);
// cache the rendered layer in the tile
if (!tile.isLoaded && !tile.content) {
// nothing to show
} else if (!tile.layers) {
const layers = this.renderSubLayers({
...this.props,
...this.getSubLayerProps({
id: tile.id,
updateTriggers: this.props.updateTriggers
}),
data: tile.content,
_offset: 0,
tile
});
tile.layers = (flatten(layers, Boolean) as Layer<{tile?: Tile2DHeader}>[]).map(layer =>
layer.clone({
tile,
...subLayerProps
})
);
} else if (
subLayerProps &&
tile.layers[0] &&
Object.keys(subLayerProps).some(
propName => tile.layers![0].props[propName] !== subLayerProps[propName]
)
) {
tile.layers = tile.layers.map(layer => layer.clone(subLayerProps));
}
return tile.layers;
});
}
filterSubLayer({layer, cullRect}: FilterContext) {
const {tile} = (layer as Layer<{tile: Tile2DHeader}>).props;
return this.state.tileset!.isTileVisible(tile, cullRect);
}
}