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 argumentsx
andy
.- 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
orterra
.- 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 inpar_*
functions, set this toFALSE
.
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 andSpatVector
inputs will be converted tosf
object. Ifradius
is not NULL,sf::st_buffer
is used to generate circular buffers as subsequent raster-vector overlay is done withexactextractr::exact_extract
.Raster input in
x
:SpatRaster
is preferred. If the input is notSpatRaster
, it will be converted toSpatRaster
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.947390
#> 2 Alleghany 1.967787
#> 3 Surry 1.979185
#> 4 Currituck 2.087879
#> 5 Northampton 1.995564
#> 6 Hertford 2.055216
#> 7 Camden 2.007895
#> 8 Gates 1.988872
#> 9 Warren 2.030980
#> 10 Stokes 1.948919
#> 11 Caswell 2.259248
#> 12 Rockingham 1.933288
#> 13 Granville 1.968703
#> 14 Person 2.050707
#> 15 Vance 2.038214
#> 16 Halifax 2.146045
#> 17 Pasquotank 1.875991
#> 18 Wilkes 1.915943
#> 19 Watauga 1.895707
#> 20 Perquimans 1.864332
#> 21 Chowan 2.067013
#> 22 Avery 2.087210
#> 23 Yadkin 2.030051
#> 24 Franklin 1.952200
#> 25 Forsyth 2.081115
#> 26 Guilford 1.971956
#> 27 Alamance 1.851228
#> 28 Bertie 1.861882
#> 29 Orange 1.943042
#> 30 Durham 2.267813
#> 31 Nash 2.116162
#> 32 Mitchell 2.020578
#> 33 Edgecombe 2.139719
#> 34 Caldwell 2.003097
#> 35 Yancey 1.859106
#> 36 Martin 1.997395
#> 37 Wake 1.919064
#> 38 Madison 1.956803
#> 39 Iredell 1.979584
#> 40 Davie 2.242437
#> 41 Alexander 1.812335
#> 42 Davidson 2.031528
#> 43 Burke 1.928709
#> 44 Washington 2.051182
#> 45 Tyrrell 2.120088
#> 46 McDowell 1.951825
#> 47 Randolph 1.997749
#> 48 Chatham 1.956700
#> 49 Wilson 2.065812
#> 50 Rowan 1.879703
#> 51 Pitt 1.969181
#> 52 Catawba 2.094155
#> 53 Buncombe 2.115411
#> 54 Johnston 2.030387
#> 55 Haywood 1.925143
#> 56 Dare 2.003934
#> 57 Beaufort 1.995797
#> 58 Swain 1.995029
#> 59 Greene 1.892647
#> 60 Lee 1.786664
#> 61 Rutherford 2.080569
#> 62 Wayne 2.036669
#> 63 Harnett 2.025678
#> 64 Cleveland 2.155540
#> 65 Lincoln 2.244004
#> 66 Jackson 2.096978
#> 67 Moore 2.074827
#> 68 Mecklenburg 1.946310
#> 69 Cabarrus 1.985471
#> 70 Montgomery 1.982364
#> 71 Stanly 1.831006
#> 72 Henderson 2.046879
#> 73 Graham 1.807080
#> 74 Lenoir 1.992867
#> 75 Transylvania 2.139678
#> 76 Gaston 1.860849
#> 77 Polk 2.001973
#> 78 Macon 2.087381
#> 79 Sampson 2.149392
#> 80 Pamlico 1.884383
#> 81 Cherokee 1.974908
#> 82 Cumberland 1.973660
#> 83 Jones 1.979061
#> 84 Union 1.981828
#> 85 Anson 2.053549
#> 86 Hoke 1.982197
#> 87 Hyde 2.087430
#> 88 Duplin 2.001727
#> 89 Richmond 2.093516
#> 90 Clay 1.872995
#> 91 Craven 1.985506
#> 92 Scotland 1.972766
#> 93 Onslow 1.982934
#> 94 Robeson 2.052670
#> 95 Carteret 2.064187
#> 96 Bladen 2.083948
#> 97 Pender 1.920480
#> 98 Columbus 2.075454
#> 99 New Hanover 1.874795
#> 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
#> `/home/runner/work/_temp/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.947390
#> 2 Alleghany 1.967787
#> 3 Surry 1.979185
#> 4 Currituck 2.087879
#> 5 Northampton 1.995564
#> 6 Hertford 2.055216
#> 7 Camden 2.007895
#> 8 Gates 1.988872
#> 9 Warren 2.030980
#> 10 Stokes 1.948919
#> 11 Caswell 2.259248
#> 12 Rockingham 1.933288
#> 13 Granville 1.968703
#> 14 Person 2.050707
#> 15 Vance 2.038214
#> 16 Halifax 2.146045
#> 17 Pasquotank 1.875991
#> 18 Wilkes 1.915943
#> 19 Watauga 1.895707
#> 20 Perquimans 1.864332
#> 21 Chowan 2.067013
#> 22 Avery 2.087210
#> 23 Yadkin 2.030051
#> 24 Franklin 1.952200
#> 25 Forsyth 2.081115
#> 26 Guilford 1.971956
#> 27 Alamance 1.851228
#> 28 Bertie 1.861882
#> 29 Orange 1.943042
#> 30 Durham 2.267813
#> 31 Nash 2.116162
#> 32 Mitchell 2.020578
#> 33 Edgecombe 2.139719
#> 34 Caldwell 2.003097
#> 35 Yancey 1.859106
#> 36 Martin 1.997395
#> 37 Wake 1.919064
#> 38 Madison 1.956803
#> 39 Iredell 1.979584
#> 40 Davie 2.242437
#> 41 Alexander 1.812335
#> 42 Davidson 2.031528
#> 43 Burke 1.928709
#> 44 Washington 2.051182
#> 45 Tyrrell 2.120088
#> 46 McDowell 1.951825
#> 47 Randolph 1.997749
#> 48 Chatham 1.956700
#> 49 Wilson 2.065812
#> 50 Rowan 1.879703
#> 51 Pitt 1.969181
#> 52 Catawba 2.094155
#> 53 Buncombe 2.115411
#> 54 Johnston 2.030387
#> 55 Haywood 1.925143
#> 56 Dare 2.003934
#> 57 Beaufort 1.995797
#> 58 Swain 1.995029
#> 59 Greene 1.892647
#> 60 Lee 1.786664
#> 61 Rutherford 2.080569
#> 62 Wayne 2.036669
#> 63 Harnett 2.025678
#> 64 Cleveland 2.155540
#> 65 Lincoln 2.244004
#> 66 Jackson 2.096978
#> 67 Moore 2.074827
#> 68 Mecklenburg 1.946310
#> 69 Cabarrus 1.985471
#> 70 Montgomery 1.982364
#> 71 Stanly 1.831006
#> 72 Henderson 2.046879
#> 73 Graham 1.807080
#> 74 Lenoir 1.992867
#> 75 Transylvania 2.139678
#> 76 Gaston 1.860849
#> 77 Polk 2.001973
#> 78 Macon 2.087381
#> 79 Sampson 2.149392
#> 80 Pamlico 1.884383
#> 81 Cherokee 1.974908
#> 82 Cumberland 1.973660
#> 83 Jones 1.979061
#> 84 Union 1.981828
#> 85 Anson 2.053549
#> 86 Hoke 1.982197
#> 87 Hyde 2.087430
#> 88 Duplin 2.001727
#> 89 Richmond 2.093516
#> 90 Clay 1.872995
#> 91 Craven 1.985506
#> 92 Scotland 1.972766
#> 93 Onslow 1.982934
#> 94 Robeson 2.052670
#> 95 Carteret 2.064187
#> 96 Bladen 2.083948
#> 97 Pender 1.920480
#> 98 Columbus 2.075454
#> 99 New Hanover 1.874795
#> 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
#> `/home/runner/work/_temp/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
#> `/home/runner/work/_temp/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