Skip to content

Commit

Permalink
check if discrete colormap has negative values (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Apr 8, 2022
1 parent e8dc2df commit d775bab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

* Switch to `pyproject.toml` and `flit` for packaging
* Catch discrete colormap with negative values

# 3.1.2 (2022-03-25)

Expand Down
2 changes: 1 addition & 1 deletion rio_tiler/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def apply_cmap(data: numpy.ndarray, colormap: ColorMapType) -> DataMaskType:
# rio_tiler.colormap.make_lut, because we don't want to create a `lookup table`
# with more than 256 entries (256 x 4) array. In this case we use `apply_discrete_cmap`
# which can work with arbitrary colormap dict.
if len(colormap) > 256 or max(colormap) >= 256:
if len(colormap) > 256 or max(colormap) >= 256 or min(colormap) < 0:
return apply_discrete_cmap(data, colormap)

lookup_table = make_lut(colormap)
Expand Down
11 changes: 10 additions & 1 deletion tests/test_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,22 @@ def test_apply_discrete_cmap():
cm = deepcopy(colormap.EMPTY_COLORMAP)
cm.pop(255)
cm[1000] = (255, 255, 255, 255)

d, m = colormap.apply_cmap(data, cm)
dd, mm = colormap.apply_discrete_cmap(data, cm)
numpy.testing.assert_array_equal(dd, d)
numpy.testing.assert_array_equal(mm, m)

cm = {1: (0, 0, 0, 255), 256: (255, 255, 255, 255)}
assert colormap.apply_cmap(data, cm)

# Test with negative value
cm = {-100: (255, 255, 255, 255), 1: (0, 0, 0, 255), 256: (255, 255, 255, 255)}
data[0, 5:, 5:] = -100
d, m = colormap.apply_cmap(data, cm)
dd, mm = colormap.apply_discrete_cmap(data, cm)
numpy.testing.assert_array_equal(dd, d)
numpy.testing.assert_array_equal(mm, m)


def test_apply_intervals_cmap():
"""Should return valid data and mask."""
Expand Down

0 comments on commit d775bab

Please sign in to comment.