Introduction to Turf.JS – JavaScript library for Geospatial Analysis.

What is Turf.JS?

Turf.js is a JavaScript library for spatial analysis. It helps you analyze, aggregate, and transform data in order to visualize it in new ways and answer advanced questions about it. In this guide, we’ll take a look at how Turf.js works, what it can do, and some examples of it in action.

Turf uses GeoJSON for all geographic data. Turf expects the data to be standard WGS84 longitude, latitude coordinates. Most Turf functions work with GeoJSON features. These are are pieces of data that represent a collection of properties (ie: population, elevation, zipcode, etc.) along with a geometry.

Spatial analysis with Turf.JS

Turf.js is a library for spatial analysis: a large family of tasks like ‘calculating area and distance’ and ‘joining points to polygons’ that enable people to see new facets of their data. Spatial analysis is used in every industry: to find the nearest coffee shop, calculate travel time, and show regional statistics for utility usage. It’s also a huge part of GIS, where many problems are solved with spatial analysis.

Getting started with Turf.js

There are a few different ways to get started using Turf. In this post, I explored the use of TurfJS spatial functions in performing simple and basic spatial analysis that likely to be faced in any GIS project.

Below, I will be explaining some of the most powerful and spatial functions of TurfJS;

1. Area: This function take one or more features and returns the area in square meters. This can be useful in scenarios where you wish to determine the area of a polygon such as buildings, parks, state boundary etc.

1
2
var area = turf.area(polygons);
//input polygon can either be Feature or FeatureCollection

2. Boundary Box: Returns the bounding box/extent of a set spatial feature. Such features can be a combination of point, line or polygon data types.

1
2
3
var bbox = turf.bbox(features);
//bbox extent in [ minX, minY, maxX, maxY ] order
var bboxPolygon = turf.bboxPolygon(bbox);

3. Bearing: This function returns the geographic bearing between two point features. The result is expressed in decimal degrees.

1
var bearing = turf.bearing(point1, point2);

4. Center: Returns a point feature that represents the absolute center of a Feature or FeatureCollection.

1
var centerPt = turf.center(features);

5. Distance: This function calculates the distance between two points and returns a value in degrees, radians, miles, or kilometers.

1
2
var distance = turf.distance(from_point, to_point, units);
// units can be in degrees, radians, miles, or kilometers (default).

6. Line Distance: Unlike the Distance function that works with Point feature, this functions measures a Linestring and Polygon and returns the length in degrees, radians, miles or kilometers.

1
2
var length = turf.lineDistance(line, units);
// units can be in degrees, radians, miles, or kilometers (default).

7. Buffer: This function calculates the buffer radius of a given feature and returns a polygon of the buffered area. Units supported are miles, kilometers, and degrees.

1
2
3
var buffered = turf.buffer(point, radius, unit);
// units can be in degrees, radians, miles, or kilometers (default).
// radius in number format

8. Intersection: This function returns the intersection of two overlapping polygons.

1
var intersection = turf.intersect(poly1, poly2);

9. FeatureCollection: Creates a feature collection of one or more features.

1
2
3
4
5
6
var features = [
turf.point([-75.343, 39.984], {name: 'Location A'}),
turf.point([-75.833, 39.284], {name: 'Location B'}),
turf.point([-75.534, 39.123], {name: 'Location C'})
];
var fc = turf.featureCollection(features);

10. Random: Generate random GeoJSON data, including Points and Polygons for testing and experimentation.

1
2
3
4
5
var points = turf.random(type, count, {
bbox: [minX, minY, maxX, maxY ]
});
//type can be "points" or "polygons"
//count is the number of random features to be generated

11. SquareGrid: This function takes a bounding box and cell depth and returns a set of square polygons in a grid.

1
2
3
var squareGrid = turf.squareGrid(bbox, cellSize, units);
//cellSize is the proposed width of each cell
// units can be in degrees, radians, miles, or kilometers (default).

12. Within: Takes a set of points and set of polygons and returns the point that fall within the polygon.

1
var within = turf.within(points, polygon);

TurfJS has many more functions that I couldn’t touch in this post. To learn more about the awesome functionalities of TurfJS, check out the documentation here.

Somide Olaoye Anthony

Somide Olaoye Anthony

Experienced geospatial information technologist with in-depth understanding of GIS practices and concepts. Outstanding knowledge of GIS software development, geodatabase design, data analysis and visualization. Proficient in use of Python, R, PHP, JavaScript, SQL, Tableau & PowerBI.
Somide Olaoye Anthony