#' PD (class level)
#'
#' @description Patch density (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{PD = \frac{n_{i}} {A} * 10000 * 100}
#' where \eqn{n_{i}} is the number of patches and \eqn{A} is the total landscape
#' area in square meters.
#'
#' PD is an 'Aggregation metric'. It describes the fragmentation of a class, however, does not
#' necessarily contain information about the configuration or composition of the class. In
#' contrast to \code{\link{lsm_c_np}} it is standardized to the area and comparisons among
#' landscapes with different total area are possible.
#'
#' Because the metric is based on distances or areas please make sure your data
#' is valid using \code{\link{check_landscape}}.
#'
#' \subsection{Units}{Number per 100 hectares}
#' \subsection{Ranges}{0 < PD <= 1e+06}
#' \subsection{Behaviour}{Increases as the landscape gets more patchy. Reaches its maximum
#' if every cell is a different patch.}
#'
#' @seealso
#' \code{\link{lsm_c_np}},
#' \code{\link{lsm_l_ta}}, \cr
#' \code{\link{lsm_l_pd}}
#'
#' @return tibble
#'
#' @examples
#' landscape <- terra::rast(landscapemetrics::landscape)
#' lsm_c_pd(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_c_pd <- function(landscape, directions = 8) {
landscape <- landscape_as_list(landscape)
result <- lapply(X = landscape,
FUN = lsm_c_pd_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_c_pd_calc <- function(landscape, directions, resolution, extras = NULL) {
if (missing(resolution)) resolution <- terra::res(landscape)
if (is.null(extras)){
metrics <- "lsm_c_pd"
landscape <- terra::as.matrix(landscape, wide = TRUE)
extras <- prepare_extras(metrics, landscape_mat = landscape,
directions = directions, resolution = resolution)
}
# all cells are NA
if (all(is.na(landscape))) {
return(tibble::new_tibble(list(level = "class",
class = as.integer(NA),
id = as.integer(NA),
metric = "pd",
value = as.double(NA))))
}
# get patch area
area_patch <- lsm_p_area_calc(landscape,
directions = directions,
resolution = resolution,
extras = extras)
# summarise to total area
area_patch <- sum(area_patch$value)
# get number of patches
np_class <- lsm_c_np_calc(landscape, directions = directions, extras = extras)
# calculate relative patch density
np_class$value <- (np_class$value / area_patch) * 100
return(tibble::new_tibble(list(level = rep("class", nrow(np_class)),
class = as.integer(np_class$class),
id = rep(as.integer(NA), nrow(np_class)),
metric = rep("pd", nrow(np_class)),
value = as.double(np_class$value))))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.