用形状指向多边形吗?

时间:2018-09-11 17:15:08

标签: python matlab matplotlib shapely.geometry

我正在运行以下脚本,我认为应该为多边形中的点返回TRUE,但返回FALSE。

from shapely import geometry

polygon = [(-1571236.8349707182, 8989180.222117377), (1599362.9654156454, 8924317.946336618), (-1653179.0745812152, 8922145.163675062), (-1626237.6614402141, 8986445.107619021)]

Point_X = -1627875.474
Point_Y = 8955472.968

line = geometry.LineString(polygon)
point = geometry.Point(Point_X, Point_Y)

print(line.contains(point))

当我在Matlab中绘制多边形和点时,得到以下形状

enter image description here

from matplotlib import pylab as plt
poly = [[-1571236.8349707182, 8989180.222117377],
    [1599362.9654156454, 8924317.946336618],
    [-1653179.0745812152, 8922145.163675062],
    [-1626237.6614402141, 8986445.107619021]]

x = [point[0] for point in poly]
y = [point[1] for point in poly]

p1 = [-1627875.474, 8955472.968]
p2 = [-1627875.474, 8955472.968]
plt.plot(x,y,p1[0],p1[1],'*r',p2[0],p2[1],'*b')
plt.show()

您知道为什么整形脚本返回FALSE吗?

1 个答案:

答案 0 :(得分:1)

您要测试的是您的要点是否在对象LineString上。

如果要测试点在多边形中,则必须使用类contains的{​​{1}}方法

Polygon

输出

from shapely import geometry

polygon = [(-1571236.8349707182, 8989180.222117377), (1599362.9654156454, 8924317.946336618), (-1653179.0745812152, 8922145.163675062), (-1626237.6614402141, 8986445.107619021)]

Point_X = -1627875.474
Point_Y = 8955472.968

line = geometry.LineString(polygon)
point = geometry.Point(Point_X, Point_Y)
polygon = geometry.Polygon(line)

print(polygon.contains(point))

请参阅https://shapely.readthedocs.io/en/latest/manual.html