4

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:

enter image description here

In this case, polygons intersect each other:

enter image description here

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

enter image description here

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?

Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • you consider it as intersection only if their perimeters intersect each other... I think that red is actually intersecting the green one... – Hitmands Sep 27 '17 at 15:37
  • this might be of help https://stackoverflow.com/questions/33629369/find-co-ordinates-where-linestring-intersects-a-polygon-border-in-turfjs – Hitmands Sep 27 '17 at 15:39
  • Possible duplicate of [Find co-ordinates where LineString intersects a Polygon border in turfjs](https://stackoverflow.com/questions/33629369/find-co-ordinates-where-linestring-intersects-a-polygon-border-in-turfjs) – Hitmands Sep 27 '17 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 Sep 27 '17 at 15:58
  • Obviously, it is not a duplicate question, since even the person, whose question was accepted, says that "Ultimately, turfjs does not seem to have an API for doing this." – Jacobian Sep 27 '17 at 16:01
  • 1
    Though, possibly in newer version of Turf.js booleanCrosses will do the trick – Jacobian Sep 27 '17 at 16:02
  • I've just thought you would end up with the same conclusion. – Hitmands Sep 27 '17 at 16:02

4 Answers4

2

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

Tim
  • 162
  • 2
  • 10
0

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

0
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 completely 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

Samiksha Jagtap
  • 585
  • 2
  • 13
  • 29
0

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


KnownRock J
  • 126
  • 1
  • 4