Skip to contents

Extract raster values with point buffers or polygons

Usage

extract_at(x, y, ...)

# S4 method for class '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 class '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 class '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 class '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 class '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 class '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,
  ...
)

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.

Value

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

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

Author

Insang Song geoissong@gmail.com

Examples

ncpath <- system.file("gpkg/nc.gpkg", package = "sf")
rastpath <- system.file("extdata/nc_srtm15_otm.tif", package = "chopin")

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

extract_at(rrast, rpnt, "pid", "mean", radius = 1000)
#> Switch terra class to sf...
#>      pid      mean
#> 1  ID-01 1.9822181
#> 2  ID-02 3.4319303
#> 3  ID-03 3.9273467
#> 4  ID-04 0.2887684
#> 5  ID-05 1.0191861
#> 6  ID-06 1.4394057
#> 7  ID-07 3.3074858
#> 8  ID-08 3.7643454
#> 9  ID-09 1.9196675
#> 10 ID-10 3.5066707
#> 11 ID-11 1.7782122
#> 12 ID-12 0.7785873
#> 13 ID-13 2.0458317
#> 14 ID-14 1.1224172
#> 15 ID-15 3.9863422
#> 16 ID-16 2.9632106
extract_at(rrast, nc, "NAME", "mean")
#> Switch terra class to sf...
#>             NAME     mean
#> 1           Ashe 1.947373
#> 2      Alleghany 1.967829
#> 3          Surry 1.979166
#> 4      Currituck 2.087910
#> 5    Northampton 1.995594
#> 6       Hertford 2.055209
#> 7         Camden 2.007728
#> 8          Gates 1.988871
#> 9         Warren 2.030988
#> 10        Stokes 1.948884
#> 11       Caswell 2.259268
#> 12    Rockingham 1.933299
#> 13     Granville 1.968687
#> 14        Person 2.050681
#> 15         Vance 2.038219
#> 16       Halifax 2.146035
#> 17    Pasquotank 1.876140
#> 18        Wilkes 1.915930
#> 19       Watauga 1.895659
#> 20    Perquimans 1.864217
#> 21        Chowan 2.067043
#> 22         Avery 2.087230
#> 23        Yadkin 2.030065
#> 24      Franklin 1.952211
#> 25       Forsyth 2.081105
#> 26      Guilford 1.971966
#> 27      Alamance 1.851238
#> 28        Bertie 1.861859
#> 29        Orange 1.943087
#> 30        Durham 2.267795
#> 31          Nash 2.116158
#> 32      Mitchell 2.020657
#> 33     Edgecombe 2.139695
#> 34      Caldwell 2.003155
#> 35        Yancey 1.859101
#> 36        Martin 1.997451
#> 37          Wake 1.919050
#> 38       Madison 1.956670
#> 39       Iredell 1.979587
#> 40         Davie 2.242482
#> 41     Alexander 1.812352
#> 42      Davidson 2.031530
#> 43         Burke 1.928711
#> 44    Washington 2.051171
#> 45       Tyrrell 2.120135
#> 46      McDowell 1.951835
#> 47      Randolph 1.997758
#> 48       Chatham 1.956705
#> 49        Wilson 2.065757
#> 50         Rowan 1.879678
#> 51          Pitt 1.969185
#> 52       Catawba 2.094098
#> 53      Buncombe 2.115458
#> 54      Johnston 2.030401
#> 55       Haywood 1.925160
#> 56          Dare 2.003897
#> 57      Beaufort 1.995803
#> 58         Swain 1.994998
#> 59        Greene 1.892638
#> 60           Lee 1.786678
#> 61    Rutherford 2.080518
#> 62         Wayne 2.036684
#> 63       Harnett 2.025692
#> 64     Cleveland 2.155510
#> 65       Lincoln 2.243986
#> 66       Jackson 2.097023
#> 67         Moore 2.074824
#> 68   Mecklenburg 1.946314
#> 69      Cabarrus 1.985481
#> 70    Montgomery 1.982317
#> 71        Stanly 1.831049
#> 72     Henderson 2.046894
#> 73        Graham 1.807232
#> 74        Lenoir 1.992871
#> 75  Transylvania 2.139632
#> 76        Gaston 1.860922
#> 77          Polk 2.001914
#> 78         Macon 2.087482
#> 79       Sampson 2.149409
#> 80       Pamlico 1.884407
#> 81      Cherokee 1.974882
#> 82    Cumberland 1.973618
#> 83         Jones 1.979062
#> 84         Union 1.981821
#> 85         Anson 2.053536
#> 86          Hoke 1.982203
#> 87          Hyde 2.087412
#> 88        Duplin 2.001717
#> 89      Richmond 2.093529
#> 90          Clay 1.872937
#> 91        Craven 1.985508
#> 92      Scotland 1.972783
#> 93        Onslow 1.982929
#> 94       Robeson 2.052696
#> 95      Carteret 2.064198
#> 96        Bladen 2.083966
#> 97        Pender 1.920480
#> 98      Columbus 2.075436
#> 99   New Hanover 1.874862
#> 100    Brunswick 1.911671
extract_at(rrast, ncpath, "NAME", "mean")
#>  Input is a character. Trying to read with sf.
#> Reading layer `nc.gpkg' from data source 
#>   `/usr/local/lib/R/site-library/sf/gpkg/nc.gpkg' using driver `GPKG'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
#>             NAME     mean
#> 1           Ashe 1.947373
#> 2      Alleghany 1.967829
#> 3          Surry 1.979166
#> 4      Currituck 2.087910
#> 5    Northampton 1.995594
#> 6       Hertford 2.055209
#> 7         Camden 2.007728
#> 8          Gates 1.988871
#> 9         Warren 2.030988
#> 10        Stokes 1.948884
#> 11       Caswell 2.259268
#> 12    Rockingham 1.933299
#> 13     Granville 1.968687
#> 14        Person 2.050681
#> 15         Vance 2.038219
#> 16       Halifax 2.146035
#> 17    Pasquotank 1.876140
#> 18        Wilkes 1.915930
#> 19       Watauga 1.895659
#> 20    Perquimans 1.864217
#> 21        Chowan 2.067043
#> 22         Avery 2.087230
#> 23        Yadkin 2.030065
#> 24      Franklin 1.952211
#> 25       Forsyth 2.081105
#> 26      Guilford 1.971966
#> 27      Alamance 1.851238
#> 28        Bertie 1.861859
#> 29        Orange 1.943087
#> 30        Durham 2.267795
#> 31          Nash 2.116158
#> 32      Mitchell 2.020657
#> 33     Edgecombe 2.139695
#> 34      Caldwell 2.003155
#> 35        Yancey 1.859101
#> 36        Martin 1.997451
#> 37          Wake 1.919050
#> 38       Madison 1.956670
#> 39       Iredell 1.979587
#> 40         Davie 2.242482
#> 41     Alexander 1.812352
#> 42      Davidson 2.031530
#> 43         Burke 1.928711
#> 44    Washington 2.051171
#> 45       Tyrrell 2.120135
#> 46      McDowell 1.951835
#> 47      Randolph 1.997758
#> 48       Chatham 1.956705
#> 49        Wilson 2.065757
#> 50         Rowan 1.879678
#> 51          Pitt 1.969185
#> 52       Catawba 2.094098
#> 53      Buncombe 2.115458
#> 54      Johnston 2.030401
#> 55       Haywood 1.925160
#> 56          Dare 2.003897
#> 57      Beaufort 1.995803
#> 58         Swain 1.994998
#> 59        Greene 1.892638
#> 60           Lee 1.786678
#> 61    Rutherford 2.080518
#> 62         Wayne 2.036684
#> 63       Harnett 2.025692
#> 64     Cleveland 2.155510
#> 65       Lincoln 2.243986
#> 66       Jackson 2.097023
#> 67         Moore 2.074824
#> 68   Mecklenburg 1.946314
#> 69      Cabarrus 1.985481
#> 70    Montgomery 1.982317
#> 71        Stanly 1.831049
#> 72     Henderson 2.046894
#> 73        Graham 1.807232
#> 74        Lenoir 1.992871
#> 75  Transylvania 2.139632
#> 76        Gaston 1.860922
#> 77          Polk 2.001914
#> 78         Macon 2.087482
#> 79       Sampson 2.149409
#> 80       Pamlico 1.884407
#> 81      Cherokee 1.974882
#> 82    Cumberland 1.973618
#> 83         Jones 1.979062
#> 84         Union 1.981821
#> 85         Anson 2.053536
#> 86          Hoke 1.982203
#> 87          Hyde 2.087412
#> 88        Duplin 2.001717
#> 89      Richmond 2.093529
#> 90          Clay 1.872937
#> 91        Craven 1.985508
#> 92      Scotland 1.972783
#> 93        Onslow 1.982929
#> 94       Robeson 2.052696
#> 95      Carteret 2.064198
#> 96        Bladen 2.083966
#> 97        Pender 1.920480
#> 98      Columbus 2.075436
#> 99   New Hanover 1.874862
#> 100    Brunswick 1.911671
extract_at(
  rrast, ncpath, "NAME", "mean",
  kernel = "epanechnikov",
  bandwidth = 1e5
)
#>  Input is a character. Trying to read with sf.
#> Reading layer `nc.gpkg' from data source 
#>   `/usr/local/lib/R/site-library/sf/gpkg/nc.gpkg' using driver `GPKG'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
#> Kernel function [ epanechnikov ] is applied to calculate weights...
#> Switch sf class to terra...
#> Warning: Point geometries are acceptable for kernel weighting. Convert to points...
#> (Note: inside = TRUE is applied)
#> # A tibble: 100 × 2
#>    NAME      value
#>    <chr>     <dbl>
#>  1 Alamance   1.85
#>  2 Alexander  1.81
#>  3 Alleghany  1.97
#>  4 Anson      2.06
#>  5 Ashe       1.95
#>  6 Avery      2.08
#>  7 Beaufort   1.99
#>  8 Bertie     1.86
#>  9 Bladen     2.08
#> 10 Brunswick  1.92
#> # ℹ 90 more rows
extract_at(
  rastpath, ncpath, "NAME", "mean",
  kernel = "epanechnikov",
  bandwidth = 1e5
)
#> Input is a character. Attempt to read it with terra::rast...
#>  Input is a character. Trying to read with sf.
#> Reading layer `nc.gpkg' from data source 
#>   `/usr/local/lib/R/site-library/sf/gpkg/nc.gpkg' using driver `GPKG'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
#> Kernel function [ epanechnikov ] is applied to calculate weights...
#> Switch sf class to terra...
#> Warning: Point geometries are acceptable for kernel weighting. Convert to points...
#> (Note: inside = TRUE is applied)
#> # A tibble: 100 × 2
#>    NAME       value
#>    <chr>      <dbl>
#>  1 Alamance   189. 
#>  2 Alexander  358. 
#>  3 Alleghany  877. 
#>  4 Anson      106. 
#>  5 Ashe       981. 
#>  6 Avery     1086. 
#>  7 Beaufort    11.8
#>  8 Bertie      18.9
#>  9 Bladen      26.7
#> 10 Brunswick   11.7
#> # ℹ 90 more rows