[GIS] Running turf.js on desktop

node-jsturf

Morgan Herlocker answered in another question, that he uses turf.js on his desktop with node.js.

I'm really new to GIS and won't become pro for all hacks. My need is:

  1. having a geojson with a polygon – I got it
  2. add a buffer of 5km to that geojson-plygon – well, my problem
  3. having again a geojson with only the buffer – the result from 2
  4. add the geojson-buffer to a OpenStreetMap – I got it

I only use buffer and all operations are the same every time. That is why I do not have to step in deep.

This is what happens after the script-way:

enter image description here

It remains a problem of epsg:3857 or epsg:4326

The result is imported to OpenStreetMap.

Best Answer

Since you are a beginner at Node, I'll try to give you a step by step answer

  1. Firstly you need to install Node on your system. The exact steps will depend on your Operating system, and version, but it's a good bet that you will find the installer at https://nodejs.org/download/ You should also note that if you have an updated installer, then NPM or the Node Package Manager will also be installed for you.

  2. Now you need to install turf on your system. The Node way of doing this, is to create a folder, and then install the library inside that folder, so that you can work inside that folder. Open the Node.js command prompt (if you are on windows), and go to your working directory. Then type npm install turf. This command will download the required files (turf as well as dependencies) from the internet, and install it your working directory.

  3. Now that you have installed the library, you will want to use it. Generally speaking, Node modules & libraries do not have any UI of their own. You call them in your own Nodejs scripts.

You want to buffer and get the buffered output as GeoJSON. Here is an example script that buffers a polygon, and writes the output as a GeoJSON file

//Import the required modules
var turf = require( "turf" );
var fs = require('fs');

//input GeoJSON Feature
var mumbai={"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[72.7752778054935,19.2690995044639],[72.7817405956684,19.1238719055124],[72.8102749851638,19.1265959946878],[72.8247751057141,19.0910359643985],[72.8153720376396,19.0437583107182],[72.8209940779221,19.0352490234709],[72.8329105061117,19.0434886443927],[72.8359963318184,19.0373312064806],[72.8231900924928,19.0205055493087],[72.8125475128558,19.029908429424],[72.8080633067106,18.9898352623244],[72.7870845004595,18.947660113865],[72.7942309393155,18.937263881405],[72.809080960536,18.9503035811928],[72.8217391510222,18.9394947874404],[72.799135569344,18.9068306279723],[72.8113769649099,18.8893472433276],[72.8218126386322,18.8951870964281],[72.8474780411575,18.9235963464463],[72.8675088075429,19.0019646776918],[72.8819781251631,19.0064190047499],[72.8877097499884,18.9888559741936],[72.9236539940438,18.9983155390154],[72.9660988576404,19.0502455556637],[72.9836291475658,19.1747956918978],[72.9248922078066,19.2203564322991],[72.8998650805814,19.2558042171924],[72.8514714066599,19.2738226789422],[72.8514714066599,19.2738226789422],[72.8514714066599,19.2738226789422],[72.7752778054935,19.2690995044639]]]},"properties":{}};

//Buffer it by 500 meters
var buffered = turf.buffer(mumbai, 500, 'meters');

//try writing to a file
fs.writeFile("D:/buffered.geojson", JSON.stringify(buffered), function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 

//also display on Console
console.log("The Output Generated was: ");
console.log(buffered);