Learn R Programming

reproducible (version 1.2.11)

fastMask: Faster operations on rasters (DEPRECATED as terra::mask is fast)

Description

This alternative to raster::mask is included here.

Usage

fastMask(
  x,
  y,
  cores = NULL,
  useGDAL = getOption("reproducible.useGDAL", TRUE),
  verbose = getOption("reproducible.verbose", 1),
  ...,
  skipDeprecastedMsg = FALSE
)

Value

A Raster* object, masked (i.e., smaller extent and/or several pixels converted to NA)

Arguments

x

A Raster* object.

y

A SpatialPolygons object. If it is not in the same projection as x, it will be reprojected on the fly to that of x

cores

An integer* or 'AUTO'. This will be used if gdalwarp is triggered. 'AUTO' will calculate 90 number of cores in the system, while an integer or rounded float will be passed as the exact number of cores to be used.

useGDAL

Logical or "force". Defaults to getOption("reproducible.useGDAL" = TRUE). If TRUE, then this function will use gdalwarp only when not small enough to fit in memory (i.e., if the operation fails the raster::canProcessInMemory(x, 3) test). Using gdalwarp will usually be faster than raster::projectRaster, the function used if this is FALSE. Since since the two options use different algorithms, there may be different projection results. "force" will cause it to use GDAL regardless of the memory test described here.

verbose

Numeric, -1 silent (where possible), 0 being very quiet, 1 showing more messaging, 2 being more messaging, etc. Default is 1. Above 3 will output much more information about the internals of Caching, which may help diagnose Caching challenges. Can set globally with an option, e.g., options('reproducible.verbose' = 0) to reduce to minimal

...

Currently unused.

skipDeprecastedMsg

Logical. If TRUE, then the message about this function being deprecated will be suppressed.

Author

Eliot McIntire

Examples

Run this code

# This function is mostly superfluous as terra::mask is fast
library(sp)
library(raster)

Sr1 <- Polygon(cbind(c(2, 4, 4, 0.9, 2), c(2, 3, 5, 4, 2)))
Sr2 <- Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
Sr3 <- Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5)))

Srs1 <- Polygons(list(Sr1), "s1")
Srs2 <- Polygons(list(Sr2), "s2")
Srs3 <- Polygons(list(Sr3), "s3")
shp <- SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3)
d <- data.frame(vals = 1:3, other = letters[3:1], stringsAsFactors = FALSE)
row.names(d) <- names(shp)
shp <- SpatialPolygonsDataFrame(shp, data = d)
poly <- list()
poly[[1]] <- raster(raster::extent(shp), vals = 0, res = c(1, 1))
poly[[2]] <- raster(raster::extent(shp), vals = 1, res = c(1, 1))
origStack <- stack(poly)

# during transition from raster to terra, the following requires terra to run
if (requireNamespace("terra", silent = TRUE)) {
  newStack1 <- mask(x= terra::rast(origStack), mask = terra::vect(sf::st_as_sf(shp)))
  newStack2 <- fastMask(x = origStack, y = sf::st_as_sf(shp))

# test all equal
  all.equal(newStack1, newStack2)

  newStack1 <- stack(newStack1)
  newStack2 <- stack(newStack2)

 if (interactive()) {
  plot(newStack2[[1]])
  plot(shp, add = TRUE)
 }

}

Run the code above in your browser using DataLab