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.9822180
#> 2  ID-02 3.4319303
#> 3  ID-03 3.9273469
#> 4  ID-04 0.2887684
#> 5  ID-05 1.0191861
#> 6  ID-06 1.4394056
#> 7  ID-07 3.3074858
#> 8  ID-08 3.7643452
#> 9  ID-09 1.9196675
#> 10 ID-10 3.5066707
#> 11 ID-11 1.7782123
#> 12 ID-12 0.7785873
#> 13 ID-13 2.0458314
#> 14 ID-14 1.1224172
#> 15 ID-15 3.9863424
#> 16 ID-16 2.9632106
extract_at(rrast, nc, "NAME", "mean")
#> Switch terra class to sf...
#>             NAME     mean
#> 1           Ashe 1.947389
#> 2      Alleghany 1.967786
#> 3          Surry 1.979187
#> 4      Currituck 2.087876
#> 5    Northampton 1.995565
#> 6       Hertford 2.055210
#> 7         Camden 2.007897
#> 8          Gates 1.988871
#> 9         Warren 2.030981
#> 10        Stokes 1.948921
#> 11       Caswell 2.259249
#> 12    Rockingham 1.933287
#> 13     Granville 1.968706
#> 14        Person 2.050704
#> 15         Vance 2.038220
#> 16       Halifax 2.146046
#> 17    Pasquotank 1.875998
#> 18        Wilkes 1.915945
#> 19       Watauga 1.895705
#> 20    Perquimans 1.864330
#> 21        Chowan 2.067016
#> 22         Avery 2.087213
#> 23        Yadkin 2.030048
#> 24      Franklin 1.952199
#> 25       Forsyth 2.081115
#> 26      Guilford 1.971956
#> 27      Alamance 1.851228
#> 28        Bertie 1.861882
#> 29        Orange 1.943045
#> 30        Durham 2.267816
#> 31          Nash 2.116163
#> 32      Mitchell 2.020571
#> 33     Edgecombe 2.139718
#> 34      Caldwell 2.003096
#> 35        Yancey 1.859110
#> 36        Martin 1.997396
#> 37          Wake 1.919060
#> 38       Madison 1.956800
#> 39       Iredell 1.979581
#> 40         Davie 2.242431
#> 41     Alexander 1.812335
#> 42      Davidson 2.031528
#> 43         Burke 1.928706
#> 44    Washington 2.051181
#> 45       Tyrrell 2.120093
#> 46      McDowell 1.951827
#> 47      Randolph 1.997752
#> 48       Chatham 1.956700
#> 49        Wilson 2.065808
#> 50         Rowan 1.879705
#> 51          Pitt 1.969182
#> 52       Catawba 2.094156
#> 53      Buncombe 2.115411
#> 54      Johnston 2.030389
#> 55       Haywood 1.925136
#> 56          Dare 2.003935
#> 57      Beaufort 1.995800
#> 58         Swain 1.995033
#> 59        Greene 1.892648
#> 60           Lee 1.786665
#> 61    Rutherford 2.080568
#> 62         Wayne 2.036669
#> 63       Harnett 2.025676
#> 64     Cleveland 2.155538
#> 65       Lincoln 2.244003
#> 66       Jackson 2.096980
#> 67         Moore 2.074826
#> 68   Mecklenburg 1.946311
#> 69      Cabarrus 1.985470
#> 70    Montgomery 1.982361
#> 71        Stanly 1.831009
#> 72     Henderson 2.046881
#> 73        Graham 1.807090
#> 74        Lenoir 1.992868
#> 75  Transylvania 2.139676
#> 76        Gaston 1.860851
#> 77          Polk 2.001978
#> 78         Macon 2.087376
#> 79       Sampson 2.149392
#> 80       Pamlico 1.884379
#> 81      Cherokee 1.974911
#> 82    Cumberland 1.973663
#> 83         Jones 1.979058
#> 84         Union 1.981829
#> 85         Anson 2.053550
#> 86          Hoke 1.982194
#> 87          Hyde 2.087428
#> 88        Duplin 2.001726
#> 89      Richmond 2.093513
#> 90          Clay 1.872997
#> 91        Craven 1.985506
#> 92      Scotland 1.972763
#> 93        Onslow 1.982935
#> 94       Robeson 2.052673
#> 95      Carteret 2.064187
#> 96        Bladen 2.083948
#> 97        Pender 1.920480
#> 98      Columbus 2.075451
#> 99   New Hanover 1.874797
#> 100    Brunswick 1.911686
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.947389
#> 2      Alleghany 1.967786
#> 3          Surry 1.979187
#> 4      Currituck 2.087876
#> 5    Northampton 1.995565
#> 6       Hertford 2.055210
#> 7         Camden 2.007897
#> 8          Gates 1.988871
#> 9         Warren 2.030981
#> 10        Stokes 1.948921
#> 11       Caswell 2.259249
#> 12    Rockingham 1.933287
#> 13     Granville 1.968706
#> 14        Person 2.050704
#> 15         Vance 2.038220
#> 16       Halifax 2.146046
#> 17    Pasquotank 1.875998
#> 18        Wilkes 1.915945
#> 19       Watauga 1.895705
#> 20    Perquimans 1.864330
#> 21        Chowan 2.067016
#> 22         Avery 2.087213
#> 23        Yadkin 2.030048
#> 24      Franklin 1.952199
#> 25       Forsyth 2.081115
#> 26      Guilford 1.971956
#> 27      Alamance 1.851228
#> 28        Bertie 1.861882
#> 29        Orange 1.943045
#> 30        Durham 2.267816
#> 31          Nash 2.116163
#> 32      Mitchell 2.020571
#> 33     Edgecombe 2.139718
#> 34      Caldwell 2.003096
#> 35        Yancey 1.859110
#> 36        Martin 1.997396
#> 37          Wake 1.919060
#> 38       Madison 1.956800
#> 39       Iredell 1.979581
#> 40         Davie 2.242431
#> 41     Alexander 1.812335
#> 42      Davidson 2.031528
#> 43         Burke 1.928706
#> 44    Washington 2.051181
#> 45       Tyrrell 2.120093
#> 46      McDowell 1.951827
#> 47      Randolph 1.997752
#> 48       Chatham 1.956700
#> 49        Wilson 2.065808
#> 50         Rowan 1.879705
#> 51          Pitt 1.969182
#> 52       Catawba 2.094156
#> 53      Buncombe 2.115411
#> 54      Johnston 2.030389
#> 55       Haywood 1.925136
#> 56          Dare 2.003935
#> 57      Beaufort 1.995800
#> 58         Swain 1.995033
#> 59        Greene 1.892648
#> 60           Lee 1.786665
#> 61    Rutherford 2.080568
#> 62         Wayne 2.036669
#> 63       Harnett 2.025676
#> 64     Cleveland 2.155538
#> 65       Lincoln 2.244003
#> 66       Jackson 2.096980
#> 67         Moore 2.074826
#> 68   Mecklenburg 1.946311
#> 69      Cabarrus 1.985470
#> 70    Montgomery 1.982361
#> 71        Stanly 1.831009
#> 72     Henderson 2.046881
#> 73        Graham 1.807090
#> 74        Lenoir 1.992868
#> 75  Transylvania 2.139676
#> 76        Gaston 1.860851
#> 77          Polk 2.001978
#> 78         Macon 2.087376
#> 79       Sampson 2.149392
#> 80       Pamlico 1.884379
#> 81      Cherokee 1.974911
#> 82    Cumberland 1.973663
#> 83         Jones 1.979058
#> 84         Union 1.981829
#> 85         Anson 2.053550
#> 86          Hoke 1.982194
#> 87          Hyde 2.087428
#> 88        Duplin 2.001726
#> 89      Richmond 2.093513
#> 90          Clay 1.872997
#> 91        Craven 1.985506
#> 92      Scotland 1.972763
#> 93        Onslow 1.982935
#> 94       Robeson 2.052673
#> 95      Carteret 2.064187
#> 96        Bladen 2.083948
#> 97        Pender 1.920480
#> 98      Columbus 2.075451
#> 99   New Hanover 1.874797
#> 100    Brunswick 1.911686
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