-
Notifications
You must be signed in to change notification settings - Fork 5
/
worker.js
38 lines (33 loc) · 1.1 KB
/
worker.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
var concave = require('@turf/concave');
var geojsonhint = require('geojsonhint');
var geobuf = require('geobuf');
var Pbf = require('pbf');
module.exports = function(self) {
self.addEventListener('message', function(ev) {
var dataUrl = ev.data[0];
self.makeRequest(dataUrl);
});
self.makeRequest = function(url) {
var req = new XMLHttpRequest();
req.addEventListener('load', self.validateData);
req.addEventListener('error', self.transferFailed);
req.open('GET', url);
req.send();
};
self.validateData = function() {
var errors = geojsonhint.hint(this.responseText);
if (errors.len > 0) {
self.postMessage(['Errors', errors.join(', ') ]);
} else {
self.turfIt(this.responseText);
}
};
self.transferFailed = function() {
self.postMessage(['Error', this.responseText]);
};
self.turfIt = function(data) {
var results = concave(JSON.parse(data), 0.75, 'miles');
var buf = geobuf.encode(results, new Pbf());
self.postMessage(['Done', buf]);
};
};