What is @turf/difference?
@turf/difference is a module in the Turf.js library that allows you to compute the geometric difference between two polygons. This is useful in geospatial analysis for determining the area that is unique to one polygon when compared to another.
What are @turf/difference's main functionalities?
Compute Difference Between Two Polygons
This feature allows you to compute the difference between two polygons. The resulting polygon represents the area that is unique to the first polygon when compared to the second polygon.
const turf = require('@turf/turf');
const polygon1 = turf.polygon([[
[-82.574787, 35.594087],
[-82.574787, 35.615581],
[-82.545261, 35.615581],
[-82.545261, 35.594087],
[-82.574787, 35.594087]
]]);
const polygon2 = turf.polygon([[
[-82.560024, 35.585153],
[-82.560024, 35.602602],
[-82.52964, 35.602602],
[-82.52964, 35.585153],
[-82.560024, 35.585153]
]]);
const difference = turf.difference(polygon1, polygon2);
console.log(difference);
Other packages similar to @turf/difference
jsts
JSTS is a JavaScript library of spatial predicates and functions for processing geometry. It is a port of the Java Topology Suite (JTS). JSTS provides a more comprehensive set of geometric operations compared to @turf/difference, including union, intersection, and buffer operations.
martinez-polygon-clipping
Martinez Polygon Clipping is a library for performing boolean operations on polygons, such as union, intersection, and difference. It is known for its performance and accuracy, making it a good alternative to @turf/difference for complex polygon operations.
@turf/difference
difference
Finds the difference between multiple polygons by clipping the subsequent polygon from the first.
Parameters
Examples
var polygon1 = turf.polygon([[
[128, -26],
[141, -26],
[141, -21],
[128, -21],
[128, -26]
]], {
"fill": "#F00",
"fill-opacity": 0.1
});
var polygon2 = turf.polygon([[
[126, -28],
[140, -28],
[140, -20],
[126, -20],
[126, -28]
]], {
"fill": "#00F",
"fill-opacity": 0.1
});
var difference = turf.difference(turf.featureCollection([polygon1, polygon2]));
var addToMap = [polygon1, polygon2, difference];
Returns (Feature<(Polygon | MultiPolygon)> | null) a Polygon or MultiPolygon feature showing the area of polygon1
excluding the area of polygon2
(if empty returns null
)
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Installation
Install this single module individually:
$ npm install @turf/difference
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf