isciences / exactextractr

R package for fast and accurate raster zonal statistics
https://isciences.gitlab.io/exactextractr/
272 stars 26 forks source link

Support weighted usage of exact_resample #59

Open amarbler opened 3 years ago

amarbler commented 3 years ago

Hello,

I think I found a bug in the exact_resample() function. While exact_resample() accepts "weighted_mean" and "weighted_sum" as functions, it does not accept the weights argument:


library(raster)
library(exactextractr)

r = raster::raster(resolution = 2)
target = raster::shift(r, 2.5, 1)

set.seed(1111)
raster::values(r) = as.integer(round(rnorm(raster::ncell(r), 0, 1)))

w_area = raster::area(r)

res_r = exactextractr::exact_resample(r, target, fun = "weighted_mean", weights = w_area)
Error in .local(x, y, ...) : unused argument (weights = w_area)

res_r = exactextractr::exact_resample(r, target, fun = "weighted_sum", weights = w_area)
Error in .local(x, y, ...) : unused argument (weights = w_area)

sessionInfo()

R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 10 (buster)

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] exactextractr_0.7.0 raster_3.4-13       sp_1.4-5

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7.2       compiler_3.5.2     pillar_1.6.1       R.methodsS3_1.8.1
 [5] R.utils_2.10.1     class_7.3-19       iterators_1.0.13   tools_3.5.2
 [9] gdalUtils_2.0.3.2  lifecycle_1.0.0    tibble_3.1.2       lattice_0.20-44
[13] pkgconfig_2.0.3    rlang_0.4.11       foreach_1.5.1      DBI_1.1.1
[17] rgdal_1.5-23       e1071_1.7-8        dplyr_1.0.7        generics_0.1.0
[21] vctrs_0.3.8        classInt_0.4-3     grid_3.5.2         tidyselect_1.1.1
[25] glue_1.4.2         sf_1.0-3           R6_2.5.0           fansi_0.5.0
[29] purrr_0.3.4        magrittr_2.0.1     codetools_0.2-18   ellipsis_0.3.2
[33] units_0.7-2        assertthat_0.2.1   KernSmooth_2.23-20 utf8_1.2.1
[37] proxy_0.4-26       crayon_1.4.1       R.oo_1.24.0
dbaston commented 3 years ago

I've updated this to throw an error if the weighted operations are used. Can you share what you're trying to accomplish with them?

amarbler commented 3 years ago

I am trying to resample precipitation data into a coarser grid and wanted to use the "weighted_mean" function instead of the "mean" to take into account that the areas of pixels within the coarser cells have different areas b/c it is a lon/lat grid.

dbaston commented 3 years ago

That makes sense. I think for now you'd need to multiply the precipitation flux (fine grid) by area to convert to a precipitation amount, resample to the coarse grid using sum, and then divide by area again to get back to a precipitation flux.

dbaston commented 2 years ago

This doesn't solve the general case of weighting, but the coverage_area argument added to exact_resample should cover the precipitation averaging scenario you describe.

amarbler commented 2 years ago

Thank you!