View source: R/functions_geographical.R
calculate_cell_area | R Documentation |
Calculate area extent of grid cells
calculate_cell_area(x, grid, crs = sf::st_crs(x), ...)
x |
A numerical two-dimensional object
(a |
grid |
A |
crs |
An object which is a crs or from which one can be derived.
|
... |
Currently, unused and silently ignored. |
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
.
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)"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.