R/get_slope.R

Defines functions get_slope

Documented in get_slope

#' Calculate a slope
#'
#' @param x A matrix
#' @param neighbourhood The number of directions in which cell adjacencies are
#'   considered as neighbours: 4 (rook's case), 8 (queen's case) or a binary matrix
#'   where the ones define the neighbourhood. The default is 4.
#'
#' @details "Slope" is calculated as follows:
#'   1. For each cell, the algorithm looks at its 4 neighbors and
#'   calculates the absolute difference between the main cell and its neighbors.
#'   2. Next, it sums these four values.
#'   3. After repeating this operation for every cell, it calculates
#'   an average of the sum of the absolute differences for the whole raster.
#'
#' @return A slope value
get_slope = function(x, neighbourhood = matrix(4)){
  mean(cslope(x, directions = neighbourhood))
}

Try the bespatial package in your browser

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

bespatial documentation built on April 3, 2025, 7:58 p.m.