# NOT RUN {
### SpatRaster
r <- rast(nrow=18, ncol=36)
v <- 1:ncell(r)
v[200:400] <- NA
values(r) <- v
# summed area in km2
area(r) / 1000000
# area for each raster cell
a <- area(r, sum=FALSE)
# you can use mask to remove the cells in r that are NA
am <- mask(a, r)
# and the sum of the cells to get the same result
global(am, "sum", na.rm=TRUE) / 1000000
### SpatVector
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
a <- area(v)
a
sum(a)
perimeter(v)
# }
Run the code above in your browser using DataLab