Evaluate conditions for unclassified cells and classify them if conditions are true.

cond.4.all(attTbl, cond, classVector = NULL, class, ovw_class = FALSE)

Arguments

attTbl

data.frame, the attribute table returned by the function attTbl.

cond

character string, the conditions a cell have to meet to be classified as indicated by the argument class. If there is a classVector input, the classification number is only assigned to unclassified cells unless the argument ovw_class = TRUE. See conditions for more details.

classVector

numeric vector, if provided, it defines the cells in the attribute table that have already been classified. See conditions for more information about class vectors.

class

numeric, the classification number to assign to all cells that meet the function conditions.

ovw_class

logic, if there is a classVector input, reclassify cells that were already classified and that meet the function conditions.

Value

Update classVector with the new cells that were classified by the function. If there is no classVector input, the function returns a new class vector. See conditions for more details about class vectors.

Details

  • The function evaluates the conditions of the argument conditions for all unclassified cells (i.e., classVector NA-cells).

  • Cells that meet the function conditions are classified as indicted by the argument class.

  • Absolute test cell conditions can be used (see conditions).

Examples

# DUMMY DATA
################################################################################
library(scapesClassification)
library(terra)

# LOAD THE DUMMY RASTER
r <- list.files(system.file("extdata", package = "scapesClassification"),
                pattern = "dummy_raster\\.tif", full.names = TRUE)
r <- terra::rast(r)

# COMPUTE THE ATTRIBUTE TABLE
at <- attTbl(r, "dummy_var")

# COMPUTE THE LIST OF NEIGBORHOODS
nbs <- ngbList(r)

################################################################################
# COND.4.ALL
################################################################################
# compute new class vector
# conditions: "dummy_var == 1"
cv1 <- cond.4.all(attTbl = at, cond = "dummy_var <= 1", class = 1)

unique(cv1) # one class (class 1)
#> [1]  1 NA

# update class vector `cv1`
# conditions: "dummy_var <= 3"
cv2   <- cond.4.all(attTbl = at, cond = "dummy_var <= 3", class = 2,
                    classVector = cv1) # input previous class vector

unique(cv2) # two classes (class 1 and class 2)
#> [1]  1  2 NA

# convert class vector 2 raster
r_cv1 <- cv.2.rast(r, at$Cell, classVector = cv1)
r_cv2 <- cv.2.rast(r, at$Cell, classVector = cv2)

################################################################################
# PLOTS
################################################################################
oldpar <- par(mfrow = c(1,2))
m <- c(4.5, 0.5, 2, 3.2)

# 1.
r_cv1[which(is.na(values(r_cv1)))] <- 10
plot(r_cv1, type="classes", mar=m, col=c("#78b2c4","#818792"), axes=FALSE,
     plg=list(x=1, y=1, cex=.80, title="Classes",legend=c("1", "NA")))
text(r); lines(r)
mtext(side=3, line=1, adj=0, cex=1, font=2, "1. COND.4.ALL")
mtext(side=3, line=0, adj=0, cex=0.9, "New class vector")
mtext(side=1, line=0, cex=0.9, adj=0, "Rule: 'dummy_var <= 1'")
mtext(side=1, line=1, cex=0.9, adj=0, "Class: 1")

# 2.
r_cv2[which(is.na(values(r_cv2)))] <- 10
plot(r_cv2, type="classes", mar=m, col=c("#78b2c4","#cfad89","#818792"), axes=FALSE,
     plg=list(x=1, y=1, cex=.80, title="Classes",legend=c("1", "2", "NA")))
text(r); lines(r)
mtext(side=3, line=1, adj=0, cex=1, font=2, "2. COND.4.ALL")
mtext(side=3, line=0, adj=0, cex=0.9, "Update class vector (class 1 not overwritten)")
mtext(side=1, line=0, cex=0.9, adj=0, "Rule: 'dummy_var <= 3'")
mtext(side=1, line=1, cex=0.9, adj=0, "Class: 2")

par(oldpar)