-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
237 lines (188 loc) · 5.15 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var _ = require("lodash");
var childProcess = require("child_process");
var fs = require("fs-extra");
var sax = require("sax");
var saxpath = require("saxpath");
var strict = true;
var numCPUs = require("os").cpus().length;
var shutdown = false;
// CityGML helpers
var citygmlSRS = require("citygml-srs");
var readStream;
var workers = {};
var processQueue = [];
var maxQueue = 30;
var queueCheck;
// TODO: Look into batching and threads to improve reliability and performance
var createWorkers = function() {
// Create a new worker for each CPU
for (var i = 0; i < numCPUs; i++) {
var worker = childProcess.fork(__dirname + "/worker");
workers[worker.pid] = {
process: worker,
ready: true,
alive: true
};
// Be notified when worker processes die.
worker.on("exit", function() {
workers[this.pid].alive = false;
});
// Receive messages from this worker and handle them in the master process.
worker.on("message", function(msg) {
if (msg.finished) {
if (msg.err) {
console.error(msg.err);
}
if (shutdown) {
this.kill("SIGINT");
}
workers[this.pid].ready = true;
}
});
}
};
var areWorkersShutdown = function() {
var alive = false;
_.each(workers, function(worker) {
if (worker.alive) {
alive = true;
return false;
}
});
if (alive) {
return false;
} else {
return true;
}
}
var processingQueue = false;
var updateQueue = function() {
if (processingQueue) {
return;
}
processingQueue = true;
processWorkers();
processingQueue = false;
};
var processWorkers = function() {
_.each(workers, function(worker, pid) {
if (processQueue.length === 0) {
return false;
}
if (worker.ready) {
worker.ready = false;
var item = processQueue.shift();
worker.process.send(item);
// Resume queue when space is available
if (processQueue.length < maxQueue && readStream.isPaused()) {
console.log("Resuming queue");
readStream.resume();
}
}
});
};
var citygmlToObj = function(options, callback) {
if (!options) {
options = {};
}
var defaults = {
overwrite: false
};
// Set defaults
_.defaults(options, defaults);
if (!options.citygmlPath) {
callback(new Error("CityGML path is required"));
return;
}
if (!options.objPath) {
callback(new Error("OBJ path is required: ", citygmlPath));
return;
}
if (!options.proj4def) {
callback(new Error("Proj4 definition is required: ", citygmlPath));
return;
}
// Set up workers
if (_.isEmpty(workers)) {
createWorkers();
queueCheck = setInterval(updateQueue, 50);
}
var saxParser = sax.createStream(strict, {
xmlns: true
});
var streamErrorHandler = function (e) {
console.error("Error:", e);
// Clear the error
// TODO: Check this is how sax-js recommends error handling
this._parser.error = null;
this._parser.resume();
};
saxParser.on("error", streamErrorHandler);
// REMOVED: SRS logic replaced with user-defined proj4 definition
//
// var saxStreamEnvelope = new saxpath.SaXPath(saxParser, "//gml:Envelope");
//
// saxStreamEnvelope.on("match", function(xml) {
// var srs = citygmlSRS(xml);
//
// if (srs) {
// envelopeSRS = srs;
// }
//
// // No need to kill the stream as it's still used to find buildings
// // readStream.destroy();
// });
var saxStream = new saxpath.SaXPath(saxParser, "//bldg:Building");
saxStream.on("match", function(xml) {
processQueue.push({
xml: xml,
proj4def: options.proj4def,
objPath: options.objPath,
overwrite: options.overwrite,
valhallaKey: options.valhallaKey
});
// Pause stream if queue is too large
if (processQueue.length >= maxQueue && !readStream.isPaused()) {
console.log("Pausing queue");
readStream.pause();
}
});
saxStream.on("end", function() {
console.log("Shutting down");
console.log("Queue count:", processQueue.length);
// Wait for queue to empty
var queueWaitTimer = setInterval(function() {
if (processQueue.length > 0) {
return;
}
// Stop queue wait timer
clearInterval(queueWaitTimer);
shutdown = true;
// Stop update check
clearInterval(queueCheck);
// Update queue one last time
// updateQueue();
// Clean up unused workers
// Keep the currently active worker open until it's finished
_.each(workers, function(worker) {
if (worker.ready) {
worker.process.kill("SIGINT");
}
});
console.log("Waiting for workers");
// Wait for all workers to finish before callback
// TODO: Don't let this go on forever
var aliveTimer = setInterval(function() {
var areShutdown = areWorkersShutdown();
if (areShutdown) {
console.log("Queue count:", processQueue.length);
clearInterval(aliveTimer);
callback(null);
}
}, 500);
}, 500);
});
readStream = fs.createReadStream(options.citygmlPath);
readStream.pipe(saxParser);
};
module.exports = citygmlToObj;