Introduction to Turf.JS – JavaScript library for Geospatial Analysis

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, zip code, etc.) along with 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 takes 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 boundaries, etc.

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

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.

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

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

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

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

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.

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

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

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

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.

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

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

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

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

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.

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

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

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

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

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.