isciences / exactextract

Fast and accurate raster zonal statistics
Apache License 2.0
258 stars 33 forks source link

Add functionality to convert nodata to a fixed value #91

Closed dbaston closed 5 months ago

dbaston commented 6 months ago

Many population datasets use NODATA pixels for areas outside the modeled domain, e.g. ocean pixels. For the purpose of exactextract these should be considered equivalent to zero.

The following workaround is available in Python using GDAL:

from osgeo import gdal
gdal.BuildVRT('/vsimem/vrt1.vrt', "my_pop_data.tif", VRTNodata = 0)
pop_src = gdal.BuildVRT('', '/vsimem/vrt1.vrt', srcNodata = "none")

This is not clear in its intent and easy to get wrong. As an alternative, the exactextractr package uses default_weight and default_value arguments to convert NODATA into a constant value.

Similar functionality could be added to exactextract, either by:

1) modifying Raster to have a default_value argument. This would require that all RasterSource implementations in turn accept such a value, and pass it along when constructing Raster objects. 2) modifying RasterStats to take appropriate arguments, so you could call e.g.weighted_mean(default_weight=0). This would be automatically exposed in both the Python bindings and the CLI and would avoid the need to modify RasterSource implementations.

dbaston commented 5 months ago

Implemented in 364acf3ce579c3b51b78e70642460816b4da9ddc