Skip to contents

This function simulates point patterns in space using the values of an input raster as weights or probabilities for selecting a point in a given location. It was designed to simulate points based on neutral landscape models but it works with other input rasters as well.

Usage

set_points_from_raster(base_raster, n_features = 1000)

Arguments

base_raster

[RasterLayer]
Input raster used for defining the weights.

n_features

[integer(1)=1000]
Total number of features to spread in space.

Value

A data.frame with the (x,y) coordinates of the simulated points.

Details

The function works by first selecting random pixels in the landscape and finding their centers, then adding random variation within each pixel to define the final point locations. It was based on this StackExchange very useful answer from "Spacedman": https://gis.stackexchange.com/questions/224321/randomly-generate-points-using-weights-from-raster

TO IMPROVE: implement with terra package

Examples

#-----
# minimal example

# example based on
# https://gis.stackexchange.com/questions/224321/randomly-generate-points-using-weights-from-raster
library(raster)
#> Loading required package: sp
#> 
#> Attaching package: ‘raster’
#> The following object is masked from ‘package:dplyr’:
#> 
#>     select

# raster
set.seed(12)
r <- raster::raster(matrix(runif(12),3,4))

# points
pts <- set_points_from_raster(r, n_features = 300)

# plot
raster::plot(r)
points(pts)


# or
# library(landscapetools)
# library(ggplot2)
# landscapetools::show_landscape(r) +
#   geom_point(aes(x, y), data = pts)

# with terra
r <- terra::rast(r)
# points
pts <- set_points_from_raster(r, n_features = 300)

#-----
# using NLMR
library(NLMR)

# example NLM
set.seed(123)
nlm1 <- NLMR::nlm_mpd(100, 100, 100, roughness = .5)
#> Warning: nlm_mpd changes the dimensions of the RasterLayer if even ncols/nrows are choosen.

# points
pts <- set_points_from_raster(nlm1, n_features = 1000)

# plot
raster::plot(nlm1)
points(pts)


# or
# landscapetools::show_landscape(nlm1) +
#   geom_point(aes(x, y), data = pts)