Skip to main content
Version: 6.5.0

difference

Description

Finds the difference between two polygons by clipping the second polygon from the first.

Parameters

NameTypeDescription
polygon1Feature<Polygon | MultiPolygon>input Polygon feature
polygon2Feature<Polygon | MultiPolygon>Polygon feature to difference from polygon1

Returns

    Feature<Polygon | MultiPolygon> | null a Polygon or MultiPolygon feature showing the area of polygon1 excluding the area of polygon2 (if empty returns null)

Examples

var polygon1 = turf.polygon(
[
[
[128, -26],
[141, -26],
[141, -21],
[128, -21],
[128, -26],
],
],
{
fill: "#F00",
"fill-opacity": 0.1,
},
);
var polygon2 = turf.polygon(
[
[
[126, -28],
[140, -28],
[140, -20],
[126, -20],
[126, -28],
],
],
{
fill: "#00F",
"fill-opacity": 0.1,
},
);

var difference = turf.difference(polygon1, polygon2);

Installation

$ npm install @turf/difference

import { difference } from "@turf/difference";
const result = difference(...);
$ npm install @turf/turf

import * as turf from "@turf/turf";
const result = turf.difference(...);