cogeotiff / rio-tiler

User friendly Rasterio plugin to read raster datasets.
https://cogeotiff.github.io/rio-tiler/
BSD 3-Clause "New" or "Revised" License
505 stars 106 forks source link

Interpolation should happen after masking #634

Closed daisy12321 closed 1 year ago

daisy12321 commented 1 year ago

I noticed that if the data has a nodata mask, the interpolation seems to happen before masking, which results in very strange numbers. For example, if I run the following

from rio_tiler.io.rasterio import Reader

with Reader(
    "https://data.chc.ucsb.edu/products/CHIRPS-2.0/global_daily/cogs/p05/2023/chirps-v2.0.2023.06.06.cog"
) as reader:
    img = reader.preview(
        nodata=-9999,
    )
img

The bottom right of the image has values like -9998.1943359375, -10037.056640625, which seems like rio-tiler is interpolating with the nodata value of -9999.

Could it be that https://github.com/cogeotiff/rio-tiler/blob/45fc03c34b18ae66d89a38940d6756d2b9729e6e/rio_tiler/reader.py#L231-L233 should be set before the dataset.read here https://github.com/cogeotiff/rio-tiler/blob/45fc03c34b18ae66d89a38940d6756d2b9729e6e/rio_tiler/reader.py#L222-L229?

vincentsarago commented 1 year ago

the issue doesn't come from rio-tiler but from the fact that the COG was created without the nodata set. When it got created the overview creation introduced the artefacts.

when using the preview method, rio-tiler will use one of the overviews and thus return the wrong values.

The only way to avoid this is to regenerate the COG but with the nodata value set