fast_extract: Fast extract

Description Usage Arguments Details Value See Also Examples

Description

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

Usage

1
2
3
4
5
6
7
8
9
## S4 method for signature 'Raster,SpatialPolygons'
fast_extract(x, y, fun = mean,
  velox = requireNamespace("velox", quietly = TRUE), ...)

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

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

Arguments

x

Raster-class object.

y

Spatial-class object.

fun

function used to summarise 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.

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.

Value

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

See Also

extract, VeloxRaster_extract.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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)})

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

prioritizr/prioritizrutils documentation built on May 25, 2019, 12:20 p.m.