extractByMask: Extract raster values by mask

Description Usage Arguments Details Value See Also Examples

View source: R/extractByMask.R

Description

This function extracts the values from a given input raster based on a mask.

Usage

1
extractByMask(layer, msk, spatial = FALSE, rast = NULL)

Arguments

layer

an object of class "RasterLayer" (package raster). The raster from which data will be extracted

msk

an object of class "RasterLayer" or a two column matrix of coordinates. The reference raster (or coordinates) to be used as mask for extraction.

spatial

logical. Should the output be spatially explicit -i.e. a georeferenced raster? Default is FALSE, returning a vector of extracted values from rast. If TRUE an object of class "RasterLayer" is returned.

rast

deprecated, use layer instead.

Details

When input data given to rast does not match the resolution and extent of a raster mask argument, the latter is preferred. The function will therefore return a vector of n elements, one for each non NA cell in the mask. Input raster cells falling inside mask cells, but not over their cells centre will be ignored.

Value

a vector, or an object of class "RasterLayer". The values from the input raster (rast argument) at coordinates provided as matrix, or those overlapping with non NA cells in the mask raster. If spatial == TRUE an object of class "RasterLayer" is returned.

See Also

aoi

Examples

1
2
3
4
5
6
7
8
9
data(ConwyData)
list2env(ConwyData, environment())

m <- aoi(msk=ConwyLU, mskSub=c(2,3))
head( extractByMask(ConwySlope, msk=m), 20) 

# Extract making a raster
library(raster)
plot( extractByMask(ConwySlope, msk=m, spatial=TRUE) )

Example output

<environment: R_GlobalEnv>
 [1] 13 14  3 NA  9  2  1 11 12  8  3  2  1  1 12 17  8  6  9  2
Loading required package: sp
NOTE: rgdal::checkCRSArgs: no proj_defs.dat in PROJ.4 shared files
NOTE: rgdal::checkCRSArgs: no proj_defs.dat in PROJ.4 shared files
NOTE: rgdal::checkCRSArgs: no proj_defs.dat in PROJ.4 shared files

bnspatial documentation built on Jan. 17, 2020, 9:06 a.m.