A polygon touches more than one point with Shapely

I have a list of Shapely polygons in Python. To find out which touch of the polygon is easy, use the method .touches(). However, I need something that returns Trueonly when polygons share more than one point (in other words, share a border). Let me illustrate:

In [1]: from shapely.geometry import Polygon
In [2]: polygons = [Polygon([(0,0),(0,1),(1,1),(1,0)]), Polygon([(1,0),(1,1),(2,1),(2,0)]), Polygon([(2,1),(2,2),(3,2),(3,1)])]

In [3]: polygons[0].touches(polygons[1])
Out[3]: True

In [4]: polygons[0].touches(polygons[2])
Out[4]: False

In [5]: polygons[1].touches(polygons[2])
Out[5]: True

In this case, the polygon 0 and 1 separates the two points (the whole border). Polygon 1 and 2 share only one point. What I am looking for a function that will give me True, False, Falsein the example above, or just something that returns the number of points of contact, then I can perform the rest of the logic of its own.

And, of course, any solution that does not involve manual iteration over all points is optimal - if I need to do this, it does not seem to match the purpose of using Shapely :-)

+5
source share
2 answers

I did not use a slender image, but you tried to see if the intersection of two polygons is a line?

+4
source

If you really want to check if two polygons have more than x the number of points, you can simply do this:

p0,p1,p2 = polygons
x = 2
len(set(p1.boundary.coords).intersection(p2.boundary.coords))>=x

But I think you might need to determine if the two edges are collinear (and overlapping).

This implementation of Andrew's suggestions is probably what you are looking for:

>>> type(p0.intersection(p1)) is geometry.LineString
True
>>> type(p1.intersection(p2)) is geometry.LineString
False
+7
source

All Articles