Learn R Programming

chopin (version 0.9.4)

extract_at: Extract raster values with point buffers or polygons

Description

Extract raster values with point buffers or polygons

Usage

extract_at(x, y, ...)

# S4 method for SpatRaster,sf extract_at( x = NULL, y = NULL, id = NULL, func = "mean", extent = NULL, radius = NULL, out_class = "sf", kernel = NULL, kernel_func = stats::weighted.mean, bandwidth = NULL, max_cells = 3e+07, .standalone = TRUE, ... )

# S4 method for character,character extract_at( x = NULL, y = NULL, id = NULL, func = "mean", extent = NULL, radius = NULL, out_class = "sf", kernel = NULL, kernel_func = stats::weighted.mean, bandwidth = NULL, max_cells = 3e+07, .standalone = TRUE, ... )

# S4 method for SpatRaster,character extract_at( x = NULL, y = NULL, id = NULL, func = "mean", extent = NULL, radius = NULL, out_class = "sf", kernel = NULL, kernel_func = stats::weighted.mean, bandwidth = NULL, max_cells = 3e+07, .standalone = TRUE, ... )

# S4 method for SpatRaster,SpatVector extract_at( x = NULL, y = NULL, id = NULL, func = "mean", extent = NULL, radius = NULL, out_class = "sf", kernel = NULL, kernel_func = stats::weighted.mean, bandwidth = NULL, max_cells = 3e+07, .standalone = TRUE, ... )

# S4 method for character,sf extract_at( x = NULL, y = NULL, id = NULL, func = "mean", extent = NULL, radius = NULL, out_class = "sf", kernel = NULL, kernel_func = stats::weighted.mean, bandwidth = NULL, max_cells = 3e+07, .standalone = TRUE, ... )

# S4 method for character,SpatVector extract_at( x = NULL, y = NULL, id = NULL, func = "mean", extent = NULL, radius = NULL, out_class = "sf", kernel = NULL, kernel_func = stats::weighted.mean, bandwidth = NULL, max_cells = 3e+07, .standalone = TRUE, ... )

Value

A data.frame object with summarized raster values with respect to the mode (polygon or buffer) and the function.

Arguments

x

SpatRaster object or file path(s) with extensions that are GDAL-compatible. When multiple file paths are used, the rasters must have the same extent and resolution.

y

sf/SpatVector object or file path.

...

Placeholder.

id

character(1). Unique identifier of each point.

func

function taking one numeric vector argument. Default is "mean" for all supported signatures in arguments x and y.

extent

numeric(4) or SpatExtent. Extent of clipping vector. It only works with points of character(1) file path.

radius

numeric(1). Buffer radius.

out_class

character(1). Output class. One of sf or terra.

kernel

character(1). Name of a kernel function One of "uniform", "triweight", "quartic", and "epanechnikov"

kernel_func

function. Kernel function to apply to the extracted values. Default is stats::weighted.mean()

bandwidth

numeric(1). Kernel bandwidth.

max_cells

integer(1). Maximum number of cells in memory.

.standalone

logical(1). Default is TRUE, which means that the function will be executed in a standalone mode. When using this function in par_* functions, set this to FALSE.

Author

Insang Song geoissong@gmail.com

Details

Inputs are preprocessed in different ways depending on the class.

  • Vector inputs in y: sf is preferred, thus character and SpatVector inputs will be converted to sf object. If radius is not NULL, sf::st_buffer is used to generate circular buffers as subsequent raster-vector overlay is done with exactextractr::exact_extract.

  • Raster input in x: SpatRaster is preferred. If the input is not SpatRaster, it will be converted to SpatRaster object.

See Also

Other Macros for calculation: kernelfunction(), summarize_aw(), summarize_sedc()

Examples

Run this code
ncpath <- system.file("gpkg/nc.gpkg", package = "sf")
rastpath <- file.path(tempdir(), "test.tif")

nc <- terra::vect(ncpath)
nc <- terra::project(nc, "EPSG:5070")
rrast <- terra::rast(nc, nrow = 300, ncol = 660)
terra::values(rrast) <- rgamma(1.98e5, 4, 2)
rpnt <- terra::spatSample(rrast, 16L, as.points = TRUE)
rpnt$pid <- sprintf("ID-%02d", seq(1, 16))

extract_at(rrast, rpnt, "pid", "mean", radius = 1000)
extract_at(rrast, nc, "NAME", "mean")
extract_at(rrast, ncpath, "NAME", "mean")
# Using SpatRaster object
suppressWarnings(
  extract_at(
    rrast, ncpath, "NAME", "mean",
    kernel = "epanechnikov",
    bandwidth = 1e5
  )
)
# Using raster path
terra::writeRaster(rrast, rastpath, overwrite = TRUE)
suppressWarnings(
  extract_at(
    rastpath, ncpath, "NAME", "mean",
    kernel = "epanechnikov",
    bandwidth = 1e5
  )
)

Run the code above in your browser using DataLab