isciences / exactextractr

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

NA values is removed arbitrarily #38

Closed kongdd closed 3 years ago

kongdd commented 3 years ago

I found that NA values is removed arbitrarily. This is not desired for regional ET, GPP statistics. In this situation, NA values should be regarded as zero.

Hope users could have the options to remove NA values or replace them with zero. Or the give the notice in the exact_extract description.

dbaston commented 3 years ago

I think replacing the NA values with zero is useful but beyond of the scope of this package. If you're working with a large out-of-memory raster, one way this can be accomplished is with a VRT file, e.g.:

convert_na_values_to <- 0

vrt1 <- tempfile(fileext = '.vrt')
vrt2 <- tempfile(fileext = '.vrt')

gdalUtils::gdalbuildvrt('myraster.tif', vrt1,  vrtnodata = convert_na_values_to)
gdalUtils::gdalbuildvrt(vrt1, vrt2,  srcnodata = 'none')

exact_extract(raster(vrt2), polys, 'mean') # NA values will be read as 0
kongdd commented 3 years ago

Thank you

dbaston commented 3 years ago

So, I caved. There are now default_value and default_weight arguments.