This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
TileDataSource.ts
337 lines (300 loc) · 11.2 KB
/
TileDataSource.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
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import {
Definitions,
ITileDecoder,
StyleSet,
Theme,
TileInfo
} from "@here/harp-datasource-protocol";
import { TileKey, TilingScheme } from "@here/harp-geoutils";
import { LRUCache } from "@here/harp-lrucache";
import {
ConcurrentDecoderFacade,
CopyrightInfo,
CopyrightProvider,
DataSource,
Tile,
TileLoaderState
} from "@here/harp-mapview";
import { LoggerManager } from "@here/harp-utils";
import { DataProvider } from "./DataProvider";
import { TileInfoLoader, TileLoader } from "./TileLoader";
/**
* Set of common options for all [[TileDataSource]]s.
*/
export interface TileDataSourceOptions {
/**
* Name of [[TileDataSource]], must be unique.
*/
name?: string;
/**
* The name of the [[StyleSet]] to evaluate for the decoding.
*/
styleSetName: string;
/**
* The [[TilingScheme]] the data source is using.
*/
tilingScheme: TilingScheme;
/**
* The [[DataProvider]] to use for downloading the actual data.
*/
dataProvider: DataProvider;
/**
* Optional: Specify [[ITileDecoder]] instance.
*/
decoder?: ITileDecoder;
/**
* Optional name of decoder service class.
* @see [[ConcurrentDecoderFacade]]
* @see [[ConcurrentWorkerSet]]
*/
concurrentDecoderServiceName?: string;
/**
* Optional URL for decoder bundle to be loaded into web worker.
*/
concurrentDecoderScriptUrl?: string;
/**
* Optional count of web workers to use with the decoder bundle.
*/
concurrentDecoderWorkerCount?: number;
/**
* Optional, default copyright information of tiles provided by this data source.
* Implementation should provide this information from the source data if possible.
*/
copyrightInfo?: CopyrightInfo[];
/**
* Optional copyright info provider for tiles provided by this data source. Copyrights from
* provider are concatenated with default ones from `copyrightInfo`.
*/
copyrightProvider?: CopyrightProvider;
/**
* Optional minimum zoom level (storage level) for [[Tile]]s. Default is 1.
*/
minZoomLevel?: number;
/**
* Optional maximum zoom level (storage level) for [[Tile]]s. Default is 20.
*/
maxZoomLevel?: number;
/**
* Optional storage level offset for [[Tile]]s. Default is 0.
*/
storageLevelOffset?: number;
}
/**
* Templated factory class to create instances of [[Tile]].
*/
export class TileFactory<TileType extends Tile> {
/**
* Initialize the factory using the constructor of the element to be called when a [[Tile]] is
* created.
*
* @param m_modelConstructor Constructor of (subclass of) [[Tile]].
*/
constructor(
private m_modelConstructor: new (dataSource: DataSource, tileKey: TileKey) => TileType
) {}
/**
* Create an instance of (subclass of) [[Tile]]. The required parameters are passed as arguments
* to the constructor of [[Tile]].
*
* @param dataSource [[Datasource]] this class belongs to.
* @param tileKey Quadtree address of the [[Tile]].
*/
create(dataSource: DataSource, tileKey: TileKey): TileType {
return new this.m_modelConstructor(dataSource, tileKey);
}
}
const maxLevelTileLoaderCache = 3;
/**
* Common base class for the typical [[DataSource]] which uses an [[ITileDecoder]] to decode the
* tile content asynchronously. The decoder can be passed in as an option, or a default
* asynchronous one is generated.
*/
export class TileDataSource<TileType extends Tile> extends DataSource {
protected readonly logger = LoggerManager.instance.create("TileDataSource");
protected readonly m_decoder: ITileDecoder;
protected readonly m_tileLoaderCache: LRUCache<number, TileLoader>;
private m_isReady: boolean = false;
/**
* Set up the `TileDataSource`.
*
* @param m_tileFactory Factory to create the [[Tile]] instances.
* @param m_options Options specifying the parameters of the [[DataSource]].
*/
constructor(
private readonly m_tileFactory: TileFactory<TileType>,
private readonly m_options: TileDataSourceOptions
) {
super(
m_options.name,
m_options.styleSetName,
m_options.minZoomLevel,
m_options.maxZoomLevel,
m_options.storageLevelOffset
);
if (m_options.decoder) {
this.m_decoder = m_options.decoder;
} else if (m_options.concurrentDecoderServiceName) {
this.m_decoder = ConcurrentDecoderFacade.getTileDecoder(
m_options.concurrentDecoderServiceName,
m_options.concurrentDecoderScriptUrl,
m_options.concurrentDecoderWorkerCount
);
} else {
throw new Error(
`TileDataSource[${this.name}]: unable to create, missing decoder or ` +
`concurrentDecoderServiceName`
);
}
this.useGeometryLoader = true;
this.cacheable = true;
this.m_tileLoaderCache = new LRUCache<number, TileLoader>(this.getCacheCount());
this.m_tileLoaderCache.evictionCallback = (_, tileLoader) => {
// Cancel any pending downloads as early as possible.
tileLoader.cancel();
};
}
dispose() {
this.decoder.dispose();
}
ready(): boolean {
return this.m_isReady && this.m_options.dataProvider.ready();
}
/**
* Get the [[ITileDecoder]] of this `ITileDataSource`, which has either been passed in with
* the options, or has been supplied by the [[ConcurrentDecoderFacade]].
*/
get decoder(): ITileDecoder {
return this.m_decoder;
}
async connect() {
await Promise.all([this.m_options.dataProvider.connect(), this.m_decoder.connect()]);
this.m_isReady = true;
this.m_decoder.configure(undefined, undefined, undefined, {
storageLevelOffset: this.m_options.storageLevelOffset
});
}
setStyleSet(styleSet?: StyleSet, definitions?: Definitions, languages?: string[]): void {
this.m_decoder.configure(styleSet, definitions, languages);
this.mapView.markTilesDirty(this);
}
/**
* Apply the [[Theme]] to this data source.
*
* Applies new [[StyleSet]] and definitions from theme only if matching styleset (see
* `styleSetName` property) is found in `theme`.
*/
setTheme(theme: Theme, languages?: string[]): void {
const styleSet =
this.styleSetName !== undefined && theme.styles
? theme.styles[this.styleSetName]
: undefined;
if (styleSet !== undefined) {
this.setStyleSet(styleSet, theme.definitions, languages);
}
}
clearCache() {
this.m_tileLoaderCache.evictAll();
}
/**
* Get the [[DataProvider]] that has been passed in with the options.
*/
dataProvider(): DataProvider {
return this.m_options.dataProvider;
}
getTilingScheme(): TilingScheme {
return this.m_options.tilingScheme;
}
/**
* Create a [[Tile]] and start the asynchronous download of the tile content. The [[Tile]] will
* be empty, but the download and decoding will be scheduled immediately. [[Tile]] instance is
* initialized with default copyrights, concatenated with copyrights from copyright provider of
* this data source.
*
* @param tileKey Quadtree address of the requested tile.
*/
getTile(tileKey: TileKey): TileType | undefined {
const tile = this.m_tileFactory.create(this, tileKey);
const mortonCode = tileKey.mortonCode();
const tileLoader = this.m_tileLoaderCache.get(mortonCode);
if (tileLoader !== undefined) {
tile.tileLoader = tileLoader;
} else {
const newTileLoader = new TileLoader(
this,
tileKey,
this.m_options.dataProvider,
this.decoder,
0
);
tile.tileLoader = newTileLoader;
tile.copyrightInfo = this.m_options.copyrightInfo;
if (this.m_options.copyrightProvider !== undefined) {
this.m_options.copyrightProvider
.getCopyrights(tile.geoBox, tileKey.level)
.then(copyrightInfo => {
tile.copyrightInfo =
tile.copyrightInfo === undefined
? copyrightInfo
: [...tile.copyrightInfo, ...copyrightInfo];
this.requestUpdate();
});
}
// We don't cache tiles with level 4 and above, at this level, there are 16 (2^4) tiles
// horizontally, given the assumption that the zoom level assumes the tile should be 256
// pixels wide (see function [[calculateZoomLevelFromDistance]]), and the current
// storage offset of -2 (which makes the tiles then 1024 pixels wide). this would mean a
// horizontal width of ~16k pixels for the entire earth, this would be quite a lot to
// pan, hence caching doesn't make sense above this point (as the chance that we need to
// share the TileLoader is small, and even if we did eventually see it, the original
// TileLoader would probably be evicted because it was removed by other more recent
// tiles).
if (tileKey.level <= maxLevelTileLoaderCache) {
this.m_tileLoaderCache.set(mortonCode, newTileLoader);
}
}
if (tile.tileLoader.decodedTile !== undefined) {
tile.decodedTile = tile.tileLoader.decodedTile;
} else {
tile.load();
}
return tile;
}
/**
* Get [[TileInfo]] of a tile.
*
* @param tileKey Quadtree address of the requested tile.
* @returns A promise which will contain the [[TileInfo]] when resolved.
*/
getTileInfo(tileKey: TileKey): Promise<TileInfo | undefined> {
const promise = new Promise<TileInfo | undefined>((resolve, reject) => {
const tileLoader = new TileInfoLoader(
this,
tileKey,
this.m_options.dataProvider,
this.decoder,
0
);
tileLoader.loadAndDecode().then(loaderState => {
if (loaderState === TileLoaderState.Ready) {
resolve(tileLoader.tileInfo);
} else {
reject(
new Error(`TileDataSource#getInfoTile wrong final state: ${loaderState}`)
);
}
});
});
return promise;
}
private getCacheCount(): number {
// We support up to [[maxLevelTileLoaderCache]] levels, this equates to roughly
// 2^maxLevelTileLoaderCache^2 tiles in total (at level maxLevelTileLoaderCache), we don't
// generally see that many, so we add a factor of 2 to try to get the worst case.
return Math.pow(2, maxLevelTileLoaderCache) * 2;
}
}