/
slicerWebWorker.js
52 lines (45 loc) · 1.47 KB
/
slicerWebWorker.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
import geojsonvt from 'geojson-vt';
import * as topojson from "topojson-client";
var slicers = {};
var options;
onmessage = function (e) {
if (e.data[0] === 'slice') {
// Given a blob of GeoJSON and some topojson/geojson-vt options, do the slicing.
var geojson = e.data[1];
options = e.data[2];
if (geojson.type && geojson.type === 'Topology') {
for (var layerName in geojson.objects) {
slicers[layerName] = geojsonvt(
topojson.feature(geojson, geojson.objects[layerName])
, options);
}
} else {
slicers[options.vectorTileLayerName] = geojsonvt(geojson, options);
}
} else if (e.data[0] === 'get') {
// Gets the vector tile for the given coordinates, sends it back as a message
var coords = e.data[1];
var tileLayers = {};
for (var layerName in slicers) {
var slicedTileLayer = slicers[layerName].getTile(coords.z, coords.x, coords.y);
if (slicedTileLayer) {
var vectorTileLayer = {
features: [],
extent: options.extent,
name: options.vectorTileLayerName,
length: slicedTileLayer.features.length
}
for (var i in slicedTileLayer.features) {
var feat = {
geometry: slicedTileLayer.features[i].geometry,
properties: slicedTileLayer.features[i].tags,
type: slicedTileLayer.features[i].type // 1 = point, 2 = line, 3 = polygon
}
vectorTileLayer.features.push(feat);
}
tileLayers[layerName] = vectorTileLayer;
}
}
postMessage({ layers: tileLayers, coords: coords });
}
}