require(terra)
p <- system.file("exdat", package = "rassta")
# Multi-layer SpatRaster of topographic variables
## 3 continuous variables
ftva <- list.files(path = p, pattern = "^height|^slope|^wetness",
full.names = TRUE
)
tva <- terra::rast(ftva)
# Single-layer SpatRaster of topographic classification units
## Five classification units
ftcu <- list.files(path = p, pattern = "topography.tif", full.names = TRUE)
tcu <- terra::rast(ftcu)
# Add the classification units to the SpatRaster of topographic variables
tcuvars <- c(tcu, tva)
# Data frame with source for "cu", "vars", and "dif"
ftdif <- list.files(path = p, pattern = "topodif.csv", full.names = TRUE)
tdif <- read.csv(ftdif)
# Check structure of source data frame
head(tdif)
# Predict distribution functions
## 1 distribution function per variable and classification unit = 1
tpdif <- predict_functions(cuvar.rast = tcuvars,
cu.ind = 1,
cu = tdif$Class.Unit[1:3],
vars = tdif$Variable[1:3],
dif = tdif$Dist.Func[1:3],
grid.mult = 3,
span = 0.9
)
# Plot predicted distribution functions
if(interactive()){plot(tpdif, col = hcl.colors(100, "Oslo", rev = TRUE))}
#--------
# Writing results to disk and parallel processing
if(interactive()){
# Directory for temporary files
o <- tempdir()
# Register parallel backend
require(doParallel)
registerDoParallel(4)
# Predict distribution functions
tpdif <- predict_functions(cuvar.rast = tcuvars,
cu.ind = 1,
cu = tdif$Class.Unit[1:3],
vars = tdif$Variable[1:3],
dif = tdif$Dist.Func[1:3],
grid.mult = 3, span = 0.9,
to.disk = TRUE,
outdir = o
)
# Stop cluster
stopImplicitCluster()
# Clean temporary files
file.remove(sources(tpdif))
}
Run the code above in your browser using DataLab