Geometry operations: intersect, union and difference
Quick answer
This tutorial covers polygon set operations: intersect for the shared area, union for the merged footprint, and difference for subtracting one polygon from another. It explains that intersect returns null when polygons only touch or are disjoint, why ring direction and self-intersections break topology, how cleanCoords and sliver filtering tidy results, and that attributes are not merged automatically. You will be able to run overlay analysis safely.
Key points
- Null-check turf.intersect results before reading geometry, since disjoint or edge-touching inputs return null.
- Detect self-intersections with turf.kinks and normalize rings with turf.rewind before overlay operations.
- Pass the minuend first in turf.difference(a, b) and reattach properties manually after union.
Example
var a = turf.polygon([[[116.35, 39.86], [116.45, 39.86], [116.45, 39.94],
[116.35, 39.94], [116.35, 39.86]]]);
var b = turf.polygon([[[116.40, 39.90], [116.50, 39.90], [116.50, 39.98],
[116.40, 39.98], [116.40, 39.90]]]);
var inter = turf.intersect(a, b);
console.log(inter.geometry.type); // Polygon
console.log(turf.area(inter) / 1e6); // 平方公里
var merged = turf.union(a, b);
console.log(merged.geometry.type); // Polygon,相邻时为单面
var rest = turf.difference(a, b);
console.log(turf.area(rest) / 1e6);var inter = turf.intersect(a, b);
var areaKm2 = inter ? turf.area(inter) / 1e6 : 0;
Notes
- Reversing the difference arguments yields a different but still valid polygon with no error.
- Mixing WGS84 degrees with projected meter coordinates produces meaningless intersections because Turf never checks CRS.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Geometry operations: intersect, union and difference. https://www.jingweizhi.com/en/turf/tutorials/geometry-ops.html (accessed 2026-11-03).