Skip to content

Commit 0491fd8

Browse files
committed
GITC-7208: Fixed additional issues with implementation, HyBIG now respects ndv when read from input colormap
1 parent 867b2f8 commit 0491fd8

File tree

3 files changed

+43
-99
lines changed

3 files changed

+43
-99
lines changed

hybig/browse.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
from affine import dumpsw
1212
from harmony_service_lib.message import Message as HarmonyMessage
1313
from harmony_service_lib.message import Source as HarmonySource
14-
from matplotlib.cm import ScalarMappable
1514
from matplotlib.colors import Normalize
1615
from numpy import ndarray, uint8
1716
from osgeo_utils.auxiliary.color_palette import ColorPalette
1817
from PIL import Image
1918
from rasterio.io import DatasetReader
20-
from rasterio.plot import reshape_as_image, reshape_as_raster
2119
from rasterio.warp import Resampling, reproject
2220
from rioxarray import open_rasterio
2321
from xarray import DataArray
@@ -310,7 +308,7 @@ def scale_grey_1band(data_array: DataArray) -> tuple[ndarray, ColorMap]:
310308

311309
grey_colormap = greyscale_colormap()
312310
raster_data = np.expand_dims(np.round(normalized_data).data, 0)
313-
return raster_data, grey_colormap
311+
return np.array(raster_data, dtype='uint8'), grey_colormap
314312

315313

316314
def scale_paletted_1band(
@@ -344,7 +342,8 @@ def scale_paletted_1band(
344342
scaled_band[np.isnan(band)] = len(colors) - 1
345343

346344
color_map = colormap_from_colors(colors)
347-
return np.array(np.expand_dims(scaled_band.data, 0), dtype="uint8"), color_map
345+
raster_data = np.expand_dims(scaled_band.data, 0)
346+
return np.array(raster_data, dtype='uint8'), color_map
348347

349348

350349
def image_driver(mime: str) -> str:

hybig/color_utility.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ def get_color_palette(
9090
return get_remote_palette_from_source(source)
9191
except HyBIGNoColorInformation:
9292
try:
93-
return convert_colormap_to_palette(dataset.colormap(1))
93+
ds_cmap = dataset.colormap(1)
94+
# very defensive since this function is not documented in rasterio
95+
ndv_tuple: tuple[float, ...] = dataset.get_nodatavals()
96+
if ndv_tuple is not None and len(ndv_tuple) > 0:
97+
# this service only supports one ndv, so just use the first one (usually the only one)
98+
ds_cmap['nv'] = ds_cmap[ndv_tuple[0]]
99+
ds_cmap.pop(ndv_tuple[0]) # then remove the value associated with the ndv key
100+
return convert_colormap_to_palette(ds_cmap)
94101
except ValueError:
95102
return None
96103

tests/unit/test_browse.py

Lines changed: 32 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ def test_create_browse_imagery_with_single_band_raster(self):
132132
)
133133
message = HarmonyMessage({'format': {'mime': 'JPEG'}})
134134

135+
mock_logger = MagicMock(spec=Logger)
136+
135137
with rasterio_test_file(
136138
raster_data=two_dimensional_raster,
137139
height=two_dimensional_raster.shape[1],
@@ -142,7 +144,7 @@ def test_create_browse_imagery_with_single_band_raster(self):
142144
HyBIGError, 'incorrect number of bands for image: 2'
143145
):
144146
create_browse_imagery(
145-
message, test_tif_filename, HarmonySource({}), None, None
147+
message, test_tif_filename, HarmonySource({}), None, mock_logger
146148
)
147149

148150
@patch('hybig.browse.reproject')
@@ -172,28 +174,10 @@ def test_create_browse_imagery_with_mocks(
172174
expected_raster = np.array(
173175
[
174176
[
175-
[0, 104, 198, 255],
176-
[0, 104, 198, 255],
177-
[0, 104, 198, 255],
178-
[0, 104, 198, 255],
179-
],
180-
[
181-
[0, 104, 198, 255],
182-
[0, 104, 198, 255],
183-
[0, 104, 198, 255],
184-
[0, 104, 198, 255],
185-
],
186-
[
187-
[0, 104, 198, 255],
188-
[0, 104, 198, 255],
189-
[0, 104, 198, 255],
190-
[0, 104, 198, 255],
191-
],
192-
[
193-
[255, 255, 255, 255],
194-
[255, 255, 255, 255],
195-
[255, 255, 255, 255],
196-
[255, 255, 255, 255],
177+
[0, 85, 169, 254],
178+
[0, 85, 169, 254],
179+
[0, 85, 169, 254],
180+
[0, 85, 169, 254],
197181
],
198182
],
199183
dtype='uint8',
@@ -225,7 +209,8 @@ def test_create_browse_imagery_with_mocks(
225209
target_transform = Affine(90.0, 0.0, -180.0, 0.0, -45.0, 90.0)
226210
dest = np.zeros((da_mock.rio.height, da_mock.rio.width), dtype='uint8')
227211

228-
self.assertEqual(reproject_mock.call_count, 3)
212+
# since we are no longer de-palettizing, we only have to reproject a single band
213+
self.assertEqual(reproject_mock.call_count, 1)
229214

230215
expected_calls = [
231216
call(
@@ -235,27 +220,7 @@ def test_create_browse_imagery_with_mocks(
235220
src_crs=da_mock.rio.crs,
236221
dst_transform=target_transform,
237222
dst_crs=CRS.from_string('EPSG:4326'),
238-
dst_nodata=0,
239-
resampling=Resampling.nearest,
240-
),
241-
call(
242-
source=expected_raster[1, :, :],
243-
destination=dest,
244-
src_transform=file_transform,
245-
src_crs=da_mock.rio.crs,
246-
dst_transform=target_transform,
247-
dst_crs=CRS.from_string('EPSG:4326'),
248-
dst_nodata=0,
249-
resampling=Resampling.nearest,
250-
),
251-
call(
252-
source=expected_raster[2, :, :],
253-
destination=dest,
254-
src_transform=file_transform,
255-
src_crs=da_mock.rio.crs,
256-
dst_transform=target_transform,
257-
dst_crs=CRS.from_string('EPSG:4326'),
258-
dst_nodata=0,
223+
dst_nodata=255, # NODATA_IDX
259224
resampling=Resampling.nearest,
260225
),
261226
]
@@ -306,41 +271,23 @@ def test_create_browse_imagery_with_mocks(
306271
)
307272

308273
def test_convert_singleband_to_raster_without_colortable(self):
309-
"""Tests convert_gray_1band_to_raster."""
274+
"""Tests scale_grey_1band."""
310275
return_data = np.copy(self.data).astype('float64')
311276
return_data[0][1] = np.nan
312277
ds = DataArray(return_data).expand_dims('band')
313278

314279
expected_raster = np.array(
315280
[
316281
[
317-
[0, 0, 198, 255],
318-
[0, 104, 198, 255],
319-
[0, 104, 198, 255],
320-
[0, 104, 198, 255],
321-
],
322-
[
323-
[0, 0, 198, 255],
324-
[0, 104, 198, 255],
325-
[0, 104, 198, 255],
326-
[0, 104, 198, 255],
327-
],
328-
[
329-
[0, 0, 198, 255],
330-
[0, 104, 198, 255],
331-
[0, 104, 198, 255],
332-
[0, 104, 198, 255],
333-
],
334-
[
335-
[255, 0, 255, 255],
336-
[255, 255, 255, 255],
337-
[255, 255, 255, 255],
338-
[255, 255, 255, 255],
282+
[0, 255, 169, 254],
283+
[0, 85, 169, 254],
284+
[0, 85, 169, 254],
285+
[0, 85, 169, 254],
339286
],
340287
],
341288
dtype='uint8',
342289
)
343-
actual_raster = convert_singleband_to_raster(ds, None)
290+
actual_raster, _ = convert_singleband_to_raster(ds, None)
344291
assert_array_equal(expected_raster, actual_raster, strict=True)
345292

346293
def test_convert_singleband_to_raster_with_colormap(self):
@@ -368,7 +315,7 @@ def test_convert_singleband_to_raster_with_colormap(self):
368315
image_palette = convert_colormap_to_palette(self.colormap)
369316
actual_raster, actual_palette = convert_singleband_to_raster(ds, image_palette)
370317
assert_array_equal(expected_raster, actual_raster, strict=True)
371-
assert_equal(expected_palette, actual_palette, strict=True)
318+
assert_equal(expected_palette, actual_palette)
372319

373320

374321
def test_convert_singleband_to_raster_with_colormap_and_bad_data(self):
@@ -380,39 +327,29 @@ def test_convert_singleband_to_raster_with_colormap_and_bad_data(self):
380327
# Read the image down: red, yellow, green, blue
381328
expected_raster = np.array(
382329
[
383-
[ # red
384-
[nv_color[0], 255, 0, 0],
385-
[255, 255, 0, 0],
386-
[255, 255, 0, 0],
387-
[255, 255, 0, 0],
388-
],
389-
[ # green
390-
[nv_color[1], 255, 255, 0],
391-
[0, 255, 255, 0],
392-
[0, 255, 255, 0],
393-
[0, 255, 255, 0],
394-
],
395-
[ # blue
396-
[nv_color[2], 0, 0, 255],
397-
[0, 0, 0, 255],
398-
[0, 0, 0, 255],
399-
[0, 0, 0, 255],
400-
],
401-
[ # alpha
402-
[nv_color[3], 255, 255, 255],
403-
[255, 255, 255, 255],
404-
[255, 255, 255, 255],
405-
[255, 255, 255, 255],
330+
[ # singleband paletted
331+
[4, 1, 2, 3],
332+
[0, 1, 2, 3],
333+
[0, 1, 2, 3],
334+
[0, 1, 2, 3],
406335
],
407336
],
408337
dtype='uint8',
409338
)
339+
expected_palette = {
340+
0: (255, 0, 0, 255), # red
341+
1: (255, 255, 0, 255), # yellow
342+
2: (0, 255, 0, 255), # green
343+
3: (0, 0, 255, 255), # blue
344+
4: (10, 20, 30, 40), # nv
345+
}
410346

411347
colormap = {**self.colormap, 'nv': nv_color}
412348

413349
image_palette = convert_colormap_to_palette(colormap)
414-
actual_raster = convert_singleband_to_raster(ds, image_palette)
350+
actual_raster, actual_palette = convert_singleband_to_raster(ds, image_palette)
415351
assert_array_equal(expected_raster, actual_raster, strict=True)
352+
assert_equal(expected_palette, actual_palette)
416353

417354
def test_convert_uint16_3_multiband_to_raster(self):
418355
"""Test that uint16 input scales the output."""
@@ -680,6 +617,7 @@ def test_get_color_map_from_image(self):
680617
def test_get_color_palette_map_exists_source_does_not(self):
681618
ds = Mock(DatasetReader)
682619
ds.colormap.return_value = self.colormap
620+
ds.get_nodatavals.return_value = ()
683621

684622
lines = [
685623
'100 255 0 0 255',

0 commit comments

Comments
 (0)