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

Patch/use discrete colormap #661

Merged
merged 2 commits into from
Dec 11, 2023
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# 6.2.8 (2023-12-11)

* apply `discrete` colormap when the provided colormap does not have 256 values

# 6.2.7 (2023-11-29)

* Adjusting dataset latitude for WarpedVRT parameters calculation when EPSG:4326 dataset latitudes overflows EPSG:3857 min/max latitude (https://github.com/cogeotiff/rio-tiler/pull/660)
Expand Down
4 changes: 2 additions & 2 deletions rio_tiler/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ def apply_cmap(data: numpy.ndarray, colormap: ColorMapType) -> DataMaskType:
if isinstance(colormap, Sequence):
return apply_intervals_cmap(data, colormap)

# if colormap has more than 256 values OR its `max` key >= 256 we can't use
# if colormap has less or more than 256 values OR its `max` key >= 256 we can't use
# 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 or min(colormap) < 0:
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
31 changes: 31 additions & 0 deletions tests/test_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def test_apply_discrete_cmap():
mask[2:5, 2:5] = 255
mask[5:, 5:] = 255
numpy.testing.assert_array_equal(m, mask)
dd, mm = colormap.apply_cmap(data, cm)
numpy.testing.assert_array_equal(dd, d)
numpy.testing.assert_array_equal(mm, m)

data = data.astype("uint16")
d, m = colormap.apply_discrete_cmap(data, cm)
Expand Down Expand Up @@ -251,3 +254,31 @@ def test_parse_color_bad():

with pytest.raises(InvalidColorFormat):
colormap.parse_color([0, 0, 0, 0, 0])


def test_discrete_float():
"""test for titiler issue 738."""
cm = {
0: (0, 255, 255, 255),
1: (83, 151, 145, 255),
2: (87, 194, 23, 255),
3: (93, 69, 255, 255),
4: (98, 217, 137, 255),
5: (140, 255, 41, 255),
6: (150, 110, 255, 255),
7: (179, 207, 100, 255),
8: (214, 130, 156, 255),
9: (232, 170, 108, 255),
10: (255, 225, 128, 255),
11: (255, 184, 180, 255),
12: (255, 255, 140, 255),
13: (255, 180, 196, 255),
14: (255, 0, 0, 255),
15: (255, 218, 218, 255),
}

data = numpy.round(numpy.random.random_sample((1, 256, 256)) * 15)
d, m = colormap.apply_cmap(data.copy(), cm)
dd, mm = colormap.apply_discrete_cmap(data.copy(), cm)
assert d.dtype == numpy.uint8
assert m.dtype == numpy.uint8
Loading