-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathworker_tile.js
189 lines (159 loc) · 6.78 KB
/
worker_tile.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// @flow
import FeatureIndex from '../data/feature_index';
import { performSymbolLayout } from '../symbol/symbol_layout';
import { CollisionBoxArray } from '../data/array_types';
import DictionaryCoder from '../util/dictionary_coder';
import SymbolBucket from '../data/bucket/symbol_bucket';
import { warnOnce, mapObject, values } from '../util/util';
import assert from 'assert';
import ImageAtlas from '../render/image_atlas';
import GlyphAtlas from '../render/glyph_atlas';
import EvaluationParameters from '../style/evaluation_parameters';
import { OverscaledTileID } from './tile_id';
import type {Bucket} from '../data/bucket';
import type Actor from '../util/actor';
import type StyleLayer from '../style/style_layer';
import type StyleLayerIndex from '../style/style_layer_index';
import type {StyleImage} from '../style/style_image';
import type {StyleGlyph} from '../style/style_glyph';
import type {
WorkerTileParameters,
WorkerTileCallback,
} from '../source/worker_source';
class WorkerTile {
tileID: OverscaledTileID;
uid: string;
zoom: number;
pixelRatio: number;
tileSize: number;
source: string;
overscaling: number;
showCollisionBoxes: boolean;
collectResourceTiming: boolean;
status: 'parsing' | 'done';
data: VectorTile;
collisionBoxArray: CollisionBoxArray;
abort: ?() => void;
reloadCallback: WorkerTileCallback;
vectorTile: VectorTile;
constructor(params: WorkerTileParameters) {
this.tileID = new OverscaledTileID(params.tileID.overscaledZ, params.tileID.wrap, params.tileID.canonical.z, params.tileID.canonical.x, params.tileID.canonical.y);
this.uid = params.uid;
this.zoom = params.zoom;
this.pixelRatio = params.pixelRatio;
this.tileSize = params.tileSize;
this.source = params.source;
this.overscaling = this.tileID.overscaleFactor();
this.showCollisionBoxes = params.showCollisionBoxes;
this.collectResourceTiming = !!params.collectResourceTiming;
}
parse(data: VectorTile, layerIndex: StyleLayerIndex, actor: Actor, callback: WorkerTileCallback) {
this.status = 'parsing';
this.data = data;
this.collisionBoxArray = new CollisionBoxArray();
const sourceLayerCoder = new DictionaryCoder(Object.keys(data.layers).sort());
const featureIndex = new FeatureIndex(this.tileID);
featureIndex.bucketLayerIDs = [];
const buckets: {[string]: Bucket} = {};
const options = {
featureIndex: featureIndex,
iconDependencies: {},
glyphDependencies: {}
};
const layerFamilies = layerIndex.familiesBySource[this.source];
for (const sourceLayerId in layerFamilies) {
const sourceLayer = data.layers[sourceLayerId];
if (!sourceLayer) {
continue;
}
if (sourceLayer.version === 1) {
warnOnce(`Vector tile source "${this.source}" layer "${sourceLayerId}" ` +
`does not use vector tile spec v2 and therefore may have some rendering errors.`);
}
const sourceLayerIndex = sourceLayerCoder.encode(sourceLayerId);
const features = [];
for (let index = 0; index < sourceLayer.length; index++) {
const feature = sourceLayer.feature(index);
features.push({ feature, index, sourceLayerIndex });
}
for (const family of layerFamilies[sourceLayerId]) {
const layer = family[0];
assert(layer.source === this.source);
if (layer.minzoom && this.zoom < Math.floor(layer.minzoom)) continue;
if (layer.maxzoom && this.zoom >= layer.maxzoom) continue;
if (layer.visibility === 'none') continue;
recalculateLayers(family, this.zoom);
const bucket = buckets[layer.id] = layer.createBucket({
index: featureIndex.bucketLayerIDs.length,
layers: family,
zoom: this.zoom,
pixelRatio: this.pixelRatio,
overscaling: this.overscaling,
collisionBoxArray: this.collisionBoxArray,
sourceLayerIndex: sourceLayerIndex
});
bucket.populate(features, options);
featureIndex.bucketLayerIDs.push(family.map((l) => l.id));
}
}
let error: ?Error;
let glyphMap: ?{[string]: {[number]: ?StyleGlyph}};
let imageMap: ?{[string]: StyleImage};
const stacks = mapObject(options.glyphDependencies, (glyphs) => Object.keys(glyphs).map(Number));
if (Object.keys(stacks).length) {
actor.send('getGlyphs', {uid: this.uid, stacks}, (err, result) => {
if (!error) {
error = err;
glyphMap = result;
maybePrepare.call(this);
}
});
} else {
glyphMap = {};
}
const icons = Object.keys(options.iconDependencies);
if (icons.length) {
actor.send('getImages', {icons}, (err, result) => {
if (!error) {
error = err;
imageMap = result;
maybePrepare.call(this);
}
});
} else {
imageMap = {};
}
maybePrepare.call(this);
function maybePrepare() {
if (error) {
return callback(error);
} else if (glyphMap && imageMap) {
const glyphAtlas = new GlyphAtlas(glyphMap);
const imageAtlas = new ImageAtlas(imageMap);
for (const key in buckets) {
const bucket = buckets[key];
if (bucket instanceof SymbolBucket) {
recalculateLayers(bucket.layers, this.zoom);
performSymbolLayout(bucket, glyphMap, glyphAtlas.positions, imageMap, imageAtlas.positions, this.showCollisionBoxes);
}
}
this.status = 'done';
callback(null, {
buckets: values(buckets).filter(b => !b.isEmpty()),
featureIndex,
collisionBoxArray: this.collisionBoxArray,
glyphAtlasImage: glyphAtlas.image,
iconAtlasImage: imageAtlas.image
});
}
}
}
}
function recalculateLayers(layers: $ReadOnlyArray<StyleLayer>, zoom: number) {
// Layers are shared and may have been used by a WorkerTile with a different zoom.
const parameters = new EvaluationParameters(zoom);
for (const layer of layers) {
layer.recalculate(parameters);
}
}
export default WorkerTile;