-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
vector_tile_source.js
165 lines (136 loc) · 5.49 KB
/
vector_tile_source.js
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
// @flow
import { Event, ErrorEvent, Evented } from '../util/evented';
import { extend, pick } from '../util/util';
import loadTileJSON from './load_tilejson';
import { normalizeTileURL as normalizeURL } from '../util/mapbox';
import TileBounds from './tile_bounds';
import { ResourceType } from '../util/ajax';
import browser from '../util/browser';
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 {Callback} from '../types/callback';
import type {Cancelable} from '../types/cancelable';
class VectorTileSource extends Evented implements Source {
type: 'vector';
id: string;
minzoom: number;
maxzoom: number;
url: string;
scheme: string;
tileSize: number;
_options: VectorSourceSpecification;
_collectResourceTiming: boolean;
dispatcher: Dispatcher;
map: Map;
bounds: ?[number, number, number, number];
tiles: Array<string>;
tileBounds: TileBounds;
reparseOverscaled: boolean;
isTileClipped: boolean;
_tileJSONRequest: ?Cancelable;
constructor(id: string, options: VectorSourceSpecification & {collectResourceTiming: boolean}, dispatcher: Dispatcher, eventedParent: Evented) {
super();
this.id = id;
this.dispatcher = dispatcher;
this.type = 'vector';
this.minzoom = 0;
this.maxzoom = 22;
this.scheme = 'xyz';
this.tileSize = 512;
this.reparseOverscaled = true;
this.isTileClipped = true;
extend(this, pick(options, ['url', 'scheme', 'tileSize']));
this._options = extend({ type: 'vector' }, options);
this._collectResourceTiming = options.collectResourceTiming;
if (this.tileSize !== 512) {
throw new Error('vector tile sources must have a tileSize of 512');
}
this.setEventedParent(eventedParent);
}
load() {
this.fire(new Event('dataloading', {dataType: 'source'}));
this._tileJSONRequest = loadTileJSON(this._options, this.map._transformRequest, (err, tileJSON) => {
this._tileJSONRequest = null;
if (err) {
this.fire(new ErrorEvent(err));
} else 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'}));
}
});
}
hasTile(tileID: OverscaledTileID) {
return !this.tileBounds || this.tileBounds.contains(tileID.canonical);
}
onAdd(map: Map) {
this.map = map;
this.load();
}
onRemove() {
if (this._tileJSONRequest) {
this._tileJSONRequest.cancel();
this._tileJSONRequest = null;
}
}
serialize() {
return extend({}, this._options);
}
loadTile(tile: Tile, callback: Callback<void>) {
const url = normalizeURL(tile.tileID.canonical.url(this.tiles, this.scheme), this.url);
const params = {
request: this.map._transformRequest(url, ResourceType.Tile),
uid: tile.uid,
tileID: tile.tileID,
zoom: tile.tileID.overscaledZ,
tileSize: this.tileSize * tile.tileID.overscaleFactor(),
type: this.type,
source: this.id,
pixelRatio: browser.devicePixelRatio,
showCollisionBoxes: this.map.showCollisionBoxes,
};
params.request.collectResourceTiming = this._collectResourceTiming;
if (tile.workerID === undefined || tile.state === 'expired') {
tile.workerID = this.dispatcher.send('loadTile', params, done.bind(this));
} else if (tile.state === 'loading') {
// schedule tile reloading after it has been loaded
tile.reloadCallback = callback;
} else {
this.dispatcher.send('reloadTile', params, done.bind(this), tile.workerID);
}
function done(err, data) {
if (tile.aborted)
return callback(null);
if (err) {
return callback(err);
}
if (data && data.resourceTiming)
tile.resourceTiming = data.resourceTiming;
if (this.map._refreshExpiredTiles) tile.setExpiryData(data);
tile.loadVectorData(data, this.map.painter);
callback(null);
if (tile.reloadCallback) {
this.loadTile(tile, tile.reloadCallback);
tile.reloadCallback = null;
}
}
}
abortTile(tile: Tile) {
this.dispatcher.send('abortTile', { uid: tile.uid, type: this.type, source: this.id }, undefined, tile.workerID);
}
unloadTile(tile: Tile) {
tile.unloadVectorData();
this.dispatcher.send('removeTile', { uid: tile.uid, type: this.type, source: this.id }, undefined, tile.workerID);
}
hasTransition() {
return false;
}
}
export default VectorTileSource;