R/lsm_l_np.R

Defines functions lsm_l_np_calc lsm_l_np

Documented in lsm_l_np

#' NP (landscape level)
#'
#' @description Number of patches (Aggregation metric)
#'
#' @param landscape A categorical raster object: SpatRaster; Raster* Layer, Stack, Brick; stars or a list of SpatRasters.
#' @param directions The number of directions in which patches should be
#' connected: 4 (rook's case) or 8 (queen's case).
#'
#' @details
#' \deqn{NP = N}
#' where \eqn{N} is the number of patches.
#'
#' NP is an 'Aggregation metric'. It describes the fragmentation of the landscape,
#' however, does not necessarily contain information about the configuration or
#' composition of the landscape.
#'
#' \subsection{Units}{None}
#' \subsection{Ranges}{NP >= 1}
#' \subsection{Behaviour}{Equals NP = 1 when only one patch is present and
#' increases, without limit, as the number of patches increases}
#'
#' @seealso
#' \code{\link{lsm_c_np}}
#'
#' @return tibble
#'
#' @examples
#' landscape <- terra::rast(landscapemetrics::landscape)
#' lsm_l_np(landscape)
#'
#' @references
#' McGarigal K., SA Cushman, and E Ene. 2023. FRAGSTATS v4: Spatial Pattern Analysis
#' Program for Categorical Maps. Computer software program produced by the authors;
#' available at the following web site: https://www.fragstats.org
#'
#' @export
lsm_l_np <- function(landscape, directions = 8) {

    landscape <- landscape_as_list(landscape)

    result <- lapply(X = landscape,
                     FUN = lsm_l_np_calc,
                     directions = directions)

    layer <- rep(seq_along(result),
                 vapply(result, nrow, FUN.VALUE = integer(1)))

    result <- do.call(rbind, result)

    tibble::add_column(result, layer, .before = TRUE)
}

lsm_l_np_calc <- function(landscape, directions, extras = NULL) {

    n_patches <- lsm_c_np_calc(landscape,
                               directions = directions,
                               extras = extras)

    n_patches <- sum(n_patches$value)

    # all values NA
    if (is.na(n_patches)) {
        return(tibble::new_tibble(list(level = "landscape",
                              class = as.integer(NA),
                              id = as.integer(NA),
                              metric = "np",
                              value = as.double(NA))))
    }

    return(tibble::new_tibble(list(level = rep("landscape", length(n_patches)),
                          class = rep(as.integer(NA), length(n_patches)),
                          id = rep(as.integer(NA), length(n_patches)),
                          metric = rep("np", length(n_patches)),
                          value = as.double(n_patches))))
}
r-spatialecology/landscapemetrics documentation built on April 3, 2024, 2:21 a.m.