apply_majority_rule {flexurba} | R Documentation |
Apply the majority rule algorithm
Description
The functions applies the majority rule to smooth edges of clusters of cells. The function supports two different version of the majority rule algorithm: the version of GHSL Data Package 2022 and GHSL Data Package 2023:
-
version="R2022A"
: If a cell has at least five of the eight surrounding cells belonging to a unique cluster of cells, then the cell is added to that cluster. The process is iteratively repeated until no more cells are added. -
version="R2023A"
: A cell is added to a cluster if the majority of the surrounding cells belongs to one unique cluster, with majority only computed among populated (pop > 0
) or land cells (land > 0.5
). Cells with permanent water (permanent_water
notNA
) can never be added to a cluster of cells. The process is iteratively repeated until no more cells are added.
Usage
apply_majority_rule(
x,
version = "R2022A",
permanent_water = NULL,
land = NULL,
pop = NULL
)
Arguments
x |
SpatRaster. Grid with clusters of cells |
version |
character. Version of the majority rule algorithm. Supported versions are |
permanent_water |
SpatRaster. Grid with permanent water cells (only required when |
land |
SpatRaster. Grid with proportion of permanent land (only required when |
pop |
SpatRaster. Grid with population (only required when |
Value
SpatRaster with clusters of cells with smoothed edges
Examples
nr <- nc <- 8
r <- terra::rast(nrows = nr, ncols = nc, ext = c(0, nc, 0, nr), vals = c(
NA, NA, 1, 1, 1, NA, NA, NA,
NA, NA, NA, 1, 1, NA, NA, NA,
NA, NA, 2, NA, NA, NA, NA, NA,
NA, NA, 2, NA, NA, 2, NA, NA,
NA, NA, 2, NA, 2, 2, NA, NA,
2, 2, 2, 2, 2, 2, NA, NA,
NA, NA, 2, 2, NA, NA, NA, NA,
NA, NA, NA, 2, NA, NA, NA, NA
))
terra::plot(r)
smoothed <- apply_majority_rule(r)
terra::plot(smoothed)