This documents describes methods for generating random points in geospatial polygons using both QGIS and R.
Browser tab in
the panel at the leftSelect the polygon layer in the Layers panel at the
left
Select
Vector -- Research Tools -- Random Points in Polygons from
the menus at the top
Input polygon layer. Set
Number of points for each feature. Set a
Minimum distance between points if desired. Click
Run then Close.Random points in polygons layer by
right-clicking it in the layers panel at the left and choosing
Make permanent. Choose an output format and a file name,
then press Ok.Import libraries, set parameters etc.
library(sf)
## Warning: package 'sf' was built under R version 4.5.1
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
# Load the polygon
shp <- read_sf("../Input/Polygon/")
See https://stackoverflow.com/a/70073632/8299958 for this method.
set.seed(2025)
points <- st_sample(shp, size = 50, type = "random")
plot(st_geometry(shp))
plot(points, pch = 20, add= TRUE)
write_sf(points, "../Output/Points_R/Points_R.shp")