GeoSpatial Analysis with JavaScript (Turf.js) 🗺️

4 min readMar 14, 2019


Turf.js is a JavaScript library for spatial analysis. It includes traditional spatial operations, helper functions for creating GeoJSON data, and data classification and statistics tools. Turf can be added to your website as a client-side plugin, or you can run Turf server-side with Node.js.

This article assumes you have some interest in Geographic Information System (GIS) and you have previously written a few lines of Javascript codes.
We’ll be using Turf.js over this article to manipulate data based on locations.

Turf is Simple

Modular, simple-to-understand JavaScript functions that speak GeoJSON. Its functions are very easy to jump right into.

Turf is Modular

Turf is a collection of small modules, you only need to take what you want to use. A lot of functions in Turf exist within their own modules. So you don’t need to worry about size or complexities.

Turf is Fast

Takes advantage of the newest algorithms and doesn’t require you to send data to a server. It could save you a lot of processing time.

What is GeoJSON?

GeoJSON gives you an easy format for representing simple geographical features, along with their non-spatial attributes. It also uses the JavaScript Object Notation (JSON) format.

A typical GeoJSON object contains one or more of the following:

  • A Feature of any geometry type ( Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection)
  • A Feature Collection which represents multiple features.
  • Additional Properties which are tagged along with your feature.

Sample GeoJSON Objects

Kano, Nigeria GeoJSON Feature (contains single Feature Point for Kano)
Countries GeoJSON FeatureCollection (contains multiple Feature Polygons for each country)

Click on View Raw to access the GeoJSON objects for the FeatureCollection above, also allows you visualize your GeoJSON objects.

I’d love to write more about how awesome GeoJSON is but I’m sure you can learn a lot from reading Samit Arora ‘s post here.

Getting Started with Turf.js

Using Turf.js directly in the browser

<script src=''></script>

Using Turf.js in Node.js

It is recommended we install the individual module for functions we want to use.

npm install @turf/helpers

GeoSpatial Analysis with Turf.JS

Let’s write some codes 🔥 We’ll begin with some simple functions that are available within the @turf/helpers module:

Creating a Feature Point from Coordinates

const { point } = require('@turf/helpers')let pt1 = point([8.534860, 11.999970], { 'name': 'Kano State' })console.log(pt1)

Creating FeatureCollection from Feature Points

const { featureCollection, point } = require('@turf/helpers')let pt1 = point([8.534860, 11.999970], { 'name': 'Kano State' })
let pt2 = point([3.39467, 6.45407], { 'name': 'Lagos State' })
let fc = featureCollection([pt1, pt2])
Visualizing our FeatureCollection output on

Calculating the Distance between two Points

Let’s find the distance between our points (Kano & Lagos). We’ll need to use a different module: @turf/distance

const { point } = require('@turf/helpers')
const distance = require('@turf/distance').default
let pt1 = point([8.534860, 11.999970], { 'name': 'Kano State' })
let pt2 = point([3.39467, 6.45407], { 'name': 'Lagos State' })
let result = distance(pt1, pt2, { units: 'kilometers' })
console.log(`Distance between Kano and Lagos is: ${result} KM`)
Same results using GPS Coordinates

I do look forward to writing more articles on multiple use cases with Turf.js.
If you would like to explore deeper into Turf.js functions, check out their website.

Turf.js Modules Classification

  • Measurement Modules
  • Coordinate Mutation Modules
  • Transformation Modules
  • Feature Conversion Modules
  • Misc Modules
  • Helper Modules
  • Random Modules
  • Data Modules
  • Interpolation Modules
  • Joins Modules
  • Grids Modules
  • Classification Modules
  • Aggregation Modules
  • Meta Modules
  • Assertions Modules
  • Booleans Modules
  • Unit Conversion Modules

If you ever find yourself wanting to perform any of these operations on your geospatial data, Turf is the way to go!

It is Open Source and accessible on GitHub. There’s still more work to be done: docs to improve, code to write, bugs to discover, tests to write.
Feel free to jump right in and offer some help. 🤗

Thanks for reading through! Let me know if I missed any step, if something didn’t work out quite right for you or if this guide was helpful.





Software / DevOps Engineer | Google Developer Expert for Cloud |