-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmvt-layer.ts
489 lines (409 loc) · 14.4 KB
/
mvt-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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {
Layer,
LayersList,
log,
PickingInfo,
UpdateParameters,
GetPickingInfoParams,
Viewport,
COORDINATE_SYSTEM,
DefaultProps
} from '@deck.gl/core';
import {GeoJsonLayer, GeoJsonLayerProps} from '@deck.gl/layers';
import {ClipExtension} from '@deck.gl/extensions';
import {Matrix4} from '@math.gl/core';
import {MVTWorkerLoader} from '@loaders.gl/mvt';
import {binaryToGeojson} from '@loaders.gl/gis';
import type {Loader} from '@loaders.gl/loader-utils';
import type {BinaryFeatureCollection} from '@loaders.gl/schema';
import type {Feature, Geometry} from 'geojson';
import {transform} from './coordinate-transform';
import findIndexBinary from './find-index-binary';
import TileLayer, {TileLayerPickingInfo, TileLayerProps} from '../tile-layer/tile-layer';
import type {Tileset2DProps, TileLoadProps, GeoBoundingBox} from '../tileset-2d/index';
import {
urlType,
Tileset2D,
Tile2DHeader,
getURLFromTemplate,
URLTemplate,
isGeoBoundingBox,
isURLTemplate
} from '../tileset-2d/index';
const WORLD_SIZE = 512;
const defaultProps: DefaultProps<MVTLayerProps> = {
...GeoJsonLayer.defaultProps,
data: urlType,
onDataLoad: {type: 'function', value: null, optional: true, compare: false},
uniqueIdProperty: '',
highlightedFeatureId: null,
loaders: [MVTWorkerLoader],
binary: true
};
export type TileJson = {
tilejson: string;
tiles: string[];
// eslint-disable-next-line camelcase
vector_layers: any[];
attribution?: string;
scheme?: string;
maxzoom?: number;
minzoom?: number;
version?: string;
};
type ParsedMvtTile = Feature[] | BinaryFeatureCollection;
export type MVTLayerPickingInfo<FeaturePropertiesT = {}> = TileLayerPickingInfo<
ParsedMvtTile,
PickingInfo<Feature<Geometry, FeaturePropertiesT>>
>;
/** All props supported by the MVTLayer */
export type MVTLayerProps<FeaturePropertiesT = unknown> = _MVTLayerProps<FeaturePropertiesT> &
Omit<TileLayerProps<ParsedMvtTile>, 'data'>;
/** Props added by the MVTLayer */
export type _MVTLayerProps<FeaturePropertiesT> = Omit<
GeoJsonLayerProps<FeaturePropertiesT>,
'data'
> & {
data: TileJson | URLTemplate;
/** Called if `data` is a TileJSON URL when it is successfully fetched. */
onDataLoad?: ((tilejson: TileJson | null) => void) | null;
/** Needed for highlighting a feature split across two or more tiles. */
uniqueIdProperty?: string;
/** A feature with ID corresponding to the supplied value will be highlighted. */
highlightedFeatureId?: string | number | null;
/**
* Use tile data in binary format.
*
* @default true
*/
binary?: boolean;
/**
* Loaders used to transform tiles into `data` property passed to `renderSubLayers`.
*
* @default [MVTWorkerLoader] from `@loaders.gl/mvt`
*/
loaders?: Loader[];
};
type ContentWGS84Cache = {_contentWGS84?: Feature[]};
/** Render data formatted as [Mapbox Vector Tiles](https://docs.mapbox.com/vector-tiles/specification/). */
export default class MVTLayer<
FeaturePropertiesT = any,
ExtraProps extends {} = {}
> extends TileLayer<ParsedMvtTile, Required<_MVTLayerProps<FeaturePropertiesT>> & ExtraProps> {
static layerName = 'MVTLayer';
static defaultProps = defaultProps;
state!: TileLayer<ParsedMvtTile>['state'] & {
binary: boolean;
data: URLTemplate;
tileJSON: TileJson | null;
highlightColor?: number[];
hoveredFeatureId: number | string | null;
hoveredFeatureLayerName: string | null;
};
initializeState(): void {
super.initializeState();
// GlobeView doesn't work well with binary data
const binary = this.context.viewport.resolution !== undefined ? false : this.props.binary;
this.setState({
binary,
data: null,
tileJSON: null,
hoveredFeatureId: null,
hoveredFeatureLayerName: null
});
}
get isLoaded(): boolean {
return Boolean(this.state?.data && super.isLoaded);
}
updateState({props, oldProps, context, changeFlags}: UpdateParameters<this>) {
if (changeFlags.dataChanged) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._updateTileData();
}
if (this.state?.data) {
super.updateState({props, oldProps, context, changeFlags});
this._setWGS84PropertyForTiles();
}
const {highlightColor} = props;
if (highlightColor !== oldProps.highlightColor && Array.isArray(highlightColor)) {
this.setState({highlightColor});
}
}
/* eslint-disable complexity */
private async _updateTileData(): Promise<void> {
let data = this.props.data;
let tileJSON: TileJson | null = null;
if (typeof data === 'string' && !isURLTemplate(data)) {
const {onDataLoad, fetch} = this.props;
this.setState({data: null, tileJSON: null});
try {
tileJSON = await fetch(data, {propName: 'data', layer: this, loaders: []});
} catch (error: any) {
this.raiseError(error, 'loading TileJSON');
data = null;
}
if (onDataLoad) {
onDataLoad(tileJSON, {propName: 'data', layer: this});
}
} else if (data && typeof data === 'object' && 'tilejson' in data) {
tileJSON = data;
}
if (tileJSON) {
data = tileJSON.tiles;
}
this.setState({data, tileJSON});
}
_getTilesetOptions(): Tileset2DProps {
const opts = super._getTilesetOptions();
const tileJSON: TileJson | null = this.state.tileJSON;
const {minZoom, maxZoom} = this.props;
if (tileJSON) {
if (Number.isFinite(tileJSON.minzoom) && (tileJSON.minzoom as number) > (minZoom as number)) {
opts.minZoom = tileJSON.minzoom;
}
if (
Number.isFinite(tileJSON.maxzoom) &&
(!Number.isFinite(maxZoom) || (tileJSON.maxzoom as number) < (maxZoom as number))
) {
opts.maxZoom = tileJSON.maxzoom;
}
}
return opts;
}
/* eslint-disable complexity */
renderLayers(): Layer | null | LayersList {
if (!this.state?.data) return null;
return super.renderLayers();
}
getTileData(loadProps: TileLoadProps): Promise<ParsedMvtTile> {
const {data, binary} = this.state;
const {index, signal} = loadProps;
const url = getURLFromTemplate(data, loadProps);
if (!url) {
return Promise.reject('Invalid URL');
}
let loadOptions = this.getLoadOptions();
const {fetch} = this.props;
loadOptions = {
...loadOptions,
mimeType: 'application/x-protobuf',
mvt: {
...loadOptions?.mvt,
coordinates: this.context.viewport.resolution ? 'wgs84' : 'local',
tileIndex: index
// Local worker debug
// workerUrl: `modules/mvt/dist/mvt-loader.worker.js`
// Set worker to null to skip web workers
// workerUrl: null
},
gis: binary ? {format: 'binary'} : {}
};
return fetch(url, {propName: 'data', layer: this, loadOptions, signal});
}
renderSubLayers(
props: TileLayer['props'] & {
id: string;
data: ParsedMvtTile;
_offset: number;
tile: Tile2DHeader<ParsedMvtTile>;
}
): Layer | null | LayersList {
const {x, y, z} = props.tile.index;
const worldScale = Math.pow(2, z);
const xScale = WORLD_SIZE / worldScale;
const yScale = -xScale;
const xOffset = (WORLD_SIZE * x) / worldScale;
const yOffset = WORLD_SIZE * (1 - y / worldScale);
const modelMatrix = new Matrix4().scale([xScale, yScale, 1]);
props.autoHighlight = false;
if (!this.context.viewport.resolution) {
props.modelMatrix = modelMatrix;
props.coordinateOrigin = [xOffset, yOffset, 0];
props.coordinateSystem = COORDINATE_SYSTEM.CARTESIAN;
props.extensions = [...(props.extensions || []), new ClipExtension()];
}
const subLayers = super.renderSubLayers(props);
if (this.state.binary && !(subLayers instanceof GeoJsonLayer)) {
log.warn('renderSubLayers() must return GeoJsonLayer when using binary:true')();
}
return subLayers;
}
protected _updateAutoHighlight(info: PickingInfo): void {
const {uniqueIdProperty} = this.props;
const {hoveredFeatureId, hoveredFeatureLayerName} = this.state;
const hoveredFeature = info.object;
let newHoveredFeatureId: string | number | null = null;
let newHoveredFeatureLayerName: string | null = null;
if (hoveredFeature) {
newHoveredFeatureId = getFeatureUniqueId(hoveredFeature, uniqueIdProperty);
newHoveredFeatureLayerName = getFeatureLayerName(hoveredFeature);
}
let {highlightColor} = this.props;
if (typeof highlightColor === 'function') {
highlightColor = highlightColor(info);
}
if (
hoveredFeatureId !== newHoveredFeatureId ||
hoveredFeatureLayerName !== newHoveredFeatureLayerName
) {
this.setState({
highlightColor,
hoveredFeatureId: newHoveredFeatureId,
hoveredFeatureLayerName: newHoveredFeatureLayerName
});
}
}
protected _isWGS84(): boolean {
return Boolean(this.context.viewport.resolution);
}
getPickingInfo(params: GetPickingInfoParams): MVTLayerPickingInfo<FeaturePropertiesT> {
const info = super.getPickingInfo(params);
if (this.state.binary && info.index !== -1) {
const {data} = params.sourceLayer!.props;
info.object = binaryToGeojson(data as BinaryFeatureCollection, {
globalFeatureId: info.index
}) as Feature;
}
if (info.object && !this._isWGS84()) {
info.object = transformTileCoordsToWGS84(
info.object,
info.tile!.bbox as GeoBoundingBox, // eslint-disable-line
this.context.viewport
);
}
return info;
}
getSubLayerPropsByTile(tile: Tile2DHeader<ParsedMvtTile>): Record<string, any> {
return {
highlightedObjectIndex: this.getHighlightedObjectIndex(tile),
highlightColor: this.state.highlightColor
};
}
private getHighlightedObjectIndex(tile: Tile2DHeader<ParsedMvtTile>): number {
const {hoveredFeatureId, hoveredFeatureLayerName, binary} = this.state;
const {uniqueIdProperty, highlightedFeatureId} = this.props;
const data = tile.content;
const isHighlighted = isFeatureIdDefined(highlightedFeatureId);
const isFeatureIdPresent = isFeatureIdDefined(hoveredFeatureId) || isHighlighted;
if (!isFeatureIdPresent) {
return -1;
}
const featureIdToHighlight = isHighlighted ? highlightedFeatureId! : hoveredFeatureId!;
// Iterable data
if (Array.isArray(data)) {
return data.findIndex(feature => {
const isMatchingId = getFeatureUniqueId(feature, uniqueIdProperty) === featureIdToHighlight;
const isMatchingLayer =
isHighlighted || getFeatureLayerName(feature) === hoveredFeatureLayerName;
return isMatchingId && isMatchingLayer;
});
// Non-iterable data
} else if (data && binary) {
// Get the feature index of the selected item to highlight
return findIndexBinary(
data,
uniqueIdProperty,
featureIdToHighlight,
isHighlighted ? '' : hoveredFeatureLayerName!
);
}
return -1;
}
private _pickObjects(maxObjects: number | null): PickingInfo[] {
const {deck, viewport} = this.context;
const width = viewport.width;
const height = viewport.height;
const x = viewport.x;
const y = viewport.y;
const layerIds = [this.id];
return deck!.pickObjects({x, y, width, height, layerIds, maxObjects});
}
/** Get the rendered features in the current viewport. */
getRenderedFeatures(maxFeatures: number | null = null): Feature[] {
const features = this._pickObjects(maxFeatures);
const featureCache = new Set();
const renderedFeatures: Feature[] = [];
for (const f of features) {
const featureId = getFeatureUniqueId(f.object, this.props.uniqueIdProperty);
if (featureId === undefined) {
// we have no id for the feature, we just add to the list
renderedFeatures.push(f.object as Feature);
} else if (!featureCache.has(featureId)) {
// Add removing duplicates
featureCache.add(featureId);
renderedFeatures.push(f.object as Feature);
}
}
return renderedFeatures;
}
private _setWGS84PropertyForTiles(): void {
const propName = 'dataInWGS84';
const tileset: Tileset2D = this.state.tileset!;
// @ts-expect-error selectedTiles are always initialized when tile is being processed
tileset.selectedTiles.forEach((tile: Tile2DHeader & ContentWGS84Cache) => {
if (!tile.hasOwnProperty(propName)) {
// eslint-disable-next-line accessor-pairs
Object.defineProperty(tile, propName, {
get: () => {
// Still loading or encountered an error
if (!tile.content) {
return null;
}
if (this.state.binary && Array.isArray(tile.content) && !tile.content.length) {
// TODO: @loaders.gl/mvt returns [] when no content. It should return a valid empty binary.
// https://github.com/visgl/loaders.gl/pull/1137
return [];
}
const {bbox} = tile;
if (tile._contentWGS84 === undefined && isGeoBoundingBox(bbox)) {
// Create a cache to transform only once
const content = this.state.binary ? binaryToGeojson(tile.content) : tile.content;
tile._contentWGS84 = content.map(feature =>
transformTileCoordsToWGS84(feature, bbox, this.context.viewport)
);
}
return tile._contentWGS84;
}
});
}
});
}
}
function getFeatureUniqueId(feature: Feature, uniqueIdProperty: string | undefined) {
if (feature.properties && uniqueIdProperty) {
return feature.properties[uniqueIdProperty];
}
if ('id' in feature) {
return feature.id;
}
return undefined;
}
function getFeatureLayerName(feature: Feature): string | null {
return feature.properties?.layerName || null;
}
function isFeatureIdDefined(value: unknown): boolean {
return value !== undefined && value !== null && value !== '';
}
function transformTileCoordsToWGS84(
object: Feature,
bbox: GeoBoundingBox,
viewport: Viewport
): Feature {
const feature = {
...object,
geometry: {
type: object.geometry.type
}
};
// eslint-disable-next-line accessor-pairs
Object.defineProperty(feature.geometry, 'coordinates', {
get: () => {
const wgs84Geom = transform(object.geometry, bbox, viewport);
return wgs84Geom.coordinates;
}
});
return feature as Feature;
}