Convert reduced representation to full raster
rasterizeReduced(
reduced,
fullRaster,
newRasterCols,
mapcode = names(fullRaster),
...
)data.frame or data.table that has at least one
column of codes that are represented in the fullRaster.
RasterLayer/SpatRaster of codes used in reduced that
represents a spatial representation of the data. Note that
if fullRaster is a factor SpatRaster, the active category
level values are used, not the IDs (see terra::activeCat and
terra::cats)
Character vector, length 1 or more, with the name(s) of
the column(s) in reduced whose value will be
returned as a RasterLayer/SpatRaster or list
of RasterLayer/SpatRasters.
a character, length 1, with the name of the column in reduced
that is represented in fullRaster.
Other arguments. None used yet.
A RasterLayer/SpatRaster or list of
RasterLayer/SpatRaster of with same dimensions as fullRaster representing
newRasterCols spatially, according to the join between the mapcode
contained within reduced and fullRaster
library(data.table)
library(terra)
origDTThreads <- data.table::setDTthreads(2L)
origNcpus <- options(Ncpus = 2L)
ras <- rast(ext(0, 15, 0, 15), res = 1)
fullRas <- randomPolygons(ras, numTypes = 2)
names(fullRas) <- "mapcodeAll"
uniqueComms <- unique(fullRas)
reducedDT <- data.table(uniqueComms,
communities = sample(1:1000, length(uniqueComms)),
biomass = rnbinom(length(uniqueComms), mu = 4000, 0.4))
biomass <- rasterizeReduced(reducedDT, fullRas, "biomass")
# The default key is the layer name of the fullRas, so rekey incase of miskey
setkey(reducedDT, biomass)
communities <- rasterizeReduced(reducedDT, fullRas, "communities")
coltab(communities) <- c("blue", "orange", "red")
if (interactive()) {
terra::plot(c(biomass, communities, fullRas))
}
## with a factor SpatRaster, the mapcode should correspond to the
## active category (not the ids)
cls <- data.frame(id = sort(unique(as.vector(fullRas[]))))
cls$Bclass <- LETTERS[cls$id]
levels(fullRas) <- cls
is.factor(fullRas)
#> [1] TRUE
clsDT <- as.data.table(cls)
reducedDT <- reducedDT[clsDT, on = "mapcodeAll==id"]
reducedDT[, mapcodeAll := Bclass]
#> mapcodeAll communities biomass Bclass
#> <char> <int> <num> <char>
#> 1: A 860 148 A
#> 2: B 860 148 B
biomass2 <- rasterizeReduced(reducedDT, fullRas, "biomass")
# clean up
data.table::setDTthreads(origDTThreads)
options(Ncpus = origNcpus)