Learn R Programming

flexurba (version 0.2.3)

apply_majority_rule: 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 not NA) 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
)

Value

SpatRaster with clusters of cells with smoothed edges

Arguments

x

SpatRaster. Grid with clusters of cells

version

character. Version of the majority rule algorithm. Supported versions are "R2022A" and "R2023A".

permanent_water

SpatRaster. Grid with permanent water cells (only required when version="R2023A")

land

SpatRaster. Grid with proportion of permanent land (only required when version="R2023A")

pop

SpatRaster. Grid with population (only required when version="R2023A")

Examples

Run this code
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)

Run the code above in your browser using DataLab