I'm trying to find out whether two polygons cross each other. By 'cross' I mean their exteriors are allowed to touch each other, but their interior cannot:
Only the two rightmost solutions below are allowed:
I've tried using shapely intersects or crosses (and some others) but couldn’t find a built-in function that works (they usually relate to both interior and exterior).
Did you look at the touches
method? It seems to do what you want.
If not, you could "roll your own". For example, some variation of this:
def myTouches(poly1, poly2):
return poly1.intersects(poly2) and not poly1.crosses(poly2) and not poly1.contains(poly2)
Or, assuming your shapes are just polygons, you could look at the collection returned by intersection
. If it contains only LineStrings
or a single Point
then they just "touch". If it contains anything else (multiple Points
and/or other polygons) then they overlap.
Edit:
Now that I see your picture, you'll probably also need to use the disjoint
method in addition to touches
.
This is the solution that worked for the OP (taken from question):
if ((pol1.intersects(pol2) == False) and (pol1.disjoint(pol2) == True)) or ((pol1.intersects(pol2) == True) and (pol1.touches(pol2) == True)):
allowed = True
elif (pol1.intersects(polMe) == True) and (pol1.disjoint(polMe) == False) and (pol1.touches(polMe) == False):
allowed = False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With