View source: R/objectFunctions.R
obj.border | R Documentation |
Identify the borders of raster objects.
obj.border(group, ngbList, silent = FALSE)
group |
named list, each element represents a raster object composed of
several raster cells. If there are NA values on the raster surface, raster
cells must be identified by attribute table row indices (each corresponding
to a raster cell) (see |
ngbList |
list, the list of neighborhoods returned by the function
|
silent |
logic, progress bar is not printed on the console. |
The function returns a named list of object borders. List names identify the objects; list element values identify the raster cells comprising the borders.
Note that group
has to be a named list whose
names correspond to raster object IDs.
If there are NA values on the raster surface, raster cells must be
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
).
attTbl()
, ngbList()
, obj.nbs()
# 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 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.border ################################################################################ # 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) # Raster objects RO <- split(1:NROW(at), at$cv) print(RO) # values are attribute table row indices ################################################################################ # COMPUTE BORDERS ################################################################################ RO_bd <- obj.border(RO, nbs, silent = TRUE) RO_bd1 <- at$Cell[RO_bd[["1"]]] # Convert row numbers to cell numbers RO_bd2 <- at$Cell[RO_bd[["2"]]] # Convert row numbers to cell numbers print(RO_bd) # attribute table row indices print(RO_bd1) # cell numbers print(RO_bd2) # cell numbers ################################################################################ # PLOT BORDERS ################################################################################ plot(cv.2.rast(r,at$cv), type="classes", col=c("#E6E600","#00A600"), main="Borders") points(terra::xyFromCell(r, RO_bd1), pch=20, col="blue") points(terra::xyFromCell(r, RO_bd2), pch=20, col="red") text(xyFromCell(r, 11), "NA\nvalue")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.