Introduction

This documents describes methods for generating random points in geospatial polygons using both QGIS and R.

QGIS

  1. Open the shapefile in QGIS e.g. from the Browser tab in the panel at the left

  1. Select the polygon layer in the Layers panel at the left

  2. Select Vector -- Research Tools -- Random Points in Polygons from the menus at the top

  1. Ensure the correct polygon is selected in Input polygon layer. Set Number of points for each feature. Set a Minimum distance between points if desired. Click Run then Close.

  1. Re-project the features by clicking the CRS label at the bottom right. EPSG:4326 will give coordinates in latitude and longitude.

  1. Save the new 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.

R

Startup

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

Input

# Load the polygon
shp <- read_sf("../Input/Polygon/")

Create the Random Points

See https://stackoverflow.com/a/70073632/8299958 for this method.

set.seed(2025)
points <- st_sample(shp, size = 50, type = "random")

Plot To Check

plot(st_geometry(shp))
plot(points, pch = 20, add= TRUE)

Write Output

write_sf(points, "../Output/Points_R/Points_R.shp")