How to use turf.js in node-red

I am working on Node-Red, and for my project I also use turf.js, which can be installed on node-red as well ( node-red-contrib-turf ).

This is how the structure looks:

With functionOne having this code:

msg.topic = "point";
msg.payload = [[-75.343, 39.984]];
return msg;

And functionTwo :

pt = msg.payload
msg.topic = "buffer";
msg.payload = [pt, 5, {units: 'kilometers'}];

return msg;

So, the first function is used to produce a point of type Feature as stated in the docs and I am passing it to the second one where I save it in the variable pt and then when I execute the whole thing it does not happen anything.

Does someone know what my mistake is?

I have not used this node but looking at the example on your link, the payload for the point might be incorrect

try msg.payload = [-75.343, 39.984]; instead.

Also, put debug nodes after each node to verify each step returns what you expect.

Thank you for taking a look. Actually, the first function works perfectly. I had a debug node over there and it does output a point. So, in my opinion it has to be something with the second function. Additionally, I also tried the double brackets on the second functions payload, but without any results.

Did you put a debug node on the output of the 'createPoint' node to make sure that it is sending a msg to the next function node?

At first glance you would seem to be correct and that code should work... but the node and also node-red-contrib-turfjs both seem to be several years old so the internal turf API may well have changed - You could try directly contacting the author to see if they are interested in fixing it.

As I like turf - I've contacted the author myself - I have a patch for the node - will see if he wants to accept it.

3 Likes

Thanks for reaching out. Haven't looked into this repo for a long time. I will give it a shot and update turf to the latest version - @dceejay - do you have a pull request for me?

I may have :wink: .... https://github.com/bloigge/node-red-contrib-turf/pull/5

@trickermkd - Once released to npm the OP flow above should then give


(If sent to worldmap)

[{"id":"bd598069a1cef449","type":"turf","z":"03086929b3b7f8ce","fn":"","name":"","x":410,"y":140,"wires":[["9c7463c2e60492ae","7712fc52a3289627"]]},{"id":"5bbd243899a9794c","type":"turf","z":"03086929b3b7f8ce","fn":"","name":"","x":670,"y":140,"wires":[["7712fc52a3289627"]]},{"id":"9c7463c2e60492ae","type":"function","z":"03086929b3b7f8ce","name":"","func":"msg.topic = \"buffer\";\nmsg.payload = [msg.payload, 5, { units: 'kilometers' }];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":140,"wires":[["5bbd243899a9794c"]]},{"id":"0bac9b156c763957","type":"inject","z":"03086929b3b7f8ce","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":140,"wires":[["2c03fced3d8db886"]]},{"id":"2c03fced3d8db886","type":"function","z":"03086929b3b7f8ce","name":"","func":"msg.topic = \"point\";\nmsg.payload = [[-75.343, 39.984]];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":140,"wires":[["bd598069a1cef449"]]},{"id":"7712fc52a3289627","type":"worldmap","z":"03086929b3b7f8ce","name":"","lat":"","lon":"","zoom":"","layer":"","cluster":"","maxage":"","usermenu":"show","layers":"show","panit":"false","panlock":"false","zoomlock":"false","hiderightclick":"false","coords":"false","showgrid":"false","allowFileDrop":"false","path":"/worldmap","x":820,"y":100,"wires":[]}]
1 Like

Thank you! I have updated the repo and released it on npm.

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.