Skip to contents

Masks a SpatRaster to Alberta or to any user-supplied polygon. The character shortcut "alberta" refers to the provincial boundary packaged with sciSpatialR; any sf or SpatVector polygon may also be passed directly.

Usage

mask_to_boundary(x, boundary = "alberta", inverse = FALSE, ...)

Arguments

x

A SpatRaster to mask.

boundary

A SpatVector, an sf polygon, or the character shortcut "alberta". Defaults to "alberta", the packaged provincial boundary also returned by ab_boundary().

inverse

Logical; if TRUE, cells outside the boundary are retained and cells inside are set to NA. Default FALSE.

...

Additional arguments passed to terra::mask().

Value

A SpatRaster with cells outside (or inside, if inverse = TRUE) the boundary set to NA.

Examples

if (FALSE) { # \dontrun{
library(terra)
r   <- rast(nrows = 100, ncols = 100,
            xmin = -120, xmax = -110,
            ymin =   49, ymax =   60,
            crs = "EPSG:4326")
r[] <- runif(ncell(r))
# Mask to the default Alberta provincial boundary
r_ab <- mask_to_boundary(r)

# Mask to a user polygon
poly <- vect("my_polygon.gpkg")
r_masked <- mask_to_boundary(r, poly)

# Anything on the share works the same way
nsr <- get_layer("natural_regions_subregions_of_alberta")
r_nsr <- mask_to_boundary(r, nsr)
} # }