Skip to content

Commit

Permalink
Round data to nearest integer before converting to 8bit (#250)
Browse files Browse the repository at this point in the history
* round data to nearest integer before converting to 8bit

* fix tests

* fix tests
  • Loading branch information
dionhaefner authored Feb 11, 2022
1 parent 04f1019 commit ce7958b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion terracotta/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __setitem__(self, key: Any,
def _compress_ma(arr: np.ma.MaskedArray, compression_level: int) -> CompressionTuple:
compressed_data = zlib.compress(arr.data, compression_level)
mask_to_int = np.packbits(arr.mask.astype(np.uint8))
compressed_mask = zlib.compress(mask_to_int, compression_level)
compressed_mask = zlib.compress(mask_to_int.data, compression_level)
out = (
compressed_data,
compressed_mask,
Expand Down
2 changes: 1 addition & 1 deletion terracotta/drivers/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def create(self, keys: Sequence[str], key_descriptions: Mapping[str, str] = None
binary_prefix=True, charset='utf8mb4'
)

with connection, connection.cursor() as cursor: # type: ignore
with connection, connection.cursor() as cursor:
cursor.execute(f'CREATE DATABASE {self._db_args.db}')

with self._connect(check=False):
Expand Down
1 change: 1 addition & 0 deletions terracotta/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def contrast_stretch(data: Array,
def to_uint8(data: Array, lower_bound: Number, upper_bound: Number) -> Array:
"""Re-scale an array to [1, 255] and cast to uint8 (0 is used for transparency)"""
rescaled = contrast_stretch(data, (lower_bound, upper_bound), (1, 255), clip=True)
rescaled = np.rint(rescaled)
return rescaled.astype(np.uint8)


Expand Down
14 changes: 14 additions & 0 deletions tests/handlers/test_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ def test_colormap_consistency(use_testdb, testdb, raster_file_xyz,
for val in values_to_test:
rgba = cmap[val]
assert np.all(img_data[tile_data == val] == rgba)


@pytest.mark.parametrize('stretch_range', [[0, 0.1], [20000, 30000], [-50000, 50000]])
def test_colormap_range(stretch_range):
"""Ensure that all colors from colormap file are present in returned data"""
from terracotta.cmaps import get_cmap
from terracotta.handlers import colormap
cmap_name = 'jet'
cmap_in = get_cmap(cmap_name)
num_values = cmap_in.shape[0]
cmap_out = colormap.colormap(colormap=cmap_name, stretch_range=stretch_range,
num_values=num_values)
cmap_out_arr = np.array([val['rgba'] for val in cmap_out], dtype=cmap_in.dtype)
np.testing.assert_array_equal(cmap_in, cmap_out_arr)
2 changes: 1 addition & 1 deletion tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_to_uint8():
data = np.array([-50, 0, 10, 255, 256, 1000])
np.testing.assert_array_equal(
image.to_uint8(data, -50, 1000),
[1, 13, 15, 74, 75, 255]
[1, 13, 16, 75, 75, 255]
)


Expand Down

0 comments on commit ce7958b

Please sign in to comment.