-
-
Notifications
You must be signed in to change notification settings - Fork 643
/
raster_tile_source.ts
231 lines (203 loc) · 7.69 KB
/
raster_tile_source.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
import {extend, pick} from '../util/util';
import {ImageRequest} from '../util/image_request';
import {ResourceType} from '../util/request_manager';
import {Event, ErrorEvent, Evented} from '../util/evented';
import {loadTileJson} from './load_tilejson';
import {TileBounds} from './tile_bounds';
import {Texture} from '../render/texture';
import type {Source} from './source';
import type {OverscaledTileID} from './tile_id';
import type {Map} from '../ui/map';
import type {Dispatcher} from '../util/dispatcher';
import type {Tile} from './tile';
import type {
RasterSourceSpecification,
RasterDEMSourceSpecification
} from '@maplibre/maplibre-gl-style-spec';
/**
* A source containing raster tiles (See the [Style Specification](https://maplibre.org/maplibre-style-spec/) for detailed documentation of options.)
*
* @group Sources
*
* @example
* ```ts
* map.addSource('raster-source', {
* 'type': 'raster',
* 'tiles': ['https://tiles.stadiamaps.com/tiles/stamen_watercolor/{z}/{x}/{y}.jpg'],
* 'tileSize': 256,
* });
* ```
*
* @example
* ```ts
* map.addSource('wms-test-source', {
* 'type': 'raster',
* // use the tiles option to specify a WMS tile source URL
* 'tiles': [
* 'https://img.nj.gov/imagerywms/Natural2015?bbox={bbox-epsg-3857}&format=image/png&service=WMS&version=1.1.1&request=GetMap&srs=EPSG:3857&transparent=true&width=256&height=256&layers=Natural2015'
* ],
* 'tileSize': 256
* });
* ```
* @see [Add a raster tile source](https://maplibre.org/maplibre-gl-js/docs/examples/map-tiles/)
* @see [Add a WMS source](https://maplibre.org/maplibre-gl-js/docs/examples/wms/)
* @see [Display a satellite map](https://maplibre.org/maplibre-gl-js/docs/examples/satellite-map/)
*/
export class RasterTileSource extends Evented implements Source {
type: 'raster' | 'raster-dem';
id: string;
minzoom: number;
maxzoom: number;
url: string;
scheme: string;
tileSize: number;
bounds: [number, number, number, number];
tileBounds: TileBounds;
roundZoom: boolean;
dispatcher: Dispatcher;
map: Map;
tiles: Array<string>;
_loaded: boolean;
_options: RasterSourceSpecification | RasterDEMSourceSpecification;
_tileJSONRequest: AbortController;
constructor(id: string, options: RasterSourceSpecification | RasterDEMSourceSpecification, dispatcher: Dispatcher, eventedParent: Evented) {
super();
this.id = id;
this.dispatcher = dispatcher;
this.setEventedParent(eventedParent);
this.type = 'raster';
this.minzoom = 0;
this.maxzoom = 22;
this.roundZoom = true;
this.scheme = 'xyz';
this.tileSize = 512;
this._loaded = false;
this._options = extend({type: 'raster'}, options);
extend(this, pick(options, ['url', 'scheme', 'tileSize']));
}
async load() {
this._loaded = false;
this.fire(new Event('dataloading', {dataType: 'source'}));
this._tileJSONRequest = new AbortController();
try {
const tileJSON = await loadTileJson(this._options, this.map._requestManager, this._tileJSONRequest);
this._tileJSONRequest = null;
this._loaded = true;
if (tileJSON) {
extend(this, tileJSON);
if (tileJSON.bounds) this.tileBounds = new TileBounds(tileJSON.bounds, this.minzoom, this.maxzoom);
// `content` is included here to prevent a race condition where `Style#_updateSources` is called
// before the TileJSON arrives. this makes sure the tiles needed are loaded once TileJSON arrives
// ref: https://github.com/mapbox/mapbox-gl-js/pull/4347#discussion_r104418088
this.fire(new Event('data', {dataType: 'source', sourceDataType: 'metadata'}));
this.fire(new Event('data', {dataType: 'source', sourceDataType: 'content'}));
}
} catch (err) {
this._tileJSONRequest = null;
this.fire(new ErrorEvent(err));
}
}
loaded(): boolean {
return this._loaded;
}
onAdd(map: Map) {
this.map = map;
this.load();
}
onRemove() {
if (this._tileJSONRequest) {
this._tileJSONRequest.abort();
this._tileJSONRequest = null;
}
}
setSourceProperty(callback: Function) {
if (this._tileJSONRequest) {
this._tileJSONRequest.abort();
this._tileJSONRequest = null;
}
callback();
this.load();
}
/**
* Sets the source `tiles` property and re-renders the map.
*
* @param tiles - An array of one or more tile source URLs, as in the raster tiles spec (See the [Style Specification](https://maplibre.org/maplibre-style-spec/)
*/
setTiles(tiles: Array<string>): this {
this.setSourceProperty(() => {
this._options.tiles = tiles;
});
return this;
}
/**
* Sets the source `url` property and re-renders the map.
*
* @param url - A URL to a TileJSON resource. Supported protocols are `http:` and `https:`.
*/
setUrl(url: string): this {
this.setSourceProperty(() => {
this.url = url;
this._options.url = url;
});
return this;
}
serialize() {
return extend({}, this._options);
}
hasTile(tileID: OverscaledTileID) {
return !this.tileBounds || this.tileBounds.contains(tileID.canonical);
}
async loadTile(tile: Tile): Promise<void> {
const url = tile.tileID.canonical.url(this.tiles, this.map.getPixelRatio(), this.scheme);
tile.abortController = new AbortController();
try {
const response = await ImageRequest.getImage(this.map._requestManager.transformRequest(url, ResourceType.Tile), tile.abortController, this.map._refreshExpiredTiles);
delete tile.abortController;
if (tile.aborted) {
tile.state = 'unloaded';
return;
}
if (response && response.data) {
if (this.map._refreshExpiredTiles && response.cacheControl && response.expires) {
tile.setExpiryData({cacheControl: response.cacheControl, expires: response.expires});
}
const context = this.map.painter.context;
const gl = context.gl;
const img = response.data;
tile.texture = this.map.painter.getTileTexture(img.width);
if (tile.texture) {
tile.texture.update(img, {useMipmap: true});
} else {
tile.texture = new Texture(context, img, gl.RGBA, {useMipmap: true});
tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE, gl.LINEAR_MIPMAP_NEAREST);
if (context.extTextureFilterAnisotropic) {
gl.texParameterf(gl.TEXTURE_2D, context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, context.extTextureFilterAnisotropicMax);
}
}
tile.state = 'loaded';
}
} catch (err) {
delete tile.abortController;
if (tile.aborted) {
tile.state = 'unloaded';
} else if (err) {
tile.state = 'errored';
throw err;
}
}
}
async abortTile(tile: Tile) {
if (tile.abortController) {
tile.abortController.abort();
delete tile.abortController;
}
}
async unloadTile(tile: Tile) {
if (tile.texture) {
this.map.painter.saveTileTexture(tile.texture);
}
}
hasTransition() {
return false;
}
}