Description Usage Arguments Value Examples
Extract values from raster layers for a given set of points or polygons. Using velox
package in parallel for fast extraction.
1 2 3 4 5 6 7 8 9 |
sf |
sf data frame containing point or polygon data. |
ras |
A Raster* object (RasterLayer, RasterStack, or RasterBrick), a named list of Raster objects, or a character vector or list of paths to Raster files on disk (e.g. as obtained through |
funct |
The name of a function to summarise raster values within polygons. Default is 'mean.na' (simple average, excluding NA), but other functions can be used (in that case, provide function name without quotes, e.g. funct = median). See |
small.algo |
Logical. Use 'small' algorithm to detect overlap between polygons and raster cells? See |
col.names |
Optional. Character vector with names for extracted columns in the output dataframe. If not provided, the function will use the Raster* layer names or, if |
parallel |
Logical. Run function in parallel (using |
n.cores |
Number of cores to use when parallel = TRUE. If not specified, using all available cores (see |
A sf data frame with the same number of rows as the original, and new columns containing the extracted raster values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | library(raster)
library(rgis)
## Example taken from raster::extract
# Create polygons
poly1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
poly2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
polys <- spPolygons(poly1, poly2)
polys.sf <- sf::st_as_sf(polys)
sf::st_crs(polys.sf) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
# Create rasters
r1 <- raster(ncol = 36, nrow = 18, vals = 1:(18*36))
r2 <- raster(ncol = 36, nrow = 18, vals = (1:(18*36))*2)
ras <- stack(r1, r2)
plot(ras, 1)
plot(polys, add = TRUE)
# Extract values
extract.parallel <- fast_extract(sf = polys.sf, ras = ras, parallel = TRUE)
head(extract.parallel)
extract.noparallel <- fast_extract(sf = polys.sf, ras = ras, parallel = FALSE)
# Compare with raster::extract
raster.extract <- raster::extract(ras, polys, fun = mean, df = TRUE)
head(raster.extract)
### Providing named list of rasters
ras.list <- list(r1 = r1, r2 = r2)
rgis.out <- fast_extract(sf = polys.sf, ras = ras.list, parallel = FALSE)
head(rgis.out)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.