cogeotiff / rio-tiler

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

No colormapping support for float dtype #658

Closed banesullivan-kobold closed 9 months ago

banesullivan-kobold commented 9 months ago

I'm working with some rasters that have float64 dtype and I'm trying to figure out how to best work with them for tile serving in rio-tiler.

At present, using a colormap with non-integer dtype rasters errors with rio-tiler:

from rio_tiler.io import Reader
from rio_tiler.colormap import cmap
reader = Reader("test_raster_float64.tif")
reader.tile(0, 0, 0).render(
    img_format="png",
    colormap=cmap.get("viridis"),
)
... in apply_cmap(data, colormap)
    [111]     return apply_discrete_cmap(data, colormap)
    [113] lookup_table = make_lut(colormap)
--> [114] data = lookup_table[data[0], :]
IndexError: arrays used as indices must be of integer (or boolean) type

Code to create the tiff:

```py import rasterio import numpy as np from rasterio.transform import from_origin # Create an array of float64 values data = np.random.rand(100, 100).astype('float64') # Define the transformation and metadata transform = from_origin(0, 0, 1, 1) # Assuming a basic transformation metadata = { 'driver': 'GTiff', 'height': data.shape[0], 'width': data.shape[1], 'count': 1, 'dtype': 'float64', 'crs': '+proj=latlong', 'transform': transform } # Create a test raster file with rasterio.open('test_raster_float64.tif', 'w', **metadata) as dst: dst.write(data, 1) ```

I'm wondering if there is an easy way to rescale my image before rendering to avoid this error and the warning that will happen when not using a colormap?

vincentsarago commented 9 months ago

@banesullivan-kobold Colormaps work with uint8 data (0->255) meaning that you'll have to first rescale the data from a min-max

https://cogeotiff.github.io/rio-tiler/intro/#rescale-non-byte-data-andor-apply-colormap