-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
worker_tile.js
213 lines (178 loc) · 7.39 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict';
const FeatureIndex = require('../data/feature_index');
const CollisionTile = require('../symbol/collision_tile');
const CollisionBoxArray = require('../symbol/collision_box');
const DictionaryCoder = require('../util/dictionary_coder');
const util = require('../util/util');
const SymbolInstancesArray = require('../symbol/symbol_instances');
const SymbolQuadsArray = require('../symbol/symbol_quads');
const assert = require('assert');
class WorkerTile {
constructor(params) {
this.coord = params.coord;
this.uid = params.uid;
this.zoom = params.zoom;
this.tileSize = params.tileSize;
this.source = params.source;
this.overscaling = params.overscaling;
this.angle = params.angle;
this.pitch = params.pitch;
this.showCollisionBoxes = params.showCollisionBoxes;
}
parse(data, layerIndex, actor, callback) {
// Normalize GeoJSON data.
if (!data.layers) {
data = { layers: { '_geojsonTileLayer': data } };
}
this.status = 'parsing';
this.data = data;
this.collisionBoxArray = new CollisionBoxArray();
this.symbolInstancesArray = new SymbolInstancesArray();
this.symbolQuadsArray = new SymbolQuadsArray();
const collisionTile = new CollisionTile(this.angle, this.pitch, this.collisionBoxArray);
const sourceLayerCoder = new DictionaryCoder(Object.keys(data.layers).sort());
const featureIndex = new FeatureIndex(this.coord, this.overscaling, collisionTile, data.layers);
featureIndex.bucketLayerIDs = {};
const buckets = {};
let bucketIndex = 0;
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) {
util.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 i = 0; i < sourceLayer.length; i++) {
const feature = sourceLayer.feature(i);
feature.index = i;
feature.sourceLayerIndex = sourceLayerIndex;
features.push(feature);
}
for (const family of layerFamilies[sourceLayerId]) {
const layer = family[0];
assert(layer.source === this.source);
if (layer.minzoom && this.zoom < layer.minzoom) continue;
if (layer.maxzoom && this.zoom >= layer.maxzoom) continue;
if (layer.layout && layer.layout.visibility === 'none') continue;
for (const layer of family) {
layer.recalculate(this.zoom);
}
const bucket = buckets[layer.id] = layer.createBucket({
index: bucketIndex,
layers: family,
zoom: this.zoom,
overscaling: this.overscaling,
collisionBoxArray: this.collisionBoxArray,
symbolQuadsArray: this.symbolQuadsArray,
symbolInstancesArray: this.symbolInstancesArray
});
bucket.populate(features, options);
featureIndex.bucketLayerIDs[bucketIndex] = family.map((l) => l.id);
bucketIndex++;
}
}
const done = () => {
this.status = 'done';
if (this.redoPlacementAfterDone) {
this.redoPlacement(this.angle, this.pitch, null);
this.redoPlacementAfterDone = false;
}
const transferables = [];
callback(null, {
buckets: serializeBuckets(util.values(buckets), transferables),
featureIndex: featureIndex.serialize(transferables),
collisionTile: collisionTile.serialize(transferables),
collisionBoxArray: this.collisionBoxArray.serialize(),
symbolInstancesArray: this.symbolInstancesArray.serialize(),
symbolQuadsArray: this.symbolQuadsArray.serialize()
}, transferables);
};
// Symbol buckets must be placed in reverse order.
this.symbolBuckets = [];
for (let i = layerIndex.symbolOrder.length - 1; i >= 0; i--) {
const bucket = buckets[layerIndex.symbolOrder[i]];
if (bucket) {
this.symbolBuckets.push(bucket);
}
}
if (this.symbolBuckets.length === 0) {
return done();
}
let deps = 0;
let icons = Object.keys(options.iconDependencies);
let stacks = util.mapObject(options.glyphDependencies, (glyphs) => Object.keys(glyphs).map(Number));
const gotDependency = (err) => {
if (err) return callback(err);
deps++;
if (deps === 2) {
for (const bucket of this.symbolBuckets) {
// Layers are shared and may have been used by a WorkerTile with a different zoom.
for (const layer of bucket.layers) {
layer.recalculate(this.zoom);
}
bucket.prepare(stacks, icons);
bucket.place(collisionTile, this.showCollisionBoxes);
}
done();
}
};
if (Object.keys(stacks).length) {
actor.send('getGlyphs', {uid: this.uid, stacks: stacks}, (err, newStacks) => {
stacks = newStacks;
gotDependency(err);
});
} else {
gotDependency();
}
if (icons.length) {
actor.send('getIcons', {icons: icons}, (err, newIcons) => {
icons = newIcons;
gotDependency(err);
});
} else {
gotDependency();
}
}
redoPlacement(angle, pitch, showCollisionBoxes) {
if (this.status !== 'done') {
this.redoPlacementAfterDone = true;
this.angle = angle;
return {};
}
const collisionTile = new CollisionTile(angle, pitch, this.collisionBoxArray);
for (const bucket of this.symbolBuckets) {
// Layers are shared and may have been used by a WorkerTile with a different zoom.
for (const layer of bucket.layers) {
layer.recalculate(this.zoom);
}
bucket.place(collisionTile, showCollisionBoxes);
}
const transferables = [];
return {
result: {
buckets: serializeBuckets(this.symbolBuckets, transferables),
collisionTile: collisionTile.serialize(transferables)
},
transferables: transferables
};
}
}
function serializeBuckets(buckets, transferables) {
return buckets
.filter((b) => !b.isEmpty())
.map((b) => b.serialize(transferables));
}
module.exports = WorkerTile;