forked from heremaps/harp.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSource.ts
728 lines (650 loc) · 22.3 KB
/
DataSource.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
* Copyright (C) 2023- XYZ maps contributors
* Copyright (C) 2019-2021 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import { Theme, ValueMap } from "@xyzmaps/harp-datasource-protocol";
import { ExprPool } from "@xyzmaps/harp-datasource-protocol/lib/ExprPool";
import { Projection, TileKey, TilingScheme } from "@xyzmaps/harp-geoutils";
import { assert, LoggerManager } from "@xyzmaps/harp-utils";
import * as THREE from "three";
import { MapView } from "./MapView";
import { Tile } from "./Tile";
const logger = LoggerManager.instance.create("DataSource");
/**
* Options for a {@link DataSource}.
*/
export interface DataSourceOptions {
/**
* The unique name of a {@link DataSource} instance.
*/
name?: string;
/**
* The name of the [[StyleSet]] to evaluate for the decoding.
*/
styleSetName?: string;
/**
* Used to configure the languages used by the `DataSource` according to priority;
* the first language in the array has the highest priority.
*
* An array of ISO 639-1 language codes.
*/
languages?: string[];
/**
* The minimum zoom level at which data is available or displayed at
* (depending on {@link DataSource} subclass).
* @deprecated Use [[minDataLevel]] and [[minDisplayLevel]] instead.
*/
minZoomLevel?: number;
/**
* The maximum zoom level at which data is available or displayed at
* (depending on {@link DataSource} subclass).
* @deprecated Use [[maxDataLevel]] and [[maxDisplayLevel]] instead.
*/
maxZoomLevel?: number;
/**
* The minimum zoom level at which data is available.
*/
minDataLevel?: number;
/**
* The maximum zoom level at which data is available.
*/
maxDataLevel?: number;
/**
* The minimum zoom level at which {@link DataSource} is displayed.
*/
minDisplayLevel?: number;
/**
* The maximum zoom level at which {@link DataSource} is displayed.
*/
maxDisplayLevel?: number;
/**
* Storage level offset applied to this `DataSource`.
*/
storageLevelOffset?: number;
/**
* Whether the datasource can overlap tiles. Such overlapping is necessary when zooming out and
* waiting for the tiles to load, in this case, we use cached tiles to fill the missing gaps if
* available (and in some cases, the tiles can overlap, i.e. for example when a child is next
* to a parent, the parent is rendered beneath the child), however for some datasources (those
* that produce transparent tiles for example), this gives bad results, and as such, it should
* be disabled to reduce flickering. Another way to put it is that loading tiles are replaced
* with cached tiles and we then fall (back/forward) to the next appropriate zoom level.
* @default true
*/
allowOverlappingTiles?: boolean;
/**
* Whether features from these data source can picked by calling
* {@link MapView.intersectMapObjects}. Disabling picking for data sources that don't need it
* will improve picking performance.
* @default true
*/
enablePicking?: boolean;
/**
* Maximum geometry height above ground level this {@link DataSource} can produce.
*
* @remarks
* Used in first stage of frustum culling before {@link Tile#maxGeometryHeight} data is
* available.
*
* @default [[EarthConstants.MAX_BUILDING_HEIGHT]].
*/
maxGeometryHeight?: number;
/**
* Minimum geometry height below ground level this {@link DataSource} can produce. Negative
* values describe height below ground.
*
* @remarks
* Used in first stage of frustum culling before {@link Tile#minGeometryHeight} data is
* available.
*
* @default `0`.
*/
minGeometryHeight?: number;
/**
* Number used to order [DataSource]'s relative to each other, see
* {@link DataSource.dataSourceOrder}
*/
dataSourceOrder?: number;
}
/**
* Derive a class from `DataSource` to contribute data and geometries to the {@link MapView}.
*/
export abstract class DataSource extends THREE.EventDispatcher {
/**
* Keep the update event here to avoid a global reference to the datasource (and thus prevent garbage collection).
*/
private readonly UPDATE_EVENT = { type: "update" };
/**
* A counter to generate unique names for each `DataSource`, if no name is provided in the
* constructor.
*/
private static uniqueNameCounter: number = 0;
/**
* Set to `true` if this `DataSource` is enabled; `false` otherwise.
*/
enabled: boolean = true;
/**
* Set to `true` if the {@link MapView} can cache tiles produced by this `DataSource`.
*/
cacheable: boolean = false;
/**
* Set to `true` if the loader should be used to get the tile contents.
*/
useGeometryLoader: boolean = false;
/**
* The unique name of a `DataSource` instance.
*/
name: string;
/**
* Whether the datasource should have a ground plane (this plane covers the tile entirely and
* has the minimum possible renderOrder), this can be required in some cases when fallback
* parent tiles need to be covered by the children, otherwise the content will overlap.
* Default is false
*/
addGroundPlane: boolean = false;
/**
* The minimum zoom level at which data is available.
*/
minDataLevel: number = 1;
/**
* The maximum zoom level at which data is available.
*/
maxDataLevel: number = 20;
/**
* The minimum zoom level at which {@link DataSource} is displayed.
*/
minDisplayLevel: number = 1;
/**
* The maximum zoom level at which {@link DataSource} is displayed.
*/
maxDisplayLevel: number = 20;
allowOverlappingTiles: boolean = true;
enablePicking: boolean = true;
/**
* Overrides the default rendering order of this `DataSource`.
*
* @remarks
* When `dataSourceOrder` is defined, all the objects created by this `DataSource`
* will be rendered on top of the objects created by other `DataSource`s with
* lower `dataSourceOrder` values.
*
* @defaultValue undefined
*/
dataSourceOrder: number = 0;
/**
* @internal
* @hidden
*/
readonly exprPool = new ExprPool();
/**
* The {@link MapView} instance holding a reference to this `DataSource`.
*/
private m_mapView?: MapView;
/**
* The name of the [[StyleSet]] to evaluate for the decoding.
*/
private m_styleSetName?: string;
/**
* Current value of [[maxGeometryHeight]] property.
*/
private m_maxGeometryHeight = 0;
/**
* Current value of [[minGeometryHeight]] property.
*/
private m_minGeometryHeight = 0;
/**
* Storage level offset applied to this `DataSource`.
*/
private m_storageLevelOffset: number = 0;
private readonly m_featureStateMap = new Map<number | string, ValueMap>();
/**
* An array of ISO 639-1 language codes.
*/
protected languages?: string[];
/**
* Constructs a new `DataSource`.
*
* @param options - The options to create the data source.
*/
constructor(options: DataSourceOptions = {}) {
super();
let { name } = options;
const {
styleSetName,
languages,
minZoomLevel,
maxZoomLevel,
minDataLevel,
maxDataLevel,
minDisplayLevel,
maxDisplayLevel,
storageLevelOffset,
allowOverlappingTiles,
enablePicking,
minGeometryHeight,
maxGeometryHeight,
dataSourceOrder
} = options;
if (name === undefined || name.length === 0) {
name = `anonymous-datasource#${++DataSource.uniqueNameCounter}`;
}
this.name = name;
this.styleSetName = styleSetName;
if (languages !== undefined) {
this.languages = languages;
}
if (minDataLevel !== undefined) {
this.minDataLevel = minDataLevel;
}
if (maxDataLevel !== undefined) {
this.maxDataLevel = maxDataLevel;
}
if (minZoomLevel !== undefined) {
this.minZoomLevel = minZoomLevel;
}
if (maxZoomLevel !== undefined) {
this.maxZoomLevel = maxZoomLevel;
}
if (minDisplayLevel !== undefined) {
this.minDisplayLevel = minDisplayLevel;
}
if (maxDisplayLevel !== undefined) {
this.maxDisplayLevel = maxDisplayLevel;
}
if (storageLevelOffset !== undefined) {
this.m_storageLevelOffset = storageLevelOffset;
}
if (allowOverlappingTiles !== undefined) {
this.allowOverlappingTiles = allowOverlappingTiles;
}
if (enablePicking !== undefined) {
this.enablePicking = enablePicking;
}
if (minGeometryHeight !== undefined) {
this.minGeometryHeight = minGeometryHeight;
}
if (maxGeometryHeight !== undefined) {
this.maxGeometryHeight = maxGeometryHeight;
}
if (dataSourceOrder) {
this.dataSourceOrder = dataSourceOrder;
}
}
/**
* Gets the state of the given feature id.
*
* @param featureId - The id of the feature. Id numbers are deprecated in favor of strings.
*/
getFeatureState(featureId: number | string): ValueMap | undefined {
return this.m_featureStateMap.get(featureId);
}
/**
* Clears the state of all the features of this {@link DataSource}.
*/
clearFeatureState() {
this.m_featureStateMap.clear();
}
/**
* Sets the state of the given feature id.
*
* ```typescript
* dataSource.setFeatureState(featureId, { enabled: true });
* ```
*
* @param featureId - The id of the feature. Id numbers are deprecated in favor of strings.
* @param state - The new state of the feature.
*/
setFeatureState(featureId: number | string, state: ValueMap) {
this.m_featureStateMap.set(featureId, state);
}
/**
* Removes the state associated to the given feature.
*
* @param featureId - The id of the feature. Id numbers are deprecated in favor of strings.
*/
removeFeatureState(featureId: number | string) {
this.m_featureStateMap.delete(featureId);
}
/**
* Returns the name of the [[StyleSet]] to use for the decoding.
*/
get styleSetName(): string | undefined {
return this.m_styleSetName;
}
/**
* Sets the name of the [[StyleSet]] to use for the decoding.
* If this {@link DataSource} is already
* attached to a {@link MapView}, this setter then reapplies
* [[StyleSet]] with this name found in
* {@link MapView}s theme.
*/
set styleSetName(styleSetName: string | undefined) {
if (styleSetName !== this.m_styleSetName) {
this.m_styleSetName = styleSetName;
this.clearCache();
this.requestUpdate();
}
}
/**
* Destroys this `DataSource`.
*/
dispose() {
// to be overloaded by subclasses
}
/**
* Purges all the caching done by this `DataSource`
*/
clearCache() {
// to be overloaded by subclasses
}
/**
* Boolean which says whether a {@link DataSource} produces
* tiles that fully cover the tile, i.e.
* tiles underneath are completely hidden. Must be
* overridden for {@link DataSource}'s that don't
* have a ground plane, but which still fully
* cover the tile, e.g. web tiles.
*/
isFullyCovering(): boolean {
return this.addGroundPlane;
}
/**
* Returns `true` if this `DataSource` is ready
* and the {@link MapView} can invoke `getTile()` to
* start requesting data.
*/
ready(): boolean {
return true;
}
/**
* The {@link MapView} that is holding this `DataSource`.
*/
get mapView(): MapView {
if (this.m_mapView === undefined) {
throw new Error("This DataSource was not added to MapView");
}
return this.m_mapView;
}
/**
* The {@link @xyzmaps/harp-geoutils#Projection} used by
* the {@link MapView} that is holding this `DataSource`.
*
* An `Error` is thrown if you call this method
* before this `DataSource` has been added
* to a {@link MapView}.
*/
get projection(): Projection {
return this.mapView.projection;
}
/**
* This method is called when the `DataSource` is added to a {@link MapView}. Override this
* method to provide any custom initialization, such as, to establish a network connection,
* or to initialize complex data structures.
*/
async connect(): Promise<void> {
// to be overloaded by subclasses
}
/**
* Returns the {@link @xyzmaps/harp-geoutils#TilingScheme} used by this `DataSource`.
*/
abstract getTilingScheme(): TilingScheme;
/**
* This method is called when this `DataSource` is added to a {@link MapView}.
*
* Overrides of this method must invoke the definition of the super class.
*
* @param mapView - The instance of the {@link MapView}.
*/
attach(mapView: MapView): void {
this.m_mapView = mapView;
}
/**
* This method is called when this `DataSource` is removed from a {@link MapView}.
*
* Overrides of this method must invoke the definition of the super class.
*
* @param mapView - The instance of the {@link MapView}.
*/
detach(mapView: MapView) {
assert(this.m_mapView === mapView);
this.m_mapView = undefined;
}
/**
* @return Whether this `DataSource` is detached from the `MapView`
*/
isDetached() {
return this.m_mapView === undefined;
}
/**
* Apply the {@link @xyzmaps/harp-datasource-protocol#Theme} to this data source.
*
* If `DataSource` depends on a `styleSet` defined by this theme or `languages`, it must update
* its tiles' geometry.
*
* @param theme - The Theme to be applied
*/
async setTheme(theme: Theme): Promise<void>;
/**
* Apply the {@link @xyzmaps/harp-datasource-protocol#Theme} to this data source.
*
* If `DataSource` depends on a `styleSet` defined by this theme or `languages`, it must update
* its tiles' geometry.
*
* @param theme - The Theme to be applied
* @param languages - optional: The languages in priority order to be applied
*
* @deprecated use setTheme( Theme ) and setLanguages(string[]) instead
*/
async setTheme(theme: Theme, languages?: string[]): Promise<void> {
// to be overwritten by subclasses
}
/**
* Used to configure the languages used by the `DataSource` according to priority;
* the first language in the array has the highest priority.
*
* @param languages - An array of ISO 639-1 language codes.
*/
setLanguages(languages?: string[]): void {
this.languages = languages;
// to be overloaded by subclasses
}
/**
* Used to express different country point of view (political view).
*
* @note Set to `undefined` (or empty string) if you want to reset to default point of view.
* @param pov - The country code which point of view should be presented in lower-case
* ISO 3166-1 alpha-2 format.
*/
setPoliticalView(pov?: string): void {
// to be overloaded by subclasses
}
/**
* This method is called when {@link MapView} needs to visualize or preload the content of a
* {@link @xyzmaps/harp-geoutils#TileKey}.
*
* @param tileKey - The unique identifier for a map tile.
* @param delayLoad - If true, the Tile will be created, but Tile.load will not be called
* @default false.
*/
abstract getTile(tileKey: TileKey, delayLoad?: boolean): Tile | undefined;
/**
* This method is called by {@link MapView} before the
* tile needs to be updated, for example after
* a theme change.
*
* @param tile - The {@link Tile} to update.
*/
updateTile(tile: Tile) {
// to be overloaded by subclasses
}
/**
* This method is called by the {@link MapView} to determine if the content of the surrounding
* tiles must be preloaded.
*
* @returns `true` if the {@link MapView} should try to preload tiles surrounding the visible
* tiles; `false` otherwise. The default is `false`.
*/
shouldPreloadTiles(): boolean {
return false;
}
/**
* The minimum zoom level at which data is available or displayed at
* (depending on {@link DataSource} subclass).
* @deprecated Use [[minDataLevel]] and [[minDisplayLevel]] instead.
*/
get minZoomLevel(): number {
logger.warn(
"DataSource.minZoomLevel is deprecated. Use minDataLevel and maxDataLevel instead."
);
return this.minDataLevel;
}
set minZoomLevel(level: number) {
logger.warn(
"DataSource.minZoomLevel is deprecated. Use minDataLevel and minDisplayLevel instead."
);
this.minDataLevel = level;
}
/**
* The maximum zoom level at which data is available or displayed at
* (depending on {@link DataSource} subclass).
* @deprecated Use [[maxDataLevel]] and [[maxDisplayLevel]] instead.
*/
get maxZoomLevel(): number {
logger.warn(
"DataSource.maxZoomLevel is deprecated. Use maxDataLevel and maxDisplayLevel instead."
);
return this.maxDataLevel;
}
set maxZoomLevel(level: number) {
logger.warn(
"DataSource.maxZoomLevel is deprecated. Use maxDataLevel and maxDisplayLevel instead."
);
this.maxDataLevel = level;
}
/**
* Maximum geometry height above ground level this `DataSource` can produce.
*
* Used in first stage of frustum culling before
* {@link Tile.maxGeometryHeight} data is available.
*
* @default 0.
*/
get maxGeometryHeight() {
return this.m_maxGeometryHeight;
}
set maxGeometryHeight(value: number) {
this.m_maxGeometryHeight = value;
}
/**
* Minimum geometry height below ground level this `DataSource` can produce. A negative number
* specifies a value below ground level.
*
* Used in first stage of frustum culling before
* {@link Tile.minGeometryHeight} data is available.
*
* @default 0.
*/
get minGeometryHeight() {
return this.m_minGeometryHeight;
}
set minGeometryHeight(value: number) {
this.m_minGeometryHeight = value;
}
/**
* The difference between storage level and display level of tile.
*
* Storage level offset is a value applied (added) to current zoom level giving
* a final tile level being displayed. This way we may differentiate current
* zoom level from the storage level that is displayed, giving fine grained
* control over the tiles being decoded an displayed.
*/
get storageLevelOffset() {
return this.m_storageLevelOffset;
}
/**
* Setup the relative offset between storage level and display level of tile.
*
* @param levelOffset - Difference between zoom level and display level.
*/
set storageLevelOffset(levelOffset: number) {
this.m_storageLevelOffset = levelOffset;
}
/**
* Enables or disables overlay of geometry on elevation. It must be overloaded by data sources
* supporting this feature.
*
* @param value - True to enable, false to disable.
*/
setEnableElevationOverlay(enable: boolean) {
// to be overloaded by subclasses
}
/**
* Computes the data zoom level to use.
*
* @param zoomLevel - The zoom level of the {@link MapView}.
* @returns The data zoom level to use.
*/
getDataZoomLevel(zoomLevel: number): number {
return THREE.MathUtils.clamp(
zoomLevel + this.m_storageLevelOffset,
this.minDataLevel,
this.maxDataLevel
);
}
/**
* Returns `true` if {@link DataSource} should be displayed for the zoom level.
* @param zoomLevel - The zoom level of the {@link MapView}.
*/
isVisible(zoomLevel: number): boolean {
return zoomLevel >= this.minDisplayLevel && zoomLevel <= this.maxDisplayLevel;
}
/**
* Returns `true` if {@link DataSource} can load tile with
* given {@link @xyzmaps/harp-geoutils#TileKey} and zoom level.
*
* @param zoomLevel - The zoom level of the {@link MapView}.
* @param tileKey - The unique identifier for a map tile.
* @returns `true` if the tile for the given {@link @xyzmaps/harp-geoutils#TileKey} can be loaded.
*/
canGetTile(zoomLevel: number, tileKey: TileKey): boolean {
return tileKey.level <= zoomLevel;
}
/**
* Returns `true` if {@link MapView} should traverse tiles
* further with given {@link @xyzmaps/harp-geoutils#TileKey} and
* zoom level.
*
* @param zoomLevel - The zoom level of the {@link MapView}.
* @param tileKey - The unique identifier for a map tile.
* @returns `true` if the subtiles of the given {@link @xyzmaps/harp-geoutils#TileKey} should be
* checked for collisions.
*/
shouldSubdivide(zoomLevel: number, tileKey: TileKey): boolean {
return tileKey.level <= zoomLevel;
}
/**
* Returns `true` if {@link MapView} should render the text
* elements with the given {@link @xyzmaps/harp-geoutils#TileKey} and
* zoom level.
*
* @remarks
* This is an additional check for the tiles that are already selected for rendering so the
* default implementation returns `true`.
*
* @param zoomLevel - The zoom level.
* @param tileKey - The unique identifier for a map tile.
* @returns `true` if the text elements created for the
* given {@link @xyzmaps/harp-geoutils#TileKey} should be rendered.
*/
shouldRenderText(zoomLevel: number, tileKey: TileKey): boolean {
return true;
}
/**
* Sends a request to the {@link MapView} to redraw the scene.
*/
requestUpdate() {
this.dispatchEvent(this.UPDATE_EVENT);
}
}