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

feat: handle standardising paletted tiffs #547

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 25 additions & 12 deletions scripts/gdal/gdal_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def find_band(bands: List[GdalInfoBand], color: str) -> Optional[GdalInfoBand]:
return None


# pylint: disable-msg=too-many-return-statements
def get_gdal_band_offset(file: str, info: Optional[GdalInfo] = None, preset: Optional[str] = None) -> List[str]:
"""Get the banding parameters for a `gdal_translate` command.

Expand All @@ -36,19 +37,31 @@ def get_gdal_band_offset(file: str, info: Optional[GdalInfo] = None, preset: Opt

bands = info["bands"]

alpha_band = find_band(bands, "Alpha")
alpha_band_info: List[str] = []
if alpha_band:
alpha_band_info.extend(["-b", str(alpha_band["band"])])
band_alpha_arg: List[str] = []
if band_alpha := find_band(bands, "Alpha"):
band_alpha_arg.extend(["-b", str(band_alpha["band"])])

grey_band = find_band(bands, "Gray")
if grey_band:
grey_band_index = str(grey_band["band"])
if band_grey := find_band(bands, "Gray"):
band_grey_index = str(band_grey["band"])
if preset == "dem_lerc":
# return single band if DEM/DSM
return ["-b", grey_band_index]
return ["-b", band_grey_index]
# Grey scale imagery, set R,G and B to just the grey_band
return ["-b", grey_band_index, "-b", grey_band_index, "-b", grey_band_index] + alpha_band_info
return ["-b", band_grey_index, "-b", band_grey_index, "-b", band_grey_index] + band_alpha_arg

if band_palette := find_band(bands, "Palette"):
if colour_table := band_palette["colorTable"]:
palette_channels = len(colour_table["entries"][0])
if palette_channels == 4:
return ["-expand", "rgba"]
if palette_channels == 3:
return ["-expand", "rgb"]
get_log().error(
ajacombs marked this conversation as resolved.
Show resolved Hide resolved
"unknown_palette_band_type",
palette_channels=palette_channels,
first_entry=colour_table["entries"][0],
)
get_log().error("palette_band_missing_colorTable")

band_red = find_band(bands, "Red")
band_green = find_band(bands, "Green")
Expand All @@ -61,12 +74,12 @@ def get_gdal_band_offset(file: str, info: Optional[GdalInfo] = None, preset: Opt

# Not enough bands for RGB assume it is grey scale
if len(bands) < 3:
return ["-b", "1", "-b", "1", "-b", "1"] + alpha_band_info
return ["-b", "1", "-b", "1", "-b", "1"] + band_alpha_arg

# Could be RGB assume it is RGB
return ["-b", "1", "-b", "2", "-b", "3"] + alpha_band_info
return ["-b", "1", "-b", "2", "-b", "3"] + band_alpha_arg

return ["-b", str(band_red["band"]), "-b", str(band_green["band"]), "-b", str(band_blue["band"])] + alpha_band_info
return ["-b", str(band_red["band"]), "-b", str(band_green["band"]), "-b", str(band_blue["band"])] + band_alpha_arg


def get_gdal_band_type(file: str, info: Optional[GdalInfo] = None) -> str:
Expand Down
9 changes: 8 additions & 1 deletion scripts/gdal/gdalinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
from scripts.tile.tile_index import Point


class GdalInfoBandColorTable(TypedDict):
palette: str
ajacombs marked this conversation as resolved.
Show resolved Hide resolved
count: int
entries: List[List[int]]


class GdalInfoBand(TypedDict):
band: int
"""band offset, starting at 1
Expand All @@ -17,9 +23,10 @@ class GdalInfoBand(TypedDict):
colorInterpretation: str
"""Color
Examples:
"Red", "Green", "Blue", "Alpha", "Gray"
"Red", "Green", "Blue", "Alpha", "Gray", "Palette"
"""
noDataValue: Optional[int]
colorTable: Optional[GdalInfoBandColorTable]


class GdalInfo(TypedDict):
Expand Down
20 changes: 19 additions & 1 deletion scripts/gdal/tests/gdal_bands_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from scripts.gdal.gdal_bands import get_gdal_band_offset, get_gdal_band_type
from scripts.gdal.tests.gdalinfo import add_band, fake_gdal_info
from scripts.gdal.tests.gdalinfo import add_band, add_palette_band, fake_gdal_info


def test_gdal_grey_bands() -> None:
Expand Down Expand Up @@ -53,6 +53,24 @@ def test_gdal_rgb_bands_detection() -> None:
assert " ".join(bands) == "-b 1 -b 2 -b 3"


def test_gdal_rgba_palette_detection() -> None:
gdalinfo = fake_gdal_info()
add_palette_band(gdalinfo, colour_table_entries=[[x, x, x, 255] for x in reversed(range(256))])

bands = get_gdal_band_offset("some_file.tiff", gdalinfo)

assert " ".join(bands) == "-expand rgba"


def test_gdal_rgb_palette_detection() -> None:
gdalinfo = fake_gdal_info()
add_palette_band(gdalinfo, colour_table_entries=[[x, x, x] for x in reversed(range(256))])

bands = get_gdal_band_offset("some_file.tiff", gdalinfo)

assert " ".join(bands) == "-expand rgb"


def test_gdal_default_grey_scale() -> None:
gdalinfo = fake_gdal_info()
add_band(gdalinfo, color_interpretation="Pallette")
Expand Down
19 changes: 18 additions & 1 deletion scripts/gdal/tests/gdalinfo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, cast
from typing import List, Optional, cast

from scripts.gdal.gdalinfo import GdalInfo, GdalInfoBand

Expand Down Expand Up @@ -27,3 +27,20 @@ def add_band(
},
)
)


def add_palette_band(gdalinfo: GdalInfo, colour_table_entries: List[List[int]], no_data_value: Optional[int] = None) -> None:
if gdalinfo.get("bands", None) is None:
gdalinfo["bands"] = []

gdalinfo["bands"].append(
cast(
GdalInfoBand,
{
"band": len(gdalinfo["bands"]) + 1,
"colorInterpretation": "Palette",
"noDataValue": no_data_value,
"colorTable": {"palette": "RGB", "count": len(colour_table_entries), "entries": colour_table_entries},
},
)
)