R/compact.R

Defines functions a5_uncompact a5_compact

Documented in a5_compact a5_uncompact

#' Compact a set of A5 cells
#'
#' Merges complete sibling groups into their common parent, reducing the
#' number of cells while preserving coverage.
#'
#' @param cells An [a5_cell] vector.
#' @returns An [a5_cell] vector of compacted cells.
#'
#' @seealso [a5_uncompact()]
#' @export
#' @examples
#' cell <- a5_lonlat_to_cell(-3.19, 55.95, resolution = 5)
#' children <- a5_cell_to_children(cell)
#' a5_compact(children) # back to the parent
a5_compact <- function(cells) {
  cells <- as_a5_cell(cells)
  cells_from_rs(a5_compact_rs(cell_data(cells)))
}

#' Uncompact a set of A5 cells to a target resolution
#'
#' Expands each cell to its descendants at the target resolution.
#'
#' @param cells An [a5_cell] vector.
#' @param resolution Integer scalar target resolution (0--30).
#' @returns An [a5_cell] vector of uncompacted cells.
#'
#' @seealso [a5_compact()]
#' @export
#' @examples
#' cell <- a5_lonlat_to_cell(-3.19, 55.95, resolution = 5)
#' a5_uncompact(cell, resolution = 7)
a5_uncompact <- function(cells, resolution) {
  cells <- as_a5_cell(cells)
  resolution <- vctrs::vec_cast(resolution, integer())
  check_resolution(resolution)
  vctrs::vec_assert(resolution, size = 1L)
  cells_from_rs(a5_uncompact_rs(cell_data(cells), resolution))
}

Try the a5R package in your browser

Any scripts or data that you put into this service are public.

a5R documentation built on March 26, 2026, 5:10 p.m.