Skip to contents

For simplicity, it is assumed that the coordinate systems of the points and the raster are the same.

Usage

extract_at_poly(
  polys = NULL,
  surf = NULL,
  id = NULL,
  func = "mean",
  extent = NULL,
  max_cells = 2e+07,
  ...
)

Arguments

polys

sf/SpatVector object. Polygons.

surf

SpatRaster object or file path(s) with extensions that are GDAL-compatible. A raster from which a summary will be calculated

id

character(1). Unique identifier of each point.

func

a generic function name in string or a function taking two arguments that are compatible with exact_extract. For example, "mean" or \(x, w) weighted.mean(x, w, na.rm = TRUE)

extent

numeric(4) or SpatExtent. Extent of clipping vector. It only works with polys of character(1) file path. When using numeric(4), it should be in the order of c(xmin, xmax, ymin, ymax). The coordinate system should be the same as the polys.

max_cells

integer(1). Maximum number of cells in memory. See exactextractr::exact_extract for more details.

...

Placeholder.

Value

a data.frame object with function value

Note

When Sys.setenv("CHOPIN_FORCE_CROP" = "TRUE") is set, the raster will be cropped to the extent of the polygons (with snap = "out"). To note, the function is designed to work with the exactextractr package. Arguments of exactextractr::exact_extract are set as below (default otherwise listed except for max_cells_in_memory, which is set in the max_cells argument):

  • force_df = TRUE

  • stack_apply = TRUE

  • progress = FALSE

See also

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

Author

Insang Song geoissong@gmail.com

Examples

ncpath <- system.file("gpkg/nc.gpkg", package = "sf")
nc <- terra::vect(ncpath)
nc <- terra::project(nc, "EPSG:5070")
rrast <- terra::rast(nc, nrow = 100, ncol = 220)
ncr <- terra::rasterize(nc, rrast)
terra::values(rrast) <- rgamma(2.2e4, 4, 2)
rpnt <- terra::spatSample(rrast, 16L, as.points = TRUE)
rpnt$pid <- sprintf("ID-%02d", seq(1, 16))
rpoly <-
  terra::buffer(rpnt, 5, capstyle = "square", joinstyle = "bevel")
extract_at_poly(rpoly, rrast, "pid")
#>      pid      mean
#> 1  ID-01 1.3605092
#> 2  ID-02 2.2859938
#> 3  ID-03 1.5757203
#> 4  ID-04 2.2192740
#> 5  ID-05 2.4626040
#> 6  ID-06 1.3844070
#> 7  ID-07 4.8444252
#> 8  ID-08 2.0995440
#> 9  ID-09 0.7677558
#> 10 ID-10 2.1429260
#> 11 ID-11 2.7326655
#> 12 ID-12 1.1205804
#> 13 ID-13 1.3867524
#> 14 ID-14 1.1439190
#> 15 ID-15 0.8089886
#> 16 ID-16 2.1020586