![](/img/trans.png)
[英]Making rows of points in dataframe into POLYGON using geopandas
[英]using Geopandas, How to randomly select in each polygon 5 Points by sampling method
我的建议涉及在形状的边界框中随机采样 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.