Skip to contents

When x and y are different classes, poly_weight will be converted to the class of x.

Usage

summarize_aw(x, y, ...)

# S4 method for class 'SpatVector,SpatVector'
summarize_aw(
  x,
  y,
  target_fields = NULL,
  id_x = "ID",
  fun = stats::weighted.mean,
  extent = NULL,
  ...
)

# S4 method for class 'character,character'
summarize_aw(
  x,
  y,
  target_fields = NULL,
  id_x = "ID",
  fun = stats::weighted.mean,
  out_class = "terra",
  extent = NULL,
  ...
)

# S4 method for class 'sf,sf'
summarize_aw(
  x,
  y,
  target_fields = NULL,
  id_x = "ID",
  fun = NULL,
  extent = NULL,
  ...
)

Arguments

x

A sf/SpatVector object or file path of polygons detectable with GDAL driver at weighted means will be calculated.

y

A sf/SpatVector object or file path of polygons from which weighted means will be calculated.

...

Additional arguments depending on class of x and y.

target_fields

character. Field names to calculate area-weighted.

id_x

character(1). The unique identifier of each polygon in x. Default is "ID".

fun

function(1)/character(1). The function to calculate the weighted summary. Default is stats::weighted.mean. The function must have a w argument. If both x and y are sf, it should be one of c("sum", "mean"). It will determine extensive argument in sf::st_interpolate_aw.

extent

numeric(4) or SpatExtent object. Extent of clipping x. It only works with x of character(1) file path. See terra::ext for more details. Coordinate systems should match.

out_class

character(1). "sf" or "terra". Output class.

Value

A data.frame with all numeric fields of area-weighted means.

Note

x and y classes should match. If x and y are characters, they will be read as sf objects.

See also

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

Author

Insang Song geoissong@gmail.com

Examples

# package
library(sf)
options(sf_use_s2 = FALSE)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source 
#>   `/home/runner/work/_temp/Library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> 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
nc <- sf::st_transform(nc, 5070)
pp <- sf::st_sample(nc, size = 300)
pp <- sf::st_as_sf(pp)
pp[["id"]] <- seq(1, nrow(pp))
sf::st_crs(pp) <- "EPSG:5070"
ppb <- sf::st_buffer(pp, nQuadSegs=180, dist = units::set_units(20, "km"))

system.time(
  ppb_nc_aw <-
    summarize_aw(
      ppb, nc, c("BIR74", "BIR79"),
      "id", fun = "sum"
    )
)
#> Warning: st_interpolate_aw assumes attributes are constant or uniform over areas of x
#>    user  system elapsed 
#>   0.366   0.004   0.370 
summary(ppb_nc_aw)
#>      BIR74             BIR79                  geometry  
#>  Min.   :  159.6   Min.   :  244.7   POLYGON      :300  
#>  1st Qu.: 1278.6   1st Qu.: 1568.8   epsg:5070    :  0  
#>  Median : 2231.9   Median : 2724.9   +proj=aea ...:  0  
#>  Mean   : 3000.0   Mean   : 3832.5                      
#>  3rd Qu.: 3821.2   3rd Qu.: 4872.5                      
#>  Max.   :16384.9   Max.   :23159.3                      

# terra examples
library(terra)
ncpath <- system.file("gpkg/nc.gpkg", package = "sf")
elev <- system.file("ex/elev.tif", package = "terra")
nc <- terra::vect(ncpath)
elev <- terra::rast(elev)
pp <- terra::spatSample(nc, size = 300)
pp <- terra::project(pp, crs(elev))
pp <- terra::as.points(pp)
#> Warning: [as.points] returning a copy
pp[["id"]] <- seq(1, nrow(pp))
ppb <- terra::buffer(pp, 20000)

system.time(
  ppb_nc_aw <-
    summarize_aw(
      ppb, nc, c("BIR74", "BIR79"), "id",
      fun = sum
    )
)
#> Warning: [intersect] different crs
#>    user  system elapsed 
#>   0.086   0.000   0.086 
summary(ppb_nc_aw)
#>        id             BIR74               BIR79          
#>  Min.   :  1.00   Min.   :2.453e+08   Min.   :2.453e+08  
#>  1st Qu.: 75.75   1st Qu.:1.171e+09   1st Qu.:1.171e+09  
#>  Median :150.50   Median :1.251e+09   Median :1.251e+09  
#>  Mean   :150.50   Mean   :1.169e+09   Mean   :1.169e+09  
#>  3rd Qu.:225.25   3rd Qu.:1.251e+09   3rd Qu.:1.251e+09  
#>  Max.   :300.00   Max.   :1.252e+09   Max.   :1.252e+09