### 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