Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

polygon-clipping

mfogel1.5mMIT0.15.7TypeScript support: included

Apply boolean Polygon clipping operations (intersection, union, difference, xor) to your Polygons & MultiPolygons.

polygon, clipping, boolean, intersection, union, difference, xor, geometry, martinez

readme

polygon-clipping

Apply boolean Polygon clipping operations (intersection, union, difference, xor) to your Polygons & MultiPolygons.

CI codecov License: MIT npm

Quickstart

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, ... */)

API

/* 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>)

Input

Each positional argument (<geom>) may be either a Polygon or a MultiPolygon. The GeoJSON spec is followed, with the following notes/modifications:

  • MultiPolygons may contain touching or overlapping Polygons.
  • rings are not required to be self-closing.
  • rings may contain repeated points, which are ignored.
  • rings may be self-touching and/or self-crossing. Self-crossing rings will be interpreted using the non-zero rule.
  • winding order of rings does not matter.
  • inner rings may extend outside their outer ring. The portion of inner rings outside their outer ring is dropped.
  • inner rings may touch or overlap each other.

Output

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:

  • outer rings will be wound counter-clockwise, and inner rings clockwise.
  • inner rings will not extend outside their outer ring.
  • rings will not overlap, nor share an edge with each other.
  • rings will be self-closing.
  • rings will not contain repeated points.
  • rings will not contain superfluous points (intermediate points along a straight line).
  • rings will not be self-touching nor self-crossing.
  • rings may touch each other, but may not cross each other.

In the event that the result of the operation is the empty set, output will be a MultiPolygon with no Polygons: [].

Correctness

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.

Performance

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.

Settings

Global settings are set via environment variables.

  • POLYGON_CLIPPING_MAX_QUEUE_SIZE and POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS: Aims to prevent infinite loops - usually caused by floating-point math round-off errors. Defaults are 1,000,000.

Changelog

This project adheres to Semantic Versioning.

The full changelog is available at CHANGELOG.md.

Authors

Sponsors

Please contact Mike Fogel if you or your company is interested in sponsoring work on specific bug fixes or feature requests.

Based on

changelog

Changelog

This project adheres to Semantic Versioning.

v0.15.7 (2023-12-18)

  • fix packaging issues with v0.15.6

v0.15.6 (2023-12-17)

  • re-release v0.15.4, with the packaging issues fixed

v0.15.5 (2023-12-16)

  • Revert to v0.15.3 to avoid packaging issues in v0.15.4

v0.15.4 (2023-12-16)

  • Fix error in some circumstances due to duplicate keys in splay tree #136
  • Update dependencies #158

v0.15.3 (2021-04-20)

v0.15.2 (2020-12-15)

  • Update dependencies #109

v0.15.1 (2020-07-08)

v0.15.0 (2020-07-03)

  • Limit data structure depth #97

v0.14.3 (2019-10-26)

v0.14.2 (2019-07-27)

  • Add typescript declarations #76
  • Upgrade project dependencies

v0.14.1 (2019-05-14)

  • Upgrade splaytree dependency #72

v0.14.0 (2019-03-30)

v0.13.0 (2019-02-26)

v0.12.3 (2019-02-14)

v0.12.2 (2019-01-29)

  • Add an unminified UMD to builds
  • Minimize builds to only required files

v0.12.1 (2019-01-29)

  • Fix error in release process of v0.12.0

v0.12.0 (2019-01-29)

  • Bug fixes (#65, #66)
  • Better packaging, switch to rollup (#67)
  • Upgrade development dependencies

v0.11.1 (2019-01-20)

  • Bug fixes (#60, #61, #62)
  • Fix package vulnerabilities (#63)

v0.11 (2019-01-13)

v0.10 (2019-01-07)

  • Support polygons with infinitely thin sections (#48)
  • Performance improvements (#31)
  • Bug fixes (#41, #49, #51, #53, #54)

v0.9.2 (2018-11-24)

  • Don't overwrite globals (#50)

v0.9.1 (2018-11-12)

v0.9 (2018-10-17)

  • Performance improvements (#26)
  • Bug fixes (#36, #38)

v0.8 (2018-08-30)

  • Export a default es6 module (#33)
  • Allow self-crossing rings using even-odd rule (#30)
  • Fix bug with nearly vertical segments being split (#29)
  • Fix bug with coincident segments being split slightly differently (#22)

v0.7 (2018-06-06)

v0.6.1 (2018-04-01)

  • Performance improvements
  • Drop (within rounding error) infinitely thin rings from output (#14)

v0.6 (2018-03-26)

  • Ensure output rings are not self-intersecting (#11)
  • Allow self-touching (but not crossing) input rings (#10)
  • Support empty MultiPolygons as input
  • Performance improvements (reduced memory footprint and lower CPU time)
  • Handle segments with many coincidents (#7)
  • Handle very thin input polygons (#6)

v0.5 (2018-03-01)

  • Remove clean() from module.exports (#3)
  • Expand difference() operation to optionally take multiple clippings (#1)
  • Use splay-tree instead of avl to power the sweep line status tree (#2)

v0.4 (2018-02-27)

  • First release as new package after fork from martinez