Learn R Programming

terra (version 0.8-6)

area: Area and perimeter

Description

Compute the area of polygons or for raster cells that are not NA. Computing the surface area of raster cells is particularly relevant for longitude/latitude rasters, as the size of the cells is constant in degrees, but not in meters.

The perimeter method works only on SpatVector objects, and computes the length of lines or the perimeter of polygons.

Usage

# S4 method for SpatRaster
area(x, sum=TRUE, filename="", overwrite=FALSE,  wopt=list(), ...)

# S4 method for SpatVector area(x, ...)

# S4 method for SpatVector perimeter(x)

Arguments

x

SpatRaster or SpatVector

sum

logical. If TRUE the summed area of the cells that are not NA is returned. Otherwise, a SpatRaster with the area for each cell is returned

filename

character. Output filename. Optional

overwrite

logical. If TRUE, filename is overwritten

wopt

list. Options for writing files as in writeRaster

...

additional arguments. None implemented

Value

If the coordinate reference system is longitude/latitude the values returned are in square meter for area and meter for perimeter.

In other cases, the unit is set by coordinate reference system. In most cases it is meter too.

Examples

Run this code
# 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