
Generate random points within spatial boundaries
Source:R/1.2_function_array_generation.R
generate_random_points.Rd
Creates a specified number of randomly distributed points within the boundaries of a raster or polygon object.
Arguments
- input_obj
A spatial object. Can be a RasterLayer, RasterBrick, RasterStack, sf/sfc object, or SpatialPolygons/SpatialPolygonsDataFrame.
- n_points
Integer. The exact number of points to generate.
- seed
Numeric. Optional random seed for reproducible results. Default is NULL.
Value
An sf object containing the generated points with columns:
- x
X coordinates
- y
Y coordinates
- station_id
Unique identifier for each point
- raster_value
Raster values at point locations (only if input was raster)
The object also has a "method_info" attribute indicating random distribution.
Details
This function uses sf::st_sample with type = "random" to create randomly distributed points. If the input is a raster, values are extracted at point locations.
Examples
if (FALSE) {
# Generate 100 random points in a raster
r <- raster::raster(matrix(1:100, 10, 10))
points <- generate_random_points(r, n_points = 100, seed = 456)
# Generate random points in a polygon
poly <- sf::st_sfc(sf::st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0)))))
points <- generate_random_points(poly, n_points = 50)
}