Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
polygon-clipping
Advanced tools
Apply boolean Polygon clipping operations (intersection, union, difference, xor) to your Polygons & MultiPolygons.
The polygon-clipping npm package is a library for performing boolean operations on polygons, such as union, intersection, difference, and xor. It is useful for computational geometry tasks, particularly in GIS (Geographic Information Systems) and CAD (Computer-Aided Design) applications.
Union
The union operation combines two or more polygons into a single polygon that covers the area of all input polygons.
const polygonClipping = require('polygon-clipping');
const polygon1 = [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]];
const polygon2 = [[[2, 2], [6, 2], [6, 6], [2, 6], [2, 2]]];
const union = polygonClipping.union(polygon1, polygon2);
console.log(union);
Intersection
The intersection operation finds the overlapping area between two or more polygons.
const polygonClipping = require('polygon-clipping');
const polygon1 = [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]];
const polygon2 = [[[2, 2], [6, 2], [6, 6], [2, 6], [2, 2]]];
const intersection = polygonClipping.intersection(polygon1, polygon2);
console.log(intersection);
Difference
The difference operation subtracts the area of one polygon from another, resulting in the parts of the first polygon that are not overlapped by the second polygon.
const polygonClipping = require('polygon-clipping');
const polygon1 = [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]];
const polygon2 = [[[2, 2], [6, 2], [6, 6], [2, 6], [2, 2]]];
const difference = polygonClipping.difference(polygon1, polygon2);
console.log(difference);
XOR
The XOR operation finds the areas that are covered by either of the polygons but not by both.
const polygonClipping = require('polygon-clipping');
const polygon1 = [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]];
const polygon2 = [[[2, 2], [6, 2], [6, 6], [2, 6], [2, 2]]];
const xor = polygonClipping.xor(polygon1, polygon2);
console.log(xor);
Turf is a powerful geospatial analysis library for JavaScript. It provides a wide range of spatial operations, including boolean operations on polygons. Compared to polygon-clipping, Turf offers a broader set of geospatial functions but may be more complex to use for simple polygon operations.
JSTS (JavaScript Topology Suite) is a JavaScript library for processing geometries. It includes a variety of spatial operations, such as union, intersection, and difference. JSTS is more comprehensive in terms of geometry processing capabilities but can be more heavyweight compared to polygon-clipping.
Martinez Polygon Clipping is another library for performing boolean operations on polygons. It is similar to polygon-clipping in terms of functionality but uses a different algorithm (Martinez-Rueda) for polygon clipping, which may result in different performance characteristics.
Apply boolean Polygon clipping operations (intersection
, union
, difference
, xor
) to your Polygons & MultiPolygons.
const polygonClipping = require('polygon-clipping')
const poly1 = [[[0,0],[2,0],[0,2],[0,0]]]
const poly2 = [[[-1,0],[1,0],[0,1],[-1,0]]]
polygonClipping.union (poly1, poly2 /* , poly3, ... */)
polygonClipping.intersection(poly1, poly2 /* , poly3, ... */)
polygonClipping.xor (poly1, poly2 /* , poly3, ... */)
polygonClipping.difference (poly1, poly2 /* , poly3, ... */)
/* All functions take one or more [multi]polygon(s) as input */
polygonClipping.union (<geom>, ...<geoms>)
polygonClipping.intersection(<geom>, ...<geoms>)
polygonClipping.xor (<geom>, ...<geoms>)
/* The clipGeoms will be subtracted from the subjectGeom */
polygonClipping.difference(<subjectGeom>, ...<clipGeoms>)
Each positional argument (<geom>
) may be either a Polygon or a MultiPolygon. The GeoJSON spec is followed, with the following notes/modifications:
For non-empty results, output will always be a MultiPolygon containing one or more non-overlapping, non-edge-sharing Polygons. The GeoJSON spec is followed, with the following notes/modifications:
In the event that the result of the operation is the empty set, output will be a MultiPolygon with no Polygons: []
.
Run: npm test
The tests are broken up into unit tests and end-to-end tests. The end-to-end tests are organized as GeoJSON files, to make them easy to visualize thanks to GitHub's helpful rendering of GeoJSON files. Browse those tests here.
The Martinez-Rueda-Feito polygon clipping algorithm is used to compute the result in O((n+k)*log(n))
time, where n
is the total number of edges in all polygons involved and k
is the number of intersections between edges.
This project adheres to Semantic Versioning.
The full changelog is available at CHANGELOG.md.
Please contact Mike Fogel if you or your company is interested in sponsoring work on specific bug fixes or feature requests.
FAQs
Apply boolean Polygon clipping operations (intersection, union, difference, xor) to your Polygons & MultiPolygons.
We found that polygon-clipping demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.