gpt4 book ai didi

python-3.x - 使用Geopandas,如何通过采样方法在每个多边形中随机选择5个点

转载 作者:行者123 更新时间:2023-12-05 03:57:05 31 4
gpt4 key购买 nike

Shapefile data image-Kopargaon

我想根据随机抽样方法在每个多边形中选择 5 个点。并且需要在每个多边形中有 5 个点坐标(纬度、经度)来识别种植的裁剪。

关于使用 geopandas 执行此操作的任何想法?

非常感谢。

最佳答案

我的建议涉及在形状边界框内随机抽取 x 和 y 坐标,然后检查抽样点是否确实在形状内。如果采样点在形状内,则返回它,否则重复直到找到形状内的点。对于采样,我们可以使用均匀分布,这样形状中的所有点都有相同的采样概率。这是函数:

from shapely.geometry import Point
def random_point_in_shp(shp):
within = False
while not within:
x = np.random.uniform(shp.bounds[0], shp.bounds[2])
y = np.random.uniform(shp.bounds[1], shp.bounds[3])
within = shp.contains(Point(x, y))
return Point(x,y)

下面是一个示例,说明如何将此函数应用于名为 geo_df 的示例 GeoDataFrame,以便为每个条目获取 5 个随机点:

for num in range(5):
geo_df['Point{}'.format(num)] = geo_df['geometry'].apply(random_point_in_shp)

可能有更有效的方法来执行此操作,但根据您的应用程序,该算法可能足够快。我的测试文件包含约 2300 个条目,为每个条目生成五个随机点在我的机器上花费了大约 15 秒。

关于python-3.x - 使用Geopandas,如何通过采样方法在每个多边形中随机选择5个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802921/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com