calculate_cell_area: Calculate area extent of grid cells

View source: R/functions_geographical.R

calculate_cell_areaR Documentation

Calculate area extent of grid cells

Description

Calculate area extent of grid cells

Usage

calculate_cell_area(x, grid, crs = sf::st_crs(x), ...)

Arguments

x

A numerical two-dimensional object (a matrix, array, or data.frame) with longitude/X, latitude/Y as columns; a sp::SpatialPoints object; or a terra::SpatVector object; or a sf object with a point geometry, i.e., an object with a class sf or sfc.

grid

A terra::rast, a raster::Raster or a stars::stars object for coordinate system and cells.

crs

An object which is a crs or from which one can be derived. x can be numeric as a EPSG number; a character string as a wkt; a character string as a proj4 (not recommended because outdated); or of a class including raster::Raster, sp::Spatial, sp::CRS, or a sf or sfc class.

...

Currently, unused and silently ignored.

Value

A data.frame with four columns: “Longitude”, “Latitude”, “km2”, and “rel”; and one row for each site of sites. Cell area on ellipsoid, based on grid, in square-kilometer km2 and as fraction of maximal cell area on the equator rel.

Examples

r <- terra::rast(
  xmin = 0, xmax = 1,
  ymin = -90, ymax = 90,
  crs = "OGC:CRS84",
  resolution = c(1, 1),
  vals = 1:180
)
xy <- terra::spatSample(r, size = 20L, method = "random", as.points = TRUE)

## Calculate area for a subset of cells in grid
cell_areas <- calculate_cell_area(xy, grid = r)
cell_areas <- calculate_cell_area(xy, grid = stars::st_as_stars(r))

## Calculate area for all cells in grid
cell_areas2 <- calculate_cell_area(grid = r)


## Visualize cell area by latitude
with(cell_areas2, graphics::plot(y, km2, type = "l"))

## Comparison with a spherical Earth
# A spherical Earth underestimates cell areas
# at mid latitudes compared to cell areas on a WGS84 ellipsoid as here
rel_spherical <- cos(cell_areas2[, "y"] * pi / 180)
graphics::plot(
  abs(cell_areas2[, "y"]),
  max(cell_areas2[, "km2"]) * (cell_areas2[, "rel"] - rel_spherical),
  pch = 46,
  xlab = "abs(Latitude)",
  ylab = "Cell area difference\n(ellipsoid - sphere; km2)"
)


DrylandEcology/rSW2st documentation built on Jan. 10, 2024, 6:22 p.m.