Introduction
Welcome to the Turf documentation page! Turf is a powerful JavaScript library for spatial analysis. Whether you are a developer, a GIS professional, or a data scientist, Turf provides a wide range of geospatial functions and transformations.
In this guide, you will learn how to use Turf, understand its main concepts, and explore its extensive library of geospatial operations.
Installation
To start using Turf in your projects, you need to install it as a dependency in your JavaScript environment. Follow the steps below:
- Install Turf via npm by running the following command in your terminal:
- Import Turf into your code using a standard import statement:
- You’re all set! Now you can start using Turf’s functions in your project.
npm install @turf/turf
import * as turf from '@turf/turf';
Available Functions
Turf provides a comprehensive set of geospatial functions to manipulate, analyze, and transform geographic data. Below are some of the core functions provided by Turf:
- turf.booleanContains(geom1, geom2): Determines if one geometry is completely inside another.
- turf.booleanOverlap(geom1, geom2): Determines if two geometries overlap each other.
- turf.distance(point1, point2, options): Calculates the distance between two points.
- turf.area(polygon): Calculates the area of a polygon.
- turf.convex(input): Creates the smallest convex polygon that contains all the input points.
- turf.center(input): Calculates the center point of a geometry.
- turf.buffer(geom, distance, options): Creates a buffer polygon around a geometry.
Usage and Examples
Here we provide some examples to help you understand how to use Turf’s functions:
turf.booleanContains(geom1, geom2)
The turf.booleanContains
function checks whether geom1
fully contains geom2
. It returns a boolean value indicating true if the first geometry contains the second one, or false otherwise. Here’s an example:
import * as turf from '@turf/turf';
const geom1 = turf.polygon([
[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]
]);
const geom2 = turf.point([5, 5]);
const contains = turf.booleanContains(geom1, geom2);
console.log(contains); // Output: true
turf.booleanOverlap(geom1, geom2)
The turf.booleanOverlap
function checks whether geom1
and geom2
overlap each other. It returns a boolean value indicating true if the two geometries overlap, or false if they don’t. Here’s an example:
import * as turf from '@turf/turf';
const geom1 = turf.polygon([
[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]
]);
const geom2 = turf.polygon([
[[5, 5], [5, 15], [15, 15], [15, 5], [5, 5]]
]);
const overlap = turf.booleanOverlap(geom1, geom2);
console.log(overlap); // Output: true
turf.distance(point1, point2, options)
The turf.distance
function calculates the distance between two points point1
and point2
. You can also specify an optional options
object to modify the behavior of the function. Here’s an example:
import * as turf from '@turf/turf';
const point1 = turf.point([0, 0]);
const point2 = turf.point([10, 10]);
const distance = turf.distance(point1, point2);
console.log(distance); // Output: 1568998.6790583738 meters
turf.area(polygon)
The turf.area
function calculates the area of a polygon
. It returns the area in square meters. Here’s an example:
import * as turf from '@turf/turf';
const polygon = turf.polygon([
[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]
]);
const area = turf.area(polygon);
console.log(area); // Output: 100000000 square meters
turf.convex(input)
The turf.convex
function creates the smallest convex polygon that contains all the input points. It takes an input
object containing an array of coordinates and returns a polygon feature. Here’s an example:
import * as turf from '@turf/turf';
const input = {
type: 'FeatureCollection',
features: [
turf.point([0, 0]),
turf.point([10, 0]),
turf.point([5, 5])
]
};
const convex = turf.convex(input);
console.log(convex); // Output: A polygon feature containing the convex hull
turf.center(input)
The turf.center
function calculates the center point of a geometry. It takes an input
geometry and returns a point feature representing the center. Here’s an example:
import * as turf from '@turf/turf';
const input = turf.polygon([
[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]
]);
const center = turf.center(input);
console.log(center); // Output: A point feature representing the center
turf.buffer(geom, distance, options)
The turf.buffer
function creates a buffer polygon around a geom
geometry. The buffer distance is specified in meters, and you can also provide an optional options
object to modify the buffer behavior. Here’s an example:
import * as turf from '@turf/turf';
const geom = turf.point([0, 0]);
const distance = 100; // Buffer distance in meters
const buffer = turf.buffer(geom, distance);
console.log(buffer); // Output: A polygon feature representing the buffer
Conclusion
Congratulations! You have learned the basics of Turf and how to use its core functions for geospatial analysis. Keep exploring Turf’s extensive library to discover additional capabilities and niche use cases.
If you need further assistance, refer to the official Turf documentation for more detailed explanations, examples, and references.