Extract data from a Raster-class object from a Spatial-class object using performance enhancing tricks.

# S4 method for Raster,SpatialPolygons
fast_extract(x, y, fun = mean, velox = requireNamespace("velox", quietly = TRUE), ...)

# S4 method for Raster,SpatialLines
fast_extract(x, y, fun = mean, ...)

# S4 method for Raster,SpatialPoints
fast_extract(x, y, fun = mean, ...)

Arguments

x

Raster-class object.

y

Spatial-class object.

fun

function used to summarize values. Defaults to sum. Note that this only used when x is a SpatialPolygons-class or a SpatialLines-class object. This function must have an na.rm argument.

velox

logical should the velox be used for geoprocessing? Defaults to TRUE if the package is installed. Note that this only used when x is a SpatialPolygons-class object.

...

additional arguments passed to extract.

Value

data.frame, matrix, or list object depending on the arguments.

Details

Spatial analyses will be conducted using the velox package if it is installed. Additionally, multiple threads can be used to speed up computation using the set_number_of_threads function.

See also

Examples

# load data data(sim_pu_polygons, sim_features)
# we will investigate severals ways for extracting values from a raster # using polygons. Specifically, for each band in the raster, # for each polygon in the vector layer, calculate the average # of the cells that are inside the polygon. # perform the extraction using the standard raster::extract function system.time({result <- fast_extract(sim_features, sim_pu_polygons)})
#> user system elapsed #> 0.02 0.00 0.02
# perform extract using the fast_extract function augmented using the # "velox" package system.time({result <- fast_extract(sim_features, sim_pu_polygons, velox = TRUE)})
#> user system elapsed #> 0.019 0.000 0.019
# perform extract using the fast_extract function with "velox" package # and using two threads for processing. Note that this might be slower # due to overheads but should yield faster processing times on larger # spatial data sets set_number_of_threads(2) system.time({result <- fast_extract(sim_features, sim_pu_polygons, velox = TRUE)})
#> Warning: <anonymous>: ... may be used in an incorrect context: '.fun(piece, ...)'
#> Warning: <anonymous>: ... may be used in an incorrect context: '.fun(piece, ...)'
#> user system elapsed #> 0.018 0.000 2.416