无法删除多边形之间的区域交集

5

我有两个多边形,它们交叉但无法去除。我试图通过从第一个多边形中减去第二个多边形的区域来找到它们之间的差异,并再次找到它们的交集 - 结果交集仍然存在并且有公共区域。为什么会发生这种情况?如何消除两个多边形之间的交集,并使它们的边界相接?

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???
}
1个回答

0

看起来这只是精度问题:如果您检查交集的区域

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

你会看到这是一个0.28925415367002694平方米的面积。

如果这是一个可行的解决方案,你可以使用一个阈值:

const threshold = 1; // configurable 1 square meter

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

希望这可以帮助到你。

这是一个可能的解决方案,但我正在寻找一种完全从另一个多边形中减去的方法。 - Max Max

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接