-
Notifications
You must be signed in to change notification settings - Fork 0
/
turf-module.js
236 lines (206 loc) · 6.72 KB
/
turf-module.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
module.exports = function(RED) {
function TurfModuleNode(config) {
const turf = require('@turf/turf');
const _ = require("underscore");
const fs = require("fs");
const node = this;
RED.nodes.createNode(this,config);
this.on('input', (msg, send, done) => {
const globalContext = node.context().global;
const flowContext = node.context().flow;
const turfFunction = config.meta.module;
// Init Arguments and Options
this.args = this.args || [];
this.options = this.options || [];
// We only do this once - if we have multi inputs we cannot have duplicated args and options
if (this.args.length === 0) {
_.each(config.meta.props, (value, key) => {
const val = _.clone(value);
if (key.includes("turf-argument")) {
val.value = config[key];
val.position = parseInt(key.replace("turf-argument-", ""));
this.args.push(val);
}
if (key.includes("turf-option")) {
val.value = config[key];
val.position = parseInt(key.replace("turf-option-", ""));
// We only push to options array if we have a value - empty Inputs are ignored so turf uses the default value
if (!_.isEmpty(val.value)) {
this.options.push(val);
}
}
});
this.args = _.sortBy(this.args, x => x.position);
this.options = _.sortBy(this.options, x => x.position);
}
// Helper
// Hack for finding nested objects in string with array notation (e.g.: [0])
// https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-and-arrays-by-string-path
Object.getValueByString = function(o, s) {
s = s.replace(/\[(\w+)\]/g, '.$1');
s = s.replace(/^\./, '');
const a = s.split('.');
for (let i = 0, n = a.length; i < n; ++i) {
const k = a[i];
if (k in o) {
o = o[k];
} else {
return;
}
}
return o;
}
function prepareParameter(param, msg) {
// Turf Node Value
if (param.vt === "turf" && msg.turfNodeName === param.value) {
param.data = msg.payload;
return param
}
// Message Value
if (param.vt === "msg") {
let val = undefined;
try {
val = Object.getValueByString(msg, param.value);
if (_.isUndefined(val)) {
throw Error();
}
} catch (err) {
const errorText = `Missing variable: ${param.value}`;
const error = new Error(errorText);
node.status({
fill: "red",
shape: "dot",
text: "error"
});
node.error(error, errorText);
}
if (!_.isUndefined(val)) {
param.data = val;
return param
}
}
// Global Value
if (param.vt === "global") {
if (!globalContext.get(param.value)) {
const errorText = `Missing global variable: ${param.value}`;
const error = new Error(errorText);
node.status({
fill: "red",
shape: "dot",
text: "error"
});
node.error(error, errorText);
}
param.data = globalContext.get(param.value);
return param
}
// Flow Value
if (param.vt === "flow") {
if (!flowContext.get(param.value)) {
const errorText = `Missing flow variable: ${param.value}`;
const error = new Error(errorText);
node.status({
fill: "red",
shape: "dot",
text: "error"
});
node.error(error, errorText);
}
param.data = flowContext.get(param.value);
return param
}
// JSON
if (param.vt === "json") {
try {
param.data = JSON.parse(param.value);
return param
} catch (error) {
param.data = {};
return param
}
}
// Number
if (param.vt === "num") {
param.data = parseInt(param.value);
return param
}
// Boolean
if (param.vt === "bool") {
param.data = param.value === "true";
return param
}
if (param.vt === "str") {
param.data = param.value;
return param
}
}
// Perpare Arguments
_.each(this.args, (arg) => {
prepareParameter(arg, msg);
});
// Prepare Options
_.each(this.options, (option) => {
prepareParameter(option, msg);
});
if (!_.find(this.args, x => _.isUndefined(x.data)) && !_.find(this.options, x => _.isUndefined(x.data))) {
let turfContext = [];
// Set turfNodeName
if (config.name && config.name.length > 0) {
msg.turfNodeName = config.name;
}
// Create turfContext for args
_.each(this.args, (arg) => {
turfContext.push(arg.data);
});
// Create turfContext for options
let turfOptions = {};
_.each(this.options, (option) => {
turfOptions[option.n] = option.data;
});
turfContext.push(turfOptions);
try {
node.status({
fill: "blue",
shape: "ring",
text: turfFunction
});
// Run turffunction or callback depending on the function
if (turfFunction === "featureEach" || turfFunction === "geomEach" || turfFunction === "propEach") {
turf[turfFunction].apply(null, turfContext);
msg.payload = turfContext[0];
}
else if (turfFunction === "clusterEach") {
let values = [];
turf.clusterEach(turfContext[0], 'cluster', function (cluster, clusterValue) {
values.push(cluster);
});
msg.payload = values;
}
else {
msg.payload = turf[turfFunction].apply(null, turfContext);
}
// CleanUp
this.args = [];
this.options = [];
this.turfContext = null;
send(msg);
} catch (error) {
node.status({
fill: "red",
shape: "dot",
text: "error"
});
node.error(error, msg);
}
}
})
}
RED.nodes.registerType("turf-module", TurfModuleNode);
RED.httpAdmin.get("/turf/js/*", (req, res) => {
const options = {
root: `${__dirname}/static/`,
dotfiles: 'deny'
};
res.sendFile(req.params[0], options);
});
};