Learn how to sample random points using GeoPandas.
The example below shows you how to sample random locations from shapes in GeoPandas GeoDataFrames.
To begin with, we need to import packages we’ll use:
import geopandas import geodatasets
For this example, we will use the New York Borough example data (
nybb) provided by geodatasets.
nybb = geopandas.read_file(geodatasets.get_path("nybb")) # simplify geometry to save space when rendering many interactive maps nybb.geometry = nybb.simplify(200)
ERROR 1: PROJ: proj_create_from_database: Open of /home/docs/checkouts/readthedocs.org/user_builds/geopandas/conda/stable/share/proj failed
To see what this looks like, visualize the data:
Sampling random points#
To sample points from within a GeoDataFrame, use the
sample_points() method. To specify the sample sizes, provide an explicit number of points to sample. For example, we can sample 200 points randomly from each feature:
n200_sampled_points = nybb.sample_points(200) m = nybb.explore() n200_sampled_points.explore(m=m, color='red')
This functionality also works for line geometries. For example, let’s look only at the boundary of Manhattan Island:
manhattan_parts = nybb.iloc[].explode(ignore_index=True) manhattan_island = manhattan_parts.iloc[] manhattan_island.boundary.explore()
Sampling randomly from along this boundary can use the same
manhattan_border_points = manhattan_island.boundary.sample_points(200) m = manhattan_island.explore() manhattan_border_points.explore(m=m, color='red')
Keep in mind that sampled points are returned as a single multi-part geometry, and that the distances over the line segments are calculated along the line.
30 MULTIPOINT (978930.714 196448.698, 979189.591 ... Name: sampled_points, dtype: geometry
If you want to separate out the individual sampled points, use the
.explode() method on the dataframe:
0 POINT (978930.714 196448.698) 1 POINT (979189.591 195988.865) 2 POINT (979240.162 197677.353) 3 POINT (979292.792 198499.909) 4 POINT (979313.106 198817.398) Name: sampled_points, dtype: geometry
Variable number of points#
You can also sample different number of points from different geometries if you pass an array specifying the size of the sample per geometry.
variable_size = nybb.sample_points([10, 50, 100, 200, 500]) m = nybb.explore() variable_size.explore(m=m, color='red')
Sampling from more complicated point pattern processes#
sample_points() method can use different sampling processes than those described above, so long as they are implemented in the
pointpats package for spatial point pattern analysis. For example, a “cluster-poisson” process is a spatially-random cluster process where the “seeds” of clusters are chosen randomly, and then points around these clusters are distributed according again randomly.
To see what this looks like, consider the following, where ten points will be distributed around five seeds within each of the boroughs in New York City:
sample_t = nybb.sample_points(method='cluster_poisson', size=50, n_seeds=5, cluster_radius=7500)
m = nybb.explore() sample_t.explore(m=m, color='red')