Boolean operations on polygons.

Union, intersection, difference and xor of GeoJSON polygons: holes, multipolygons, self-intersections, overlapping edges. Drag the shapes or their vertices, or open the playground.

F. Martínez, A. J. Rueda, F. R. Feito. A new algorithm for computing Boolean operations on polygons. Computers & Geosciences 35(6), 1177–1185, 2009. F. Martínez, C. Ogayar, J. R. Jiménez, A. J. Rueda. A simple algorithm for Boolean operations on polygons. Advances in Engineering Software 64, 11–19, 2013.

A  B  result  
7 KBgzipped, with its three small dependencies
3×faster than JSTS when nearly every edge intersects
Exactorientation tests, with robust-predicates
RFC 7946output: closed rings, CCW exteriors, CW holes

One call per operation

Pass GeoJSON Polygon or MultiPolygon coordinates, get MultiPolygon coordinates back, [] when the result is empty. Types included, ESM and CommonJS.

npm install martinez-polygon-clipping
import { union, intersection, diff, xor } from "martinez-polygon-clipping";

const a = [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]];
const b = [[[2, 2], [6, 2], [6, 6], [2, 6], [2, 2]]];

union(a, b);
// → [[[[0, 0], [4, 0], [4, 2], [6, 2], [6, 6],
//      [2, 6], [2, 4], [0, 4], [0, 0]]]]

Benchmarks

Operations per second, higher is better. All four libraries produce the same result areas on these inputs.

Apple M3, Node 22.17. Reproduce with npm run bench. The “shifted” cases clip Asia against a copy of itself moved 0.05° east.

Demos

The thumbnails are computed by the library as this page loads.

Under the hood