2

使用Turf.js版本 3.0.12。下面的函数应该返回两个多边形之间的交集,如果它们不相交,则返回 null。多边形确实相交。我尝试了几种不同的多边形并收到相同的错误:

“错误:第一个和最后一个位置不相等。”

我引用了这个例子:http ://turfjs.org/docs#intersect

checkForIntersection = () => {
    const boundingBox1 = turf.polygon([
      [
        [-11638128.151894445, 4697704.8042823635],
        [-11538591.465504704, 4932847.347657125],
        [-11773734.008879466, 5032384.034046866],
        [-11873270.695269207, 4797241.490672104]
      ]
    ]);

    const boundingBox2 = turf.polygon([
      [
        [-11545948.977350365, 4658759.839788924],
        [-11483057.72508946, 5032936.709591224],
        [-11857234.59489176, 5095827.961852129],
        [-11920125.847152665, 4721651.092049829]
      ]
    ]);

    const intersection = turf.intersect(boundingBox1, boundingBox2);

    console.log("intersection: ", intersection);
  };

感谢您的关注!

4

1 回答 1

2

这是因为每个多边形的第一个和最后一个点或位置必须相同。所以 boundingBox1 & boundingBox2 将是:

checkForIntersection = () => {
    const boundingBox1 = turf.polygon([
      [
        [-11638128.151894445, 4697704.8042823635],
        [-11538591.465504704, 4932847.347657125],
        [-11773734.008879466, 5032384.034046866],
        [-11873270.695269207, 4797241.490672104],
        [-11638128.151894445, 4697704.8042823635],
      ]
    ]);

    const boundingBox2 = turf.polygon([
      [
        [-11545948.977350365, 4658759.839788924],
        [-11483057.72508946, 5032936.709591224],
        [-11857234.59489176, 5095827.961852129],
        [-11920125.847152665, 4721651.092049829],
        [-11545948.977350365, 4658759.839788924],
      ]
    ]);

    const intersection = turf.intersect(boundingBox1, boundingBox2);

    console.log("intersection: ", intersection);
  };

于 2020-07-08T18:09:35.297 回答