cogeotiff / rio-tiler

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

Update `data_as_image` to return masked values #635

Closed JackDunnNZ closed 1 year ago

JackDunnNZ commented 1 year ago

Prior to this change, the raw data array was being returned, which could include nodata values, throwing off an automatic image color scale. In the following example, the nodata value used is -9999 (for all oceans), which causes the image to basically appear as a land mask:

from matplotlib.pyplot import imshow
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.02.01.cog") as r:
    img = r.read(nodata=-9999)

imshow(img.data_as_image())

image

If instead we use the masked array values, the automatic color scale is scoped to just the valid data values:

imshow(img.array[0, :, :])

image

vincentsarago commented 1 year ago

@JackDunnNZ will imshow(img.data_as_image()) still work event if the return type is a Numpy Masked array?

JackDunnNZ commented 1 year ago

Yes it seems to, the second example above is passing a masked array to imshow. I also get the same result running imshow(img.data_as_image()) on this branch

vincentsarago commented 1 year ago

@JackDunnNZ can you add something in the changelog 🙏

vincentsarago commented 1 year ago

🙏