matlab 多边形中的点使用shapely?

voj3qocg  于 2022-11-24  发布在  Matlab
关注(0)|答案(2)|浏览(179)

我正在运行下面的脚本,我认为对于多边形中的点,它应该返回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中绘制多边形和点时,我得到了以下形状

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()

知道为什么shapely脚本返回FALSE吗?

fcy6dtqo

fcy6dtqo1#

您要测试的是您的点是否在对象LineString上。
如果要测试点是否在面中,则必须使用Polygon类的contains方法

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))

输出

True

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

8nuwlpux

8nuwlpux2#

此代码使用geopandas查找多边形内的点。

import geopandas as gpd
points_gpd = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x, y)) #point coordinates to geopandas dataframe
polygons_gpd = gpd.GeoDataFrame(geometry=polygons) #polygons is a list of shapely polygons
pt2poly = gpd.sjoin(points_gpd,polygons_gpd, predicate='within').index_right #for each point index in the points, it stores the polygon index containing that point

相关问题