Skip to content

No colormapping support for float dtype #658

@banesullivan-kobold

Description

@banesullivan-kobold

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:

Details
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions