Raster Algebra

library(terra)

# open datasets
fire <- rast("data/AnalysisReady/burn_severity_ba4.tif")
dem <- rast("data/AnalysisReady/elevation.tif")

# We can use mathematical operators on rasters
# + - * /

a <- dem + 100
b <- sqrt(a)
c <- dem * b

# And logical operators
# > >= < <= == !=

# Get cells where severity class is 4
hs <- fire == 4
plot(hs)

# Get cells where severity is greater than 1 and less than 4
lm <- fire > 1 & fire < 4
plot(lm)

# Get cells where elevation is greater than or equal to 2000 meters
e2000 <- dem >= 2000
plot(e2000)

# Get areas that are high severity and greater than or equal to 2000m elevation
hs2000 <- hs + e2000
plot(hs2000)
hs2000 <- hs2000 == 2
plot(hs2000)