-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
88 lines (77 loc) · 2.88 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
78
79
80
81
82
83
84
85
86
87
88
var fs = require('fs'),
tempfile = require('tempfile'),
Promise = require('bluebird'),
request = require('request'),
Canvas = require('canvas'),
Image = Canvas.Image,
gdal = require('gdal'),
sig = require('amazon-s3-url-signer'),
child_process = require('child_process'),
path = require('path'),
config = Object.freeze(require('./config.json'));
Promise.promisifyAll(request, { multiArgs: true });
var signer = sig.urlSigner(config.s3.access, config.s3.secret, {
protocol: 'http'
});
process.on('message', function(message) {
var dsmUrl = signer.getUrl('GET', message.dsm.key, message.dsm.bucket, 600);
request.getAsync({ url: dsmUrl, encoding: null, gzip: true }).spread(function(res, body) {
var dsmTmp = tempfile('.dsm');
fs.writeFileSync(dsmTmp, body);
var rasterDSM = gdal.open(dsmTmp);
rasterDSM.bands.forEach(function(band) {
var rawBuffer = new ArrayBuffer((band.size.x * band.size.y) * 4);
var bandData = new Float32Array(rawBuffer);
var bandData8 = new Uint8Array(rawBuffer);
band.pixels.read(0, 0, band.size.x, band.size.y, bandData);
var heightMap = new Canvas(band.size.x, band.size.y),
ctx = heightMap.getContext('2d'),
imageData = ctx.createImageData(band.size.x, band.size.y);
imageData.data.set(bandData8);
ctx.putImageData(imageData, 0, 0);
var png = tempfile('.png');
fs.writeFileSync(png, heightMap.toBuffer());
child_process.exec(['./gdal.sh', dsmTmp, png, message.zoom].join(' '), {
env: {
PATH: process.env['PATH'],
LD_LIBRARY_PATH: process.env['LD_LIBRARY_PATH'],
PYTHONPATH: process.env['PYTHONPATH'] + ':' + path.join(process.env['TASK_ROOT'], "/" + config.gdal.python_package + "/"),
GDAL_DATA: path.join(process.env['TASK_ROOT'], '/local/share/gdal')
}
}, function(error, stdout, stderr) {
if(stdout) {
var output = stdout.match(/(Tiles generated to: )(.+)/im);
if(!stderr) stderr = undefined;
var outputDirectory = output[2] || "";
process.send({
output: outputDirectory.trim(),
stdout: stdout,
stderr: stderr,
png: png,
dsm: dsmTmp,
request: message,
env: process.env
});
} else {
process.send({
stdout: null,
stderr: stderr,
png: png,
dsm: dsmTmp,
request: message,
env: process.env
});
}
if (error !== null) {
process.send({
error: error,
png: png,
dsm: dsmTmp,
request: message,
env: process.env
});
}
});
});
});
});