R/layer_to_matrix.R

Defines functions layer_to_matrix

Documented in layer_to_matrix

#' Get \code{stars} layer values as matrix
#'
#' Extracts the values of a single layer in a \code{stars} object to a \code{matrix}.
#'
#' @param	x	A \code{stars} raster with one attribute and two dimensions, \code{x} and \code{y}, i.e., a single-band raster.
#' @param check Whether to check (and fix if necessary) that input has one attribute, one layer and x-y as dimensions 1-2 (default is \code{TRUE}).
#' @return A \code{matrix} with the layer values, having the same orientation as the raster (i.e., rows represent the y-axis and columns represent the x-axis).
#'
#' @examples
#' data(dem)
#' m = layer_to_matrix(dem)
#' m
#'
#' @export

layer_to_matrix = function(x, check = TRUE) {

  # Checks
  if(check) {
    x = check_one_attribute(x)
    x = check_2d(x)
  }

  # To matrix
  m = t(x[[1]])

  # Return
  return(m)

}

Try the starsExtra package in your browser

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

starsExtra documentation built on Nov. 18, 2021, 5:08 p.m.