注意

This page was generated from docs/user_guide/sampling.ipynb.
Interactive online version: Binder badge

采样点#

学习如何使用GeoPandas抽样随机点。

下面的示例向您展示如何从GeoPandas GeoDataFrames中的形状中抽样随机位置。

导入包#

首先,我们需要导入将要使用的包:

[1]:
import geopandas
import geodatasets

在这个例子中,我们将使用geodatasets提供的纽约区示例数据(nybb)。

[2]:
nybb = geopandas.read_file(geodatasets.get_path("nybb"))
# simplify geometry to save space when rendering many interactive maps
nybb.geometry = nybb.simplify(200)

要查看这是什么样子,请可视化数据:

[3]:
nybb.explore()
[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook

随机点抽样#

要从GeoDataFrame中抽样点,请使用sample_points()方法。要指定样本大小,请提供明确的抽样点数量。例如,我们可以从每个特征随机抽样200个点:

[4]:
n200_sampled_points = nybb.sample_points(200)
m = nybb.explore()
n200_sampled_points.explore(m=m, color='red')
[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook

此功能也适用于线几何。比如,我们仅查看曼哈顿岛的边界:

[5]:
manhattan_parts = nybb.iloc[[3]].explode(ignore_index=True)
manhattan_island = manhattan_parts.iloc[[30]]
manhattan_island.boundary.explore()
[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook

可以使用相同的 sample_points() 方法从这个边界随机抽样:

[6]:
manhattan_border_points = manhattan_island.boundary.sample_points(200)
m = manhattan_island.explore()
manhattan_border_points.explore(m=m, color='red')
[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook

请记住,采样点作为一个单一的多部分几何体返回,并且线段之间的距离是沿着线段计算的。

[7]:
manhattan_border_points
[7]:
30    MULTIPOINT ((979244.151 197739.698), (979249.6...
Name: sampled_points, dtype: geometry

如果您想将单独的采样点分开,请在数据框上使用 .explode() 方法:

[8]:
manhattan_border_points.explode(ignore_index=True).head()
[8]:
0    POINT (979244.151 197739.698)
1    POINT (979249.606 197824.957)
2    POINT (979612.516 198757.385)
3    POINT (979655.282 198810.761)
4     POINT (979716.16 199139.367)
Name: sampled_points, dtype: geometry

可变数量的点#

如果您传递一个数组指定每个几何体的样本大小,您还可以从不同的几何体中抽样不同数量的点。

[9]:
variable_size = nybb.sample_points([10, 50, 100, 200, 500])
m = nybb.explore()
variable_size.explore(m=m, color='red')
[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook

从更复杂的点模式过程进行抽样#

最后,sample_points() 方法可以使用与上述描述不同的采样过程,只要它们在pointpats包中实现,用于空间点模式分析。例如,“cluster-poisson”过程是一种空间随机聚类过程,其中聚类的“种子”是随机选择的,然后这些聚类周围的点也再次随机分布。

要查看这是什么样的,请考虑以下内容,其中十个点将在纽约市的五个种子周围分布:

[10]:
sample_t = nybb.sample_points(method='cluster_poisson', size=50, n_seeds=5, cluster_radius=7500)
[11]:
m = nybb.explore()
sample_t.explore(m=m, color='red')
[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook