Skip to contents

Masks all NA cells across all layers, such that returned layers have matching NA cells.

Usage

mask_all(rasts, filename = NULL, overwrite = FALSE)

Arguments

rasts

SpatRaster with nlyr(rasts) > 1 to mask

filename

characterto save output

overwrite

logical overwrite existing filename?

Value

SpatRaster

Details

Uses a ton of RAM and will break for larger rasters.

Examples


# Create some SpatRaster layers with non-matching NA cells
library(terra)
library(sdmtools)

r <- c(
  example_raster(seed = 1),
  example_raster(seed = 2),
  example_raster(seed = 3)
)
rvals <- terra::values(r)
nas <- c(1:10, 105:120, 215:240)
rvals[nas] <- NA
r[] <- rvals

# check if it pleases you to do so
# plot(r)
# mask out non-overlapping `NA` values in all layers
s <- mask_all(r)
s
#> class       : SpatRaster 
#> dimensions  : 10, 10, 3  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 10, 0, 10  (xmin, xmax, ymin, ymax)
#> coord. ref. :  
#> source(s)   : memory
#> names       :   example,    example,   example 
#> min values  : 0.1644813, 0.08614646, 0.1037884 
#> max values  : 8.7811877, 7.32227034, 3.9045514 

# plot(s)