This function takes spatial polygon objects (typically species distribution polygons) and converts them into a presence-absence matrix (PAM) using a rasterization approach at a specified resolution. The function is particularly useful for biodiversity and biogeography analyses.
Arguments
- poly
Spatial polygon object (SpatialPolygonsDataFrame, sf, etc.) containing species or taxon distribution data.
- taxon_attribute
Character. The name of the column in `poly` that contains taxon identifiers (species, genera, etc.).
- resolution
Numeric. The resolution for the output raster in coordinate system units (cell size).
- polymask
Optional. A spatial polygon object used to mask and crop the resulting raster. If NULL, no masking is applied.
Value
A PAM (Presence-Absence Matrix) object of class `pam` from the `bamm` package containing:
matrix: Presence-absence matrix (1/0) where rows represent grid cells and columns represent taxarichness: Richness pattern across grid cellssparse: Logical indicating if the matrix is stored in sparse formatcell_coordinates: Coordinates of each grid cell
Details
The function works by:
Creating a base raster with the specified resolution and extent
Splitting polygons by taxon attribute
Rasterizing each taxon's distribution using exact extraction
Stacking individual rasters into a multi-layer raster
Applying optional masking with polymask
Converting the raster stack to a PAM using bamm::models2pam
Note
This function requires the following packages: raster, exactextractr, purrr, and bamm. The input polygons are converted to SpatialPolygonsDataFrame if they aren't already.
Examples
if (FALSE) { # \dontrun{
# Load required libraries
library(raster)
# Example with sample data
uicn <- readRDS(system.file("extdata/uicn.rds",package = "bamm"))
sudam <- readRDS(system.file("extdata/suam.rds",package = "bamm"))
# Convert to PAM with 0.5 degree resolution
pam_result <- bamm::pol2pam(poly = uicn,
taxon_attribute = "binomial",
resolution = 0.5,
polymask = NULL)
# With masking polygon
pam_masked <- pol2pam(poly = uicn,
taxon_attribute = "binomial",
resolution = 0.5,
polymask = sudam)
} # }
