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

apply discrete cmap when needed #325

Merged
merged 1 commit into from
Dec 22, 2020
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
3 changes: 3 additions & 0 deletions rio_tiler/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def apply_cmap(
if data.shape[0] > 1:
raise InvalidFormat("Source data must be 1 band")

if len(colormap) > 256 or max(colormap) > 256:
Copy link
Member

Choose a reason for hiding this comment

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

Why do you only check for > 256 and not != 256?

Copy link
Member Author

@vincentsarago vincentsarago Dec 23, 2020

Choose a reason for hiding this comment

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

@kylebarron because make_lut should work for cmap that have less than 256 value or a max smaller than 256
https://github.com/cogeotiff/rio-tiler/pull/325/files#diff-1c0d3ee98487c3ad9e96a708cf2b17d8dba80980c2b0bd6faaca3688543bfd41R66-R68

just realizing that it should be max(colormap) >= 256 🤦

e.g

cmap = {1: [255,255,255,255], 255: [1,1,1, 255]}
make_lut(cmap)
array([[  0,   0,   0,   0],
       [255, 255, 255, 255],
       [  0,   0,   0,   0],
       ...,
       [  0,   0,   0,   0],
       [  0,   0,   0,   0],
       [  1,   1,   1, 255]], dtype=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.

return apply_discrete_cmap(data, colormap)

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

Expand Down
15 changes: 15 additions & 0 deletions tests/test_cmap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""tests rio_tiler colormaps"""

from copy import deepcopy

import numpy
import pytest

Expand Down Expand Up @@ -141,3 +143,16 @@ def test_apply_discrete_cmap():
mask[2:5, 2:5] = 255
mask[5:, 5:] = 255
numpy.testing.assert_array_equal(m, mask)

cm = {1: [0, 0, 0, 255], 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 = 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)