admin管理员组

文章数量:1406926

I'm using Turf.js for advanced geospatial analysis in my application, but can not find a method which checks if two polygons cross one another. Intersect method is not what I want, since if I have a tiny polygon and want to find polygon that crosses this tiny polygon, this method will return big overlapping polygons, that contain this smaller polygon.

Let me explain it visually. So, this is the polygon, that I have:

In this case, polygons intersect each other:

And in this case, I consider, that they do not intersect:

In the last case, the border of the green polygon does not cross the smaller polygon, so they do not intersect.

And my question is, if it is possible to implement this kind of intersection function in Turf.js?

I'm using Turf.js for advanced geospatial analysis in my application, but can not find a method which checks if two polygons cross one another. Intersect method is not what I want, since if I have a tiny polygon and want to find polygon that crosses this tiny polygon, this method will return big overlapping polygons, that contain this smaller polygon.

Let me explain it visually. So, this is the polygon, that I have:

In this case, polygons intersect each other:

And in this case, I consider, that they do not intersect:

In the last case, the border of the green polygon does not cross the smaller polygon, so they do not intersect.

And my question is, if it is possible to implement this kind of intersection function in Turf.js?

Share Improve this question asked Sep 27, 2017 at 15:16 JacobianJacobian 10.9k32 gold badges140 silver badges233 bronze badges 7
  • you consider it as intersection only if their perimeters intersect each other... I think that red is actually intersecting the green one... – Hitmands Commented Sep 27, 2017 at 15:37
  • this might be of help stackoverflow./questions/33629369/… – Hitmands Commented Sep 27, 2017 at 15:39
  • Possible duplicate of Find co-ordinates where LineString intersects a Polygon border in turfjs – Hitmands Commented Sep 27, 2017 at 15:39
  • In my situation these two polygons are not equivalent. I use the red one as a selection tool and want to find all polygons that intersect this selection area. By user requirements, larger polygons that contain this smaller one (used for selection), do not intersect it – Jacobian Commented Sep 27, 2017 at 15:58
  • 1 Though, possibly in newer version of Turf.js booleanCrosses will do the trick – Jacobian Commented Sep 27, 2017 at 16:02
 |  Show 2 more ments

4 Answers 4

Reset to default 3

You just need to check if the red polygon is contain to the green polygon, if it's true put intersection to false.

You can additionally check this turf.booleanContains(poly1, poly2)

const poly1 = turf.polygon(polygon1)
const poly2 = turf.polygon(polygon2)
intersected = turf.booleanWithin(poly1, poly2)

It will return true if poly1 {in your case it is red polygon} is pletely inside poly2 {In your case it is green polygon}. If they just intersect and do not contain by parent poly then it will return false.

Check This turf doc for more details

For anyone looking for a solution to this problem. You can use booleanOverlap to determine whether two polygons intersect.

const p0 = turf.polygon([[
  [0, 0],
  [0, 1],
  [1, 1],
  [1, 0],
  [0, 0]
]]);

const p1 = turf.polygon([[
  [0, 0],
  [0, 2],
  [0.5, 2],
  [0.5, 0],
  [0, 0]
]])

const p2 = turf.polygon([[
  [-2, -2],
  [-2, 2],
  [2, 2],
  [2, -2],
  [-2, -2]
]])

const p3 = turf.polygon([[
  [10, 10],
  [10, 11],
  [11, 11],
  [12, 12],
  [11, 10],
  [10, 10]
]]);

console.log(turf.booleanOverlap(p0, p0)); // false
console.log(turf.booleanOverlap(p0, p1)); // ture
console.log(turf.booleanOverlap(p0, p2)); // false
console.log(turf.booleanOverlap(p0, p3)); // false


本文标签: javascriptCheck if one polygon crosses another polygon in TurfjsStack Overflow