Learn R Programming

velox (version 0.2.0)

VeloxRaster_extract: Extract Values Given Polygons

Description

Extracts the values of all cells intersecting with a spatial object (line or polygon) sp and optionally applies R function fun.

Arguments

sp

A sf* POLYGON or MULTIPOLYGON object, a sf* LINE or MULTILINE object, a SpatialPolygons* object, or a SpatialLines* object.

fun

An R function. See Details.

df

Boolean. If TRUE, the return value will be a data frame (or list of data frames, see Details), otherwise a matrix (or list of matrices, see Details). If TRUE, a column ID_sp will be added to each data frame containing the ID of the sp object.

small

Boolean. If TRUE and sp contains polygons, then raster values for small (or oddly shaped) polygons that do not intersect with any cell centroid are established by intersecting the small polygon with the entire (boxed) cells.

legacy

Boolean. Whether to use legacy C++ code (pre velox 0.1.0-9007).

Value

If fun is passed: A numeric matrix or data frame (see argument df) with one row per element in sp, one column per band in the VeloxRaster.

Otherwise: A list of numeric matrices or data frames (see argument df), with one list element per element in sp. Each matrix/data frame consists of one column per band in the VeloxRaster, one row per raster cell intersecting with the geometry.

Details

If passed, fun must be an R function accepting a numeric vector as its first (and only mandatory) argument, and returning a scalar. If fun is NULL, extract returns a list of matrices, each matrix containing the raster values intersecting with the respective polygon (but see argument df). If sp contains polygons, then cell-polygon intersections are calculated based on cell centroids (but see argument small). If sp contains lines, then regular cell-line intersections are calculated.

Examples

Run this code
# NOT RUN {
## Make VeloxRaster with two bands
set.seed(0)
mat1 <- matrix(rnorm(100), 10, 10)
mat2 <- matrix(rnorm(100), 10, 10)
vx <- velox(list(mat1, mat2), extent=c(0,1,0,1), res=c(0.1,0.1),
            crs="+proj=longlat +datum=WGS84 +no_defs")
## Make SpatialPolygons
library(sp)
library(rgeos)
coord <- cbind(0.5, 0.5)
spoint <- SpatialPoints(coords=coord)
spols <- gBuffer(spgeom=spoint, width=0.5)
## Extract
vx$extract(sp=spols, fun=mean)

# }

Run the code above in your browser using DataLab