5

I have two polygons with invisible and unremovable intersection. I tried to find difference between them by removing area of second from first, and find intersection again - result intersection still present and have common area. Why this is happening? How to remove the intersection between two polygons and to make their borders touch?

const turf = require("@turf/turf");

const poly1 = [
    [
        [37.367249, 55.615589],
        [37.372462, 55.612478],
        [37.372463, 55.61248],
        [37.453365, 55.564205],
        [37.45336, 55.564206],
        [37.459431, 55.560583],
        [37.558005, 55.682037],
        [37.367249, 55.615589]
    ]
];
const poly2 = [
    [
        [37.336522, 55.603857],
        [37.360725, 55.57621],
        [37.408614, 55.591334],
        [37.371557, 55.613064],
        [37.336522, 55.603857]
    ]
];

const difference = turf.difference(turf.polygon(poly1), turf.polygon(poly2)); // removed poly2 from poly1, difference now is Feature<Polygon>
const intersection = turf.intersect(turf.polygon(difference.geometry.coordinates), turf.polygon(poly2));

if (intersection) { //intersection is geometry collection with polygons
    // why???
}
Max Max
  • 389
  • 1
  • 2
  • 12

1 Answers1

0

It seems it is just matter of precision: if you check the area of intersection

if (intersection) {
  console.log(turf.area(intersection));
}

you will see it is a 0.28925415367002694 square meters area.

If it is a viable solution you can use a threshold:

const threshold = 1; // configurable 1 square meter

if (intersection && turf.area(intersection) > threshold) {
  console.log("there is intersection");
}

Hope this helps.

Daniele Ricci
  • 15,422
  • 1
  • 27
  • 55
  • This is a possible solution, but I'm looking for a way to completely subtract one polygon from another – Max Max Jul 08 '20 at 07:59