You can do logical operations on GRaster
s. A cell with a value of 1 is interpreted as TRUE
, and a value of 0 is interpreted as FALSE
. You can compare:
A
GRaster
to anotherGRaster
A
GRaster
to a logical value (TRUE
orFALSE
, but notNA
–seenot.na()
)A
GRaster
to a numeric or integer value that is 0 or 1
Operators include:
|
:TRUE
if either condition isTRUE
(or 1), but returnsNA
if either condition isNA
.&
:TRUE
if both conditions areTRUE
(or 1), butNA
if either isNA
.
Usage
# S4 method for class 'GRaster,GRaster'
Logic(e1, e2)
# S4 method for class 'logical,GRaster'
Logic(e1, e2)
# S4 method for class 'GRaster,logical'
Logic(e1, e2)
# S4 method for class 'GRaster,numeric'
Logic(e1, e2)
# S4 method for class 'numeric,GRaster'
Logic(e1, e2)
# S4 method for class 'GRaster,integer'
Logic(e1, e2)
# S4 method for class 'integer,GRaster'
Logic(e1, e2)
Examples
if (grassStarted()) {
# Setup
library(terra)
# Example data
madElev <- fastData("madElev")
# Convert a SpatRaster to a GRaster
elev <- fast(madElev)
elevs <- c(elev, elev, log10(elev) - 1, sqrt(elev))
names(elevs) <- c("elev1", "elev2", "log_elev", "sqrt_elev")
elev
elevs
# Comparisons
elev < 100
elev <= 100
elev == 100
elev != 100
elev > 100
elev >= 100
elev + 100 < 2 * elev
elevs > 10
10 > elevs
# logic
elev < 10 | elev > 200
elev < 10 | cos(elev) > 0.9
elev < 10 | TRUE
TRUE | elev > 200
elev < 10 | FALSE
FALSE | elev > 200
elev < 10 & cos(elev) > 0.9
elev < 10 & TRUE
TRUE & elev > 200
elev < 10 & FALSE
FALSE & elev > 200
}