繁体   English   中英

使用 Geopandas,如何通过抽样方法在每个多边形 5 个点中随机 select

[英]using Geopandas, How to randomly select in each polygon 5 Points by sampling method

Shapefile 数据图像-Kopargaon

我想基于随机抽样方法在每个多边形中 select 5 个点。 并且在每个多边形中需要 5 个点坐标(纬度、经度)来识别种植的作物。

使用 geopandas 有什么想法吗?

非常感谢。

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

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)

下面是一个示例,如何将此 function 应用于名为geo_df的示例GeoDataFrame以获得每个条目的 5 个随机点:

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

可能有更有效的方法可以做到这一点,但根据您的应用程序,算法可能足够快。 使用包含约 2300 个条目的测试文件,在我的机器上为每个条目生成五个随机点大约需要 15 秒。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM