Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
@turf/difference
Advanced tools
@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.
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);
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 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.
Finds the difference between two polygons by clipping the second polygon from the first.
Parameters
polygon1
Feature<(Polygon | MultiPolygon)> input Polygon featurepolygon2
Feature<(Polygon | MultiPolygon)> Polygon feature to difference from polygon1Examples
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(polygon1, polygon2);
//addToMap
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.
Install this module individually:
$ npm install @turf/difference
Or install the Turf module that includes it as a function:
$ npm install @turf/turf
6.4.0
@turf/boolean-point-on-line
Added an epislon
option to help in floating point comparison.
(PR https://github.com/Turfjs/turf/pull/2051 - Author @okcoker)@turf/line-slice-along
Fixed a bug where the offset distance equal to the length of the line
(PR https://github.com/Turfjs/turf/pull/2030 - Author @EricPKerr)
@turf/helpers
Fixed the conversion ratio for converting meters to yards and vice-versa
(PR https://github.com/Turfjs/turf/pull/2046 - Author @anotherhale)
@turf/center-median
Fixed a missing TS type import
(PR https://github.com/Turfjs/turf/pull/2044 - Author @Seairth)
`@turf/bezier-spline Fix a bug ensuring the spline result reaches the end of the input (PR https://github.com/Turfjs/turf/pull/2090 - Author @the-nemz)
@turf/transform-rotate
and @turf/ellipse
) Improve documentation for angle parameter
(PR https://github.com/Turfjs/turf/pull/2016 - Author @pasieronen)
@turf/line-chunk
Fix an invalid anchor
(PR https://github.com/Turfjs/turf/pull/2071 - Author @GraxMonzo)
@turf/distance
Enhance distance doco so supported inputs are clearer
(PR https://github.com/Turfjs/turf/pull/2032 - Author @rowanwins)
@turf/concave
Replace deprecated topojson dependency
(PR https://github.com/Turfjs/turf/pull/2037 - Author @elliots)
Work towards enabling TS Strict Mode (PR https://github.com/Turfjs/turf/pull/2053 - Author @mfedderly)
FAQs
turf difference module
The npm package @turf/difference receives a total of 242,427 weekly downloads. As such, @turf/difference popularity was classified as popular.
We found that @turf/difference demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.