使用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);
};
感谢您的期待!
答案 0 :(得分:1)
这是因为每个多边形的第一个和最后一个点或位置必须相同。因此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);
};