Learn R Programming

terra (version 1.6-47)

expanse: Get the expanse (area) of individual polygons or for all (summed) raster cells

Description

Compute the area covered by polygons or for all raster cells that are not NA.

This method computes areas for longitude/latitude rasters, as the size of the cells is constant in degrees, but not in square meters. But it can also be important if the coordinate reference system is planar, but not equal-area.

For vector data, the best way to compute area is to use the longitude/latitude CRS. This is contrary to (erroneous) popular belief that suggest that you should use a planar coordinate reference system. This is done automatically, if transform=TRUE.

Usage

# S4 method for SpatRaster
expanse(x, unit="m", transform=TRUE, byValue=FALSE)

# S4 method for SpatVector expanse(x, unit="m", transform=TRUE)

Value

numeric. If x has no values, the total size of all cells. Otherwise, the total area size of all cells that are not NA, expressed in square meters, square kilometers, or hectares.

If byValue=TRUE a matrix is returned with three columns (layer, value, area)

Arguments

x

SpatRaster or SpatVector

unit

character. One of "m", "km", or "ha"

transform

logical. If TRUE, planar CRS are transformed to lon/lat for accuracy

byValue

logical. If TRUE, the area for each unique cell value is returned

See Also

cellSize for a the size of individual cells of a raster, that can be summed with global or with zonal to get the area for different categories.

Examples

Run this code

### SpatRaster 
r <- rast(nrows=18, ncols=36)
v <- 1:ncell(r)
v[200:400] <- NA
values(r) <- v

# summed area in km2
expanse(r, unit="km")

# all cells 
expanse(rast(r), unit="km")

r <- rast(ncols=90, nrows=45, ymin=-80, ymax=80)
m <- project(r, "+proj=merc")

expanse(m, unit="km")
expanse(m, unit="km", transform=FALSE)

m2 <- c(m, m)
values(m2) <- cbind(c(1,2,NA,NA), c(11:14))
expanse(m2, unit="km", byValue=TRUE)



### SpatVector
v <- vect(system.file("ex/lux.shp", package="terra"))

a <- expanse(v)
a
sum(a)

Run the code above in your browser using DataLab