extract2 {starsExtra} | R Documentation |
Extract raster values by lines or polygons
Description
Extract raster values by lines or polygons, summarizing for each feature using a function specified by the user. This function is aimed to reproduce (some of) the functionality of raster::extract
.
Usage
extract2(x, v, fun, progress = TRUE, ...)
Arguments
x |
A |
v |
An |
fun |
A function to summarize cell values per feature/band |
progress |
Display progress bar? The default is |
... |
Further arguments passed to |
Value
A vector (single-band raster) or matrix
(multi-band raster) with the extracted and summarized values
Examples
# Polygons
pol = st_bbox(landsat)
pol = st_as_sfc(pol)
set.seed(1)
pol = st_sample(pol, 5)
pol = st_buffer(pol, 100)
pol = c(pol, pol)
# Plot
plot(landsat[,,,1,drop=TRUE], reset = FALSE)
plot(pol, add = TRUE)
# Single-band raster
aggregate(landsat[,,,1,drop=TRUE], pol, mean, na.rm = TRUE)[[1]] ## Duplicated areas get 'NA'
extract2(landsat[,,,1,drop=TRUE], pol, mean, na.rm = TRUE, progress = FALSE)
# Multi-band example
extract2(landsat, pol, mean, na.rm = TRUE, progress = FALSE)
# Lines
lines = st_cast(pol, "LINESTRING")
# Single-band raster
extract2(landsat[,,,1,drop=TRUE], lines, mean, na.rm = TRUE, progress = FALSE)
# Multi-band example
extract2(landsat, lines, mean, na.rm = TRUE, progress = FALSE)
[Package starsExtra version 0.2.8 Index]