|
11 | 11 | from affine import dumpsw
|
12 | 12 | from harmony_service_lib.message import Message as HarmonyMessage
|
13 | 13 | from harmony_service_lib.message import Source as HarmonySource
|
14 |
| -from matplotlib.cm import ScalarMappable |
15 | 14 | from matplotlib.colors import Normalize
|
16 | 15 | from numpy import ndarray, uint8
|
17 | 16 | from osgeo_utils.auxiliary.color_palette import ColorPalette
|
18 | 17 | from PIL import Image
|
19 | 18 | from rasterio.io import DatasetReader
|
20 |
| -from rasterio.plot import reshape_as_image, reshape_as_raster |
21 | 19 | from rasterio.warp import Resampling, reproject
|
22 | 20 | from rioxarray import open_rasterio
|
23 | 21 | from xarray import DataArray
|
|
34 | 32 | get_color_palette,
|
35 | 33 | greyscale_colormap,
|
36 | 34 | palette_from_remote_colortable,
|
37 |
| - remove_alpha, |
38 | 35 | )
|
39 | 36 | from hybig.exceptions import HyBIGError
|
40 | 37 | from hybig.sizes import (
|
@@ -289,31 +286,15 @@ def convert_singleband_to_raster(
|
289 | 286 | data_array: DataArray,
|
290 | 287 | color_palette: ColorPalette | None = None,
|
291 | 288 | ) -> 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. |
293 | 290 |
|
294 |
| - Use a palette if provided otherwise return a greyscale image. |
| 291 | + Uses a palette if provided otherwise returns a greyscale image. |
295 | 292 | """
|
296 | 293 | if color_palette is None:
|
297 | 294 | return scale_grey_1band(data_array)
|
298 | 295 | return scale_paletted_1band(data_array, color_palette)
|
299 | 296 |
|
300 | 297 |
|
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 |
| - |
317 | 298 | def scale_grey_1band(data_array: DataArray) -> tuple[ndarray, ColorMap]:
|
318 | 299 | """Normalize input array and return scaled data with greyscale ColorMap."""
|
319 | 300 | band = data_array[0, :, :]
|
@@ -364,93 +345,13 @@ def scale_paletted_1band(
|
364 | 345 | return np.expand_dims(scaled_band.data, 0), color_map
|
365 | 346 |
|
366 | 347 |
|
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 |
| - |
406 | 348 | def image_driver(mime: str) -> str:
|
407 | 349 | """Return requested rasterio driver for output image."""
|
408 | 350 | if re.search('jpeg', mime, re.I):
|
409 | 351 | return 'JPEG'
|
410 | 352 | return 'PNG'
|
411 | 353 |
|
412 | 354 |
|
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 |
| - |
454 | 355 | def get_color_map_from_image(image: Image) -> dict:
|
455 | 356 | """Get a writable color map
|
456 | 357 |
|
|
0 commit comments