-
Notifications
You must be signed in to change notification settings - Fork 3
/
turf-node.js
41 lines (41 loc) · 1.77 KB
/
turf-node.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
module.exports = function(RED) {
function TurfNode(config) {
var turf = require('@turf/turf');
RED.nodes.createNode(this,config);
this.fn = config.fn || "";
var node = this;
this.on('input', function(msg) {
var turffunction = node.fn || msg.topic;
var turfcontext = msg.payload;
if (turffunction && typeof turffunction === "string" && turffunction.length > 0) {
if (!Array.isArray(turfcontext)) { turfcontext = [ turfcontext ]; }
try {
this.status({fill:"blue",shape:"ring",text:turffunction});
if (turffunction === "featureEach" || turffunction === "geomEach" || turffunction === "propEach") {
turf[turffunction].apply(null, turfcontext);
msg.payload = turfcontext[0];
}
else if (turffunction === "clusterEach") {
var values = [];
turf.clusterEach(turfcontext[0], 'cluster', function (cluster, clusterValue) {
values.push(cluster);
});
msg.payload = values;
}
else {
msg.payload = turf[turffunction].apply(null, turfcontext);
}
node.send(msg);
} catch (err) {
this.status({fill:"red",shape:"dot",text:"error"});
node.error(err,msg);
}
}
else {
this.status({fill:"red",shape:"dot",text:"error"});
node.error("topic not a turf command",msg);
}
});
}
RED.nodes.registerType("turf",TurfNode);
}