Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast data to uint8 #766

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 7.2.1 (2024-11-14)

* add official support for floating point values in ColorMap
* cast data to `uint8` datatype when applying linear colormap

# 7.2.0 (2024-11-05)

Expand Down
17 changes: 10 additions & 7 deletions rio_tiler/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import pathlib
import re
import warnings
from typing import Dict, List, Sequence, Tuple, Union

import attr
Expand Down Expand Up @@ -125,17 +126,19 @@ def apply_cmap(data: numpy.ndarray, colormap: ColorMapType) -> DataMaskType:
):
return apply_discrete_cmap(data, colormap)

cm = {int(k): v for k, v in colormap.items()}
lookup_table = make_lut(cm) # type: ignore
# For now we assume ColorMap are in uint8
if data.dtype != numpy.uint8:
warnings.warn(
f"Input array is of type {data.dtype} and `will be converted to Int in order to apply the ColorMap.",
UserWarning,
)
data = data.astype(numpy.uint8)

lookup_table = make_lut(colormap) # type: ignore
data = lookup_table[data[0], :]

data = numpy.transpose(data, [2, 0, 1])

# If the colormap has values between 0-255
# we cast the output array to Uint8.
if data.min() >= 0 and data.max() <= 255:
data = data.astype("uint8")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data will be Uint8 and the Lut is also uint8 so there is no need for this

return data[:-1], data[-1]


Expand Down
Binary file added tests/fixtures/cog_int16.tif
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/test_io_rasterio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from rasterio.vrt import WarpedVRT
from rasterio.warp import transform_bounds

from rio_tiler.colormap import cmap
from rio_tiler.constants import WEB_MERCATOR_TMS, WGS84_CRS
from rio_tiler.errors import (
ExpressionMixingWarning,
Expand Down Expand Up @@ -1120,3 +1121,27 @@ def test_inverted_latitude():
with pytest.warns(UserWarning):
with Reader(COG_INVERTED) as src:
_ = src.tile(0, 0, 0)


def test_int16_colormap():
"""Should raise a warning about invalid data type for applying colormap.

ref: https://github.com/developmentseed/titiler/issues/1023
"""
data = os.path.join(PREFIX, "cog_int16.tif")
color_map = cmap.get("viridis")

with Reader(data) as src:
info = src.info()
assert info.dtype == "int16"
assert info.nodata_type == "Nodata"
assert info.nodata_value == -32768

img = src.preview()
assert img.mask.any()

with pytest.warns(UserWarning):
im = img.apply_colormap(color_map)

# make sure we keep the nodata part masked
assert not im.mask.all()
Loading