Learn R Programming

bnspatial (version 0.9)

extractByMask: Extract raster values by mask

Description

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

Usage

extractByMask(rast, msk, spatial = FALSE)

Arguments

rast
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.

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.

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.

See Also

aoi

Examples

Run this code
data(ConwyData)
m <- aoi(msk=currentLU, mskSub=c(2,3))
head( extractByMask(slope, msk=m), 20) 

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

Run the code above in your browser using DataLab