One call per operation
Pass GeoJSON Polygon or MultiPolygon coordinates, get
MultiPolygon coordinates back, [] when the result is empty.
Types included, ESM and CommonJS.
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.
Playgroundstart here
Edit both shapes vertex by vertex, load any test case or paste your own GeoJSON, and copy the result.
Test case viewerdebug view
Every regression case and fixture: inputs, result and expected result. Each view has its own URL.
Asia minus Asiastress test
28k vertices against a copy of itself shifted east, so that nearly every edge intersects. Shown with a 0.3° shift.
Under the hood
- The algorithm A plane sweep in O((n + k) log n), extended here to multipolygons without cascading.
- Regression cases GeoJSON inputs with the expected result for each operation.
- Benchmark source The comparison above, run against the built bundle.
- CodePen A playground to fork.