area.calc {divraster} | R Documentation |
Calculate SpatRaster Layer Areas and Overlap Areas
Description
Calculates the total area for each layer (e.g., species) within a SpatRaster
object.
Optionally, it can also compute the overlapping areas between the primary SpatRaster
(x
)
and one or two additional single-layer SpatRaster
objects (y
and z
).
Results are returned as a data.frame
and can optionally be saved to a CSV file.
Usage
area.calc(x, y = NULL, z = NULL, filename = "", unit = "km", cellsize = NULL)
Arguments
x |
A |
y |
An optional |
z |
An optional |
filename |
Character string. If provided (e.g., "results.csv"), the resulting data frame will be saved to a CSV file with this name. If not provided, results are returned only to the R session. |
unit |
Character string specifying the unit of measurement for area calculations. Defaults to "km" (kilometers). Other options include "ha" (hectares), "m" (meters), etc. |
cellsize |
Numeric. An optional value specifying the cell size (area of a single cell)
to be used for calculations. If |
Value
A data.frame
with the following columns:
-
Layer: Name of each layer from the input
SpatRaster x
. -
Area: The calculated area for each layer in
x
(e.g., total species range area). -
Overlap_Area_Y (optional): If
y
is provided, the area where thex
layer andy
raster both have a value of 1 (overlap). -
Overlap_Area_Z (optional): If
z
is provided, the area where thex
layer andz
raster both have a value of 1 (overlap). -
Overlap_Area_All (optional): If both
y
andz
are provided, the area where thex
layer,y
raster, andz
raster all have a value of 1 (triple overlap).
Areas are reported in the specified unit
.
Examples
library(terra)
# Load example rasters for demonstration
# Ensure these files are present in your package's inst/extdata folder
bin_rast <- terra::rast(system.file("extdata", "ref.tif", package = "divraster"))
# Example 1: Calculate area for 'bin_rast' only
area_only <- area.calc(bin_rast)
area_only