Skip to contents

Creates a grid of points with a user-defined spacing within the boundaries of a raster or polygon object.

Usage

generate_spaced_points(input_obj, spacing, seed = NULL)

Arguments

input_obj

A spatial object. Can be a RasterLayer, RasterBrick, RasterStack, sf/sfc object, or SpatialPolygons/SpatialPolygonsDataFrame.

spacing

Numeric. Distance between points in map units (typically meters).

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 describing the spacing.

Details

This function creates a regular grid based on the extent of the input object, then keeps only points that fall within the boundaries. The grid is offset by spacing/2 from the extent edges to ensure even distribution.

Examples

if (FALSE) {
# Generate points every 500 meters
r <- raster::raster(matrix(1:100, 10, 10))
points <- generate_spaced_points(r, spacing = 500, seed = 123)

# Generate points in a polygon with 100m spacing
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_spaced_points(poly, spacing = 100)
}