Skip to content

Commit 5c1de96

Browse files
committed
DAS-2280: removes rasterization and quantization methods.
These might be useful to add back some day. But aren't needed anymore.
1 parent b2555a8 commit 5c1de96

File tree

1 file changed

+2
-101
lines changed

1 file changed

+2
-101
lines changed

hybig/browse.py

Lines changed: 2 additions & 101 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
@@ -34,7 +32,6 @@
3432
get_color_palette,
3533
greyscale_colormap,
3634
palette_from_remote_colortable,
37-
remove_alpha,
3835
)
3936
from hybig.exceptions import HyBIGError
4037
from hybig.sizes import (
@@ -289,31 +286,15 @@ def convert_singleband_to_raster(
289286
data_array: DataArray,
290287
color_palette: ColorPalette | None = None,
291288
) -> tuple[ndarray, ColorMap]:
292-
"""Convert input dataset to a 4 band raster image.
289+
"""Convert input dataset to a 1-band raster image with colormap.
293290
294-
Use a palette if provided otherwise return a greyscale image.
291+
Uses a palette if provided otherwise returns a greyscale image.
295292
"""
296293
if color_palette is None:
297294
return scale_grey_1band(data_array)
298295
return scale_paletted_1band(data_array, color_palette)
299296

300297

301-
def convert_gray_1band_to_raster(data_array: DataArray) -> ndarray:
302-
"""Convert a 1-band raster without a color association."""
303-
band = data_array[0, :, :]
304-
cmap = matplotlib.colormaps['Greys_r']
305-
cmap.set_bad(NODATA_RGBA)
306-
norm = Normalize(vmin=np.nanmin(band), vmax=np.nanmax(band))
307-
scalar_map = ScalarMappable(cmap=cmap, norm=norm)
308-
309-
rgba_image = np.zeros((*band.shape, 4), dtype='uint8')
310-
for row_no in range(band.shape[0]):
311-
rgba_image_slice = scalar_map.to_rgba(band[row_no, :], bytes=True)
312-
rgba_image[row_no, :, :] = rgba_image_slice
313-
314-
return reshape_as_raster(rgba_image)
315-
316-
317298
def scale_grey_1band(data_array: DataArray) -> tuple[ndarray, ColorMap]:
318299
"""Normalize input array and return scaled data with greyscale ColorMap."""
319300
band = data_array[0, :, :]
@@ -364,93 +345,13 @@ def scale_paletted_1band(
364345
return np.expand_dims(scaled_band.data, 0), color_map
365346

366347

367-
def convert_paletted_1band_to_raster(
368-
data_array: DataArray, palette: ColorPalette
369-
) -> ndarray:
370-
"""Convert a 1 band image with palette into a rgba raster image."""
371-
band = data_array[0, :, :]
372-
levels = list(palette.pal.keys())
373-
colors = [
374-
palette.color_to_color_entry(value, with_alpha=True)
375-
for value in palette.pal.values()
376-
]
377-
scaled_colors = [
378-
(r / 255.0, g / 255.0, b / 255.0, a / 255.0) for r, g, b, a in colors
379-
]
380-
381-
cmap, norm = matplotlib.colors.from_levels_and_colors(
382-
levels, scaled_colors, extend='max'
383-
)
384-
385-
# handle palette no data value
386-
if palette.ndv is not None:
387-
nodata_colors = palette.color_to_color_entry(palette.ndv, with_alpha=True)
388-
cmap.set_bad(
389-
(
390-
nodata_colors[0] / 255.0,
391-
nodata_colors[1] / 255.0,
392-
nodata_colors[2] / 255.0,
393-
nodata_colors[3] / 255.0,
394-
)
395-
)
396-
397-
scalar_map = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
398-
rgba_image = np.zeros((*band.shape, 4), dtype='uint8')
399-
for row_no in range(band.shape[0]):
400-
rgba_image[row_no, :, :] = scalar_map.to_rgba(
401-
np.ma.masked_invalid(band[row_no, :]), bytes=True
402-
)
403-
return reshape_as_raster(rgba_image)
404-
405-
406348
def image_driver(mime: str) -> str:
407349
"""Return requested rasterio driver for output image."""
408350
if re.search('jpeg', mime, re.I):
409351
return 'JPEG'
410352
return 'PNG'
411353

412354

413-
def palettize_raster(raster: ndarray) -> tuple[ndarray, dict]:
414-
"""Convert an RGB or RGBA image into a 1band image and palette.
415-
416-
Converts a 3 or 4 band np raster into a PIL image.
417-
Quantizes the image into a 1band raster with palette
418-
419-
Transparency is handled by first removing the Alpha layer and creating
420-
quantized raster from just the RGB layers. Next the Alpha layer values are
421-
treated as either transparent or opaque and any transparent values are
422-
written to the final raster as 254 and add the mapped RGBA value to the
423-
color palette.
424-
"""
425-
# reserves index 255 for transparent and off grid fill values
426-
# 0 to 254
427-
max_colors = 255
428-
rgb_raster, alpha = remove_alpha(raster)
429-
430-
multiband_image = Image.fromarray(reshape_as_image(rgb_raster))
431-
quantized_image = multiband_image.quantize(colors=max_colors)
432-
433-
color_map = get_color_map_from_image(quantized_image)
434-
435-
quantized_array, color_map = add_alpha(alpha, np.array(quantized_image), color_map)
436-
437-
one_band_raster = np.expand_dims(quantized_array, 0)
438-
return one_band_raster, color_map
439-
440-
441-
def add_alpha(
442-
alpha: ndarray | None, quantized_array: ndarray, color_map: dict
443-
) -> tuple[ndarray, dict]:
444-
"""If the input data had alpha values, manually set the quantized_image
445-
index to the transparent index in those places.
446-
"""
447-
if alpha is not None and np.any(alpha != OPAQUE):
448-
# Set any alpha to the transparent index value
449-
quantized_array = np.where(alpha != OPAQUE, NODATA_IDX, quantized_array)
450-
color_map[NODATA_IDX] = NODATA_RGBA
451-
return quantized_array, color_map
452-
453-
454355
def get_color_map_from_image(image: Image) -> dict:
455356
"""Get a writable color map
456357

0 commit comments

Comments
 (0)