mask_rast: Mask a raster based on a vector

Description Usage Arguments Value Author(s) Examples

View source: R/mask_rast.R

Description

Masks a raster file or object on the basis of a vector file or object. Pixels not covered by the vector features are set to NoData. If the input raster is multi-band, the mask is automatically applied to all bands. An optional buffer can be applied to the input vector to allow a more "lenient" masking, or to remove also the borders of the vector.

Usage

1
2
3
4
mask_rast(in_rast, mask, crop = FALSE, buffer = NULL, out_nodata = NULL,
  out_type = "rastobj", out_basename = NULL, save_multiband = FALSE,
  out_dtype = NULL, overwrite = FALSE, verbose = TRUE,
  verb_foreach = FALSE)

Arguments

in_rast

Raster file or object inheriting class raster to be masked

mask

Vector file or object of class *sf or sp to be used as a mask

crop

logical if TRUE, in_rast is also cropped on the extent of mask, Default: FALSE

buffer

numeric if not NULL, width of a buffer to be applied to mask before masking in_rast. If negative, mask is "reduced" prior to masking (see examples), Default: NULL

out_nodata

numeric value to be assigned to areas outside the mask, Default: 'NoData'

out_type

character

  • if == "rastobj", return a Raster object;

  • if == "filename", return the filename of the masked layer (GTiff or gdal vrt format depending on other arguments - see below)

Default: "rastobj" (If an invalid string is provided, defaults to rastobj)

out_basename

character filename where the masked raster should be saved (ignored if to_file == FALSE). If NULL, the masked raster is saved on a temporaty file in the R temporary folder, named <basename(in_rast)>_masked.tif:. The file is saved in TIFF format, with DEFLATE compression. For multiband inputs, a separate file is saved for each band (e.g., out_basename_001. tif, out_basename_002. tif...), unless the save_multiband argument is set to "TRUE"

save_multiband

logical

  • if TRUE, and in_rast is multi-band, the single-band masked layers are saved as a single multiband tiff at the end of processing, and the single-band files are deleted;

  • if FALSE, and in_rast is multi-band, a gdal vrt file pointing to all masked bands is created instead.

Default: FALSE

out_dtype

character data type of the output masked files, according to gdal specifications for Gtiff files ("Byte", "UInt16", "Int16", "UInt32", "Int32", "Float32", "Float64", "CInt16", "CInt32", "CFloat32" and "CFloat64"). If NULL, the data type is retrieved from the input, Default: NULL

overwrite

logical if TRUE, and out_basename is set and existing, existing files are overwritten, Default: FALSE

verbose

logical if TRUE, extended processing information is sent to the console in the form of messages

verb_foreach

logical allow verbose output from foreach for debugging purposes, Default: FALSE

Value

object of class raster (if out_type == rastobj), or character string corresponding to the filename of the created raster (if out_type == filename )

Author(s)

Lorenzo Busetto, PhD (2017) email: lbusett@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## Not run: 
library(sprawl)
library(sprawl.data)
library(raster)
in_polys <- read_vect(system.file("extdata","lc_polys.shp", package = "sprawl.data"),
                       stringsAsFactors = T)
in_rast  <- raster::stack(system.file("extdata", "sprawl_EVItest.tif",
                       package = "sprawl.data"))[[1]]
in_polys <- sf::st_transform(in_polys, proj4string(in_rast))
masked   <- mask_rast(in_rast, in_polys, verbose = FALSE)
plot_rast(in_rast, in_poly = in_polys)
plot_rast(masked, in_poly = in_polys)

## End(Not run)

IREA-CNR-MI/sprawl documentation built on May 27, 2019, 1:12 p.m.