obj.nbs: Shared borders of raster objects

View source: R/objectFunctions.R

obj.nbsR Documentation

Shared borders of raster objects

Description

Identify the shared borders of neighboring raster objects.

Usage

obj.nbs(grp.bord, ngbList, only_grp = NULL, silent = FALSE)

Arguments

grp.bord

named list, the list of borders returned by the function obj.border.

ngbList

list, the list of neighborhoods returned by the function ngbList. The list of neighborhoods has to be computed setting the argument rNumb=TRUE.

only_grp

character vector. If NULL, all IDs in grp.bord are considered. If IDs are provided, then they are the only ones considered.

silent

logic, progress bar is not printed on the console.

Value

The function returns a named list. Each element represents a raster object as identified by the list names and contains a nested named list. The names of the nested lists are the IDs of neighboring raster objects and their values identify the raster cells comprising the shared borders.

Note

If there are NA values on the raster surface, raster cells are identified by attribute table row indices (each corresponding to a raster cell). Row indices can be converted into raster cells using the Cell column of the attribute table (e.g. attTbl$Cell[indices]) (see attTbl).

See Also

attTbl(), ngbList(), obj.border()

Examples

# DUMMY DATA
######################################################################################
# LOAD LIBRARIES
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)

# ADD ONE NA VALUE
r[11] <- NA

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

# COMPUTE THE LIST OF NEIGBORHOODS
nbs <- ngbList(r, rNumb=TRUE, attTbl=at) # rnumb MUST be true to use obj.nbs

################################################################################
# COMPUTE RASTER OBJECTS
################################################################################
at$cv <- anchor.seed(at, nbs, silent=TRUE, class = NULL, rNumb=TRUE,
                     cond.filter = "dummy_var > 1",
                     cond.seed   = "dummy_var==max(dummy_var)",
                     cond.growth = "dummy_var<dummy_var[]",
                     lag.growth  = 0)

RO <- split(1:NROW(at), at$cv)

print(RO)

################################################################################
# COMPUTE BORDERS
################################################################################
RO_bd <- obj.border(RO, nbs, silent = TRUE)

################################################################################
# COMPUTE SHARED BORDERS
################################################################################
RO_sbd <- obj.nbs(RO_bd, nbs, silent = TRUE)

# Convert row indices to cell numbers
RO_sbd1 <- RO_sbd[["1"]]
RO_sbd1 <- at$Cell[unlist(RO_sbd1)]

RO_sbd2 <- RO_sbd[["2"]]
RO_sbd2 <- at$Cell[unlist(RO_sbd2)]

# Borders in `RO_sbd` are identified by row indices
print(RO_sbd[["1"]]) # Row indices
print(RO_sbd1) # Cell numbers

print(RO_sbd[["2"]])  # Row indices
print(RO_sbd2) # Cell numbers

# Neighbor objects
names(RO_sbd[["1"]]) # RO1 has one neighbor, RO2
names(RO_sbd[["2"]]) # RO2 has one neighbor, RO1

################################################################################
# PLOT BORDERS
################################################################################
plot(cv.2.rast(r,at$cv), type="classes", col=c("#E6E600","#00A600"),
     main="Shared borders")
points(terra::xyFromCell(r, RO_sbd1), pch=20, col="blue")
points(terra::xyFromCell(r, RO_sbd2), pch=20, col="red")
text(xyFromCell(r, 11), "NA\nvalue")

scapesClassification documentation built on March 18, 2022, 6:32 p.m.