python shapely说多边形不包含在另一个

ui7jx7zq  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(196)

我有两个多边形,我试图使用shapely来告诉我一个多边形是否接触到另一个多边形。
代码如下:

#!/usr/bin/env python

import shapely
import matplotlib.pyplot as plt

def main():
    a = shapely.geometry.Polygon((
        (239948.84366885605,6247904.745529621),(238281.02710020886,6250794.291071406),
        (238693.67295395266,6251899.199182685),(238292.52776364237,6253065.862501571),
        (239369.8867152306,6255078.983834209),(241149.62036734243,6258808.484109194),
        (246601.12030818601,6254149.774368979),(247369.5081433506,6252263.219573404),
        (246378.77811632192,6251486.8296787),(246522.3713691934,6251301.933776115),
        (245312.5127001227,6249410.396470293),(243242.96423660984,6248751.24094899),
        (239948.84366885605,6247904.745529621)
    ))
    b = shapely.geometry.Polygon((
        (243324,6254378),(243324,6254378),(243330,6254383),(243330,6254383),(243324,6254378)
    ))

    print(a.contains(b))
    print(a.intersects(b))
    print(a.overlaps(b))

    plt.plot(*a.exterior.xy, c='red')
    plt.plot(*b.exterior.xy, c='green')
    plt.gca().axis("equal")
    plt.show()

if __name__ == '__main__':
    main()

它输出:

False
False
False

然而,我们可以在绘制的图像上看到多边形B确实在多边形a内:
see screenshot
我期望多边形B在多边形a内,因此该程序输出:

True
True
True
5t7ly7z5

5t7ly7z51#

在阅读@blunova的评论后,似乎有一个面积为零的多边形是问题所在。
我补充道

b = b.buffer(0.01)

现在成功了!

相关问题