Learn R Programming

SpaDES.tools (version 2.0.6)

rasterizeReduced: Convert reduced representation to full raster

Description

Convert reduced representation to full raster

Usage

rasterizeReduced(
  reduced,
  fullRaster,
  newRasterCols,
  mapcode = names(fullRaster),
  ...
)

Value

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

Arguments

reduced

data.frame or data.table that has at least one column of codes that are represented in the fullRaster.

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)

newRasterCols

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.

mapcode

a character, length 1, with the name of the column in reduced that is represented in fullRaster.

...

Other arguments. None used yet.

Author

Eliot McIntire

See Also

Examples

Run this code
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)

clsDT <- as.data.table(cls)
reducedDT <- reducedDT[clsDT, on = "mapcodeAll==id"]
reducedDT[, mapcodeAll := Bclass]

biomass2 <- rasterizeReduced(reducedDT, fullRas, "biomass")

# clean up
data.table::setDTthreads(origDTThreads)
options(Ncpus = origNcpus)

Run the code above in your browser using DataLab