Learn R Programming

prioritizr (version 4.1.5)

fast_extract: Fast extract

Description

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

Usage

# 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
fun

function used to summarize values. Defaults to mean. 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

extract, VeloxRaster_extract.

Examples

Run this code
# NOT RUN {
# load data
data(sim_pu_polygons, sim_features)
# }
# NOT RUN {
# we will investigate several 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)})

# perform extract using the fast_extract function augmented using the
# "velox" package
system.time({result <- fast_extract(sim_features, sim_pu_polygons,
                                    velox = TRUE)})

# 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)})
set_number_of_threads(1)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab