| Title: | Simulating and Sampling Spatial Data |
|---|---|
| Description: | Provides tools to simulate and sample spatial data. Additional tools for creating various spatial proxies are also included. |
| Authors: | Jakub Nowosad [aut, cre] (ORCID: <https://orcid.org/0000-0002-1057-3721>), Carles Milà [ctb] |
| Maintainer: | Jakub Nowosad <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.3 |
| Built: | 2026-06-10 18:48:26 UTC |
| Source: | https://github.com/nowosad/simsam |
Blend rasters based on a provided formula
blend_rasters(x, formula, ...)blend_rasters(x, formula, ...)
x |
A |
formula |
A formula specifying how to combine the rasters |
... |
Additional |
A SpatRaster object with the combined layers
rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) sf1 = sim_covariates( rast_grid, n = 4, method = simulate_gaussian(range = 25) ) g1 = blend_rasters(sf1, ~ cov1 + cov4) g2 = blend_rasters(g1, ~ outcome^2 + (cov2 + 2), sf1) terra::plot(g1) terra::plot(g2)rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) sf1 = sim_covariates( rast_grid, n = 4, method = simulate_gaussian(range = 25) ) g1 = blend_rasters(sf1, ~ cov1 + cov4) g2 = blend_rasters(g1, ~ outcome^2 + (cov2 + 2), sf1) terra::plot(g1) terra::plot(g2)
The make_proxy() function generates spatial proxies for a given raster object.
It generates spatial proxies such as:
coordinates
Euclidean Distance Fields (EDF)
Oblique Geographic Coordinates (OGC)
make_proxy(rast_grid, method = proxy_coordinates()) add_proxy(rast_grid, method = proxy_coordinates())make_proxy(rast_grid, method = proxy_coordinates()) add_proxy(rast_grid, method = proxy_coordinates())
rast_grid |
A |
method |
A proxy method created by |
A SpatRaster object
SpatRaster
Behrens, T., Schmidt, K., Viscarra Rossel, R. A., Gries, P., Scholten, T., & MacMillan, R. A. (2018). Spatial modelling with Euclidean distance fields and machine learning. European journal of soil science, 69(5), 757-770.
Møller, A. B., Beucher, A. M., Pouladi, N., & Greve, M. H. (2020). Oblique geographic coordinates as covariates for digital soil mapping. Soil, 6(2), 269-289.
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) proxy_coords = make_proxy(rast_grid, proxy_coordinates()) proxy_edf = make_proxy(rast_grid, proxy_edf()) proxy_ogc = make_proxy(rast_grid, proxy_ogc(n = 5)) terra::plot(proxy_coords) terra::plot(proxy_edf) terra::plot(proxy_ogc)rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) proxy_coords = make_proxy(rast_grid, proxy_coordinates()) proxy_edf = make_proxy(rast_grid, proxy_edf()) proxy_ogc = make_proxy(rast_grid, proxy_ogc(n = 5)) terra::plot(proxy_coords) terra::plot(proxy_edf) terra::plot(proxy_ogc)
Returns a function that generates coordinate layers (X, Y) for a raster.
proxy_coordinates()proxy_coordinates()
A function that accepts rast_grid and returns a SpatRaster
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) make_proxy(rast_grid, proxy_coordinates())rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) make_proxy(rast_grid, proxy_coordinates())
Returns a function that generates Euclidean Distance Fields from specified locations (default: four corners and center of the raster extent).
proxy_edf(coords = NULL)proxy_edf(coords = NULL)
coords |
Optional coordinates. Can be a matrix, data frame, |
A function that accepts rast_grid and returns a SpatRaster
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) # Default: four corners and center make_proxy(rast_grid, proxy_edf()) # Custom coordinates custom_coords = matrix(c(50, 50, 150, 150), ncol = 2, byrow = TRUE) rownames(custom_coords) = c("point1", "point2") make_proxy(rast_grid, proxy_edf(coords = custom_coords))rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) # Default: four corners and center make_proxy(rast_grid, proxy_edf()) # Custom coordinates custom_coords = matrix(c(50, 50, 150, 150), ncol = 2, byrow = TRUE) rownames(custom_coords) = c("point1", "point2") make_proxy(rast_grid, proxy_edf(coords = custom_coords))
Returns a function that generates Oblique Geographic Coordinates (OGC) as covariates.
proxy_ogc(n = 5)proxy_ogc(n = 5)
n |
Number of angles (default: 5) |
A function that accepts rast_grid and returns a SpatRaster
Møller, A. B., Beuchert, A. M., Pouladi, N., & Greve, M. H. (2020). Oblique geographic coordinates as covariates for digital soil mapping. Soil, 6(2), 269-289.
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) make_proxy(rast_grid, proxy_ogc(n = 5))rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) make_proxy(rast_grid, proxy_ogc(n = 5))
Create a sample of a spatial field.
sam_field(x, size, method = sample_random())sam_field(x, size, method = sample_random())
x |
A raster object ( |
size |
Number of samples to create. |
method |
Sampling method (function created by |
An sf object with sampled points
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field( rast_grid, 100, method = sample_jittered(amount = 5) )rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field( rast_grid, 100, method = sample_jittered(amount = 5) )
Returns a function that performs clustered sampling on a spatial field. Cluster centers are randomly distributed, then samples are drawn within a buffer radius around each cluster.
sample_clustered(nclusters, radius, ...)sample_clustered(nclusters, radius, ...)
nclusters |
Number of clusters |
radius |
Cluster radius (in map units) |
... |
Additional arguments passed to |
A function that accepts x (SpatRaster) and size and returns an sf object
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field(rast_grid, 200, method = sample_clustered(nclusters = 5, radius = 10))rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field(rast_grid, 200, method = sample_clustered(nclusters = 5, radius = 10))
Returns a function that performs jittered sampling on a spatial field. Regular points are generated first, then jittered by a random amount.
sample_jittered(amount, ...)sample_jittered(amount, ...)
amount |
Jitter amount in map units |
... |
Additional arguments passed to |
A function that accepts x (SpatRaster) and size and returns an sf object
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field(rast_grid, 100, method = sample_jittered(amount = 5))rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field(rast_grid, 100, method = sample_jittered(amount = 5))
Returns a function that performs random sampling on a spatial field.
sample_random(...)sample_random(...)
... |
Additional arguments passed to |
A function that accepts x (SpatRaster) and size and returns an sf object
rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field(rast_grid, 100, method = sample_random())rast_grid = terra::rast( ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100 ) sam_field(rast_grid, 100, method = sample_random())
Generic function for simulating spatial covariates using a specified method.
sim_covariates(rast_grid, n = 6, method)sim_covariates(rast_grid, n = 6, method)
rast_grid |
A |
n |
The number of covariates to simulate (must be >= 1) |
method |
A simulation factory function —- call the function with
parameters to create the method (remember to not pass the function itself).
For example: |
A SpatRaster with n layers named cov1, cov2, ..., covN
rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) # Gaussian simulation sf1 = sim_covariates(rast_grid, n = 4, method = simulate_gaussian(range = 25)) sf2 = sim_covariates(rast_grid, n = 4, method = simulate_gaussian(range = 50, model = "Exp")) # Reusable simulation engine gauss = simulate_gaussian(range = 25) sf3 = sim_covariates(rast_grid, n = 4, method = gauss) # Random simulation sf4 = sim_covariates(rast_grid, n = 4, method = simulate_random()) terra::plot(sf1) terra::plot(sf2) terra::plot(sf3) terra::plot(sf4)rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) # Gaussian simulation sf1 = sim_covariates(rast_grid, n = 4, method = simulate_gaussian(range = 25)) sf2 = sim_covariates(rast_grid, n = 4, method = simulate_gaussian(range = 50, model = "Exp")) # Reusable simulation engine gauss = simulate_gaussian(range = 25) sf3 = sim_covariates(rast_grid, n = 4, method = gauss) # Random simulation sf4 = sim_covariates(rast_grid, n = 4, method = simulate_random()) terra::plot(sf1) terra::plot(sf2) terra::plot(sf3) terra::plot(sf4)
Returns a function that simulates spatially correlated Gaussian random fields using a variogram model.
simulate_gaussian( vgm = NULL, psill = 1, model = "Sph", range = NULL, nugget = 0, beta = 0, nmax = 30, indicators = FALSE, seed = NULL )simulate_gaussian( vgm = NULL, psill = 1, model = "Sph", range = NULL, nugget = 0, beta = 0, nmax = 30, indicators = FALSE, seed = NULL )
vgm |
A variogram model object ( |
psill |
Partial sill (default: 1). Ignored if |
model |
Variogram model type (e.g., |
range |
Spatial range parameter. Required if |
nugget |
Nugget effect (default: 0). Ignored if |
beta |
Mean of the Gaussian field (default: 0). Passed to |
nmax |
The number of nearest observations used for kriging simulation (default: 30).
Passed to |
indicators |
Indicator thresholds (default: |
seed |
Optional random seed for reproducibility. Set this to ensure that the same random field is generated every time the returned function is called. |
A function that accepts rast_grid and n and returns a SpatRaster
rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) # Direct usage sim_fn = simulate_gaussian(range = 25) sf1 = sim_covariates(rast_grid, n = 4, method = sim_fn) # With custom variogram vgm = gstat::vgm(model = "Exp", psill = 1, range = 10) sim_fn2 = simulate_gaussian(vgm = vgm) sf2 = sim_covariates(rast_grid, n = 4, method = sim_fn2) terra::plot(sf1) terra::plot(sf2)rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) # Direct usage sim_fn = simulate_gaussian(range = 25) sf1 = sim_covariates(rast_grid, n = 4, method = sim_fn) # With custom variogram vgm = gstat::vgm(model = "Exp", psill = 1, range = 10) sim_fn2 = simulate_gaussian(vgm = vgm) sf2 = sim_covariates(rast_grid, n = 4, method = sim_fn2) terra::plot(sf1) terra::plot(sf2)
Returns a function that simulates uncorrelated random fields (white noise).
simulate_random(mean = 0, sd = 1, seed = NULL)simulate_random(mean = 0, sd = 1, seed = NULL)
mean |
Mean of the random values (default: 0) |
sd |
Standard deviation of the random values (default: 1) |
seed |
Optional random seed for reproducibility |
A function that accepts rast_grid and n and returns a SpatRaster
rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) sim_fn = simulate_random() sf1 = sim_covariates(rast_grid, n = 4, method = sim_fn) terra::plot(sf1)rast_grid = terra::rast(ncols = 300, nrows = 100, xmin = 0, xmax = 300, ymin = 0, ymax = 100) sim_fn = simulate_random() sf1 = sim_covariates(rast_grid, n = 4, method = sim_fn) terra::plot(sf1)