forked from developmentseed/vt-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
77 lines (70 loc) · 2.09 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
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
var grid = require('./lib/grid')
var tilelive = require('tilelive')
var aggregate = require('geojson-polygon-aggregate')
var throttle = require('lodash.throttle')
var parallel = require('run-parallel')
process.on('message', function (options) {
var featureCount = 0
var tileCount = 0
var sendProgress = throttle(function workerProgress () {
try {
featureCount = 0
tileCount = 0
process.send({ progress: Array.prototype.slice.call(arguments) })
} catch (e) {
process.exit(1)
}
}, 100)
options.progress = function (tiles, features, lasttile) {
tileCount += tiles
featureCount += features
sendProgress(tileCount, featureCount, lasttile)
}
// aggregation functions were passed in as names. look up the actual functions.
if (typeof options.aggregations !== 'string') {
for (var layer in options.aggregations) {
for (var field in options.aggregations[layer]) {
var fn = aggregate[options.aggregations[layer][field]]
options.aggregations[layer][field] = fn(field)
}
}
}
tilelive.auto(options.input)
tilelive.auto(options.output)
parallel([
tilelive.load.bind(null, options.input),
tilelive.load.bind(null, options.output)
], function (err, results) {
if (err) { throw err }
var input = results[0]
var output = results[1]
if (output._db) {
// set a busy timeout to avoid SQLITE_BUSY
output._db.configure('busyTimeout', 30000)
}
grid(output, input, options, function (err, finalProgress, nextLevel) {
if (err) { throw err }
parallel([
function (cb) {
if (typeof output.close === 'function') { return output.close(cb) }
cb()
},
function (cb) {
if (typeof input.close === 'function') { return input.close(cb) }
cb()
}
], function (err) {
if (err) { throw err }
try {
process.send({
progress: finalProgress,
nextLevel: nextLevel
})
process.exit()
} catch (e) {
process.exit(1)
}
})
})
})
})