整齐的LineString交集工作不正确吗?

时间:2019-04-23 05:55:57

标签: python polygon intersection shapely

我在与身材匀称的工作中遇到了一个奇怪的问题。有两个点p1和p2,其中第一个属于多边形,第二个不属于多边形。当我试图找到包含这两个点的LineString之间的相交作为带有多边形边界线的端点时,我收到一条消息,提示未找到相交。我想知道,这怎么可能?

from shapely.geometry import Polygon as SPolygon, Point, LineString

p1 = Point(5.414213562373095, 2.585786437626905)
p2 = Point(15.17279752753168, -7.172797527531679)

l = LineString([p1, p2])

l1 = LineString([(2, 2), (2, 6)])
l2 = LineString([(2, 6), (6, 6)])
l3 = LineString([(6, 6), (6, 2)])
l4 = LineString([(6, 2), (2, 2)])


sp = SPolygon([(2, 2), (2, 6), (6, 6), (6, 2)])

print "Polygon contains p1:", sp.contains(p1)
print "Polygon contains p2:", sp.contains(p2)

for i, line in enumerate((l1, l2, l3, l4)):
    res = l.intersects(line)
    print "Line {0} intersects l1: {1}".format(i, res) 

这是输出:

Polygon contains p1: True
Polygon contains p2: False
Line 0 intersects l1: False
Line 1 intersects l1: False
Line 2 intersects l1: False
Line 3 intersects l1: False

1 个答案:

答案 0 :(得分:0)

我将l.intersects(line)更改为l.intersection(line),并且确实在

LINESTRING (6 6, 6 2) at Point (6, 2) 

不确定为什么.instersects()的行为有所不同。

然后我将p1和p2四舍五入

p1 = Point(round(5.414213562373095, 2), round(2.585786437626905, 2))
p2 = Point(round(15.17279752753168, 2), round(-7.172797527531679, 2))

我在

处有两个路口
LINESTRING (6 6, 6 2) at POINT (6 2)
LINESTRING (6 2, 2 2) at POINT (6 2)

此修复程序也适用于.intersects()(2个真值)

形状有点浮躁,但我通常可以通过四舍五入来解决问题。尽管这可能对您来说不可接受。