# NOT RUN {
### SpatRaster
r <- rast(nrow=18, ncol=36)
v <- 1:ncell(r)
v[200:400] <- NA
values(r) <- v
# area for each raster cell
a <- area(r, sum=FALSE)
# summed area in km2
area(r) / 1000000
## you can use mask to remove the cells in r that are NA
## and compute the global sum to get the same result
#am <- mask(a, r)
#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