Skip to content

Commit

Permalink
feat: standardising 16bits TIFF TDE-628 (#333)
Browse files Browse the repository at this point in the history
* feat: standardising 16bits TIFF TDE-628

* fix: formatting fails

* refactor: improve argument passing to get_gdal_command()

* chore: remove useless empty line
  • Loading branch information
paulfouquet authored Feb 7, 2023
1 parent acabc09 commit 1a07683
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
17 changes: 17 additions & 0 deletions scripts/gdal/gdal_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ def get_gdal_band_offset(file: str, info: Optional[GdalInfo] = None) -> List[str
return ["-b", "1", "-b", "2", "-b", "3"] + alpha_band_info

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


def get_gdal_band_type(file: str, info: Optional[GdalInfo] = None) -> str:
"""Get the band type of the first band
Args:
file: file to check
info: optional precomputed gdalinfo
Returns:
band type
"""
if info is None:
info = gdal_info(file, False)

bands = info["bands"]
return bands[0]["type"]
18 changes: 17 additions & 1 deletion scripts/gdal/gdal_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,21 @@
"overview_quality=90",
]

# Arguments to convert TIFF from 16 bits to 8 bits
CONVERT_16BITS_TO_8BITS = [
"-ot",
"Byte",
"-scale",
# 16 bit --> 2^16 = 65536 values --> 0-65535
"0",
"65535",
# 8 bit --> 2^8 = 256 values --> 0-255
"0",
"255",
]


def get_gdal_command(preset: str) -> List[str]:
def get_gdal_command(preset: str, convert_from: Optional[str] = None) -> List[str]:
get_log().info("gdal_preset", preset=preset)
gdal_command: List[str] = ["gdal_translate"]

Expand All @@ -79,6 +92,9 @@ def get_gdal_command(preset: str) -> List[str]:

gdal_command.extend(WEBP_OVERVIEWS)

if convert_from == "UInt16":
gdal_command.extend(CONVERT_16BITS_TO_8BITS)

return gdal_command


Expand Down
13 changes: 12 additions & 1 deletion scripts/gdal/tests/gdal_bands_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from scripts.gdal.gdal_bands import get_gdal_band_offset
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


Expand Down Expand Up @@ -62,3 +62,14 @@ def test_gdal_default_rgb() -> None:
bands = get_gdal_band_offset("some_file.tiff", gdalinfo)

assert " ".join(bands) == "-b 1 -b 2 -b 3"


def test_get_band_type() -> None:
gdalinfo = fake_gdal_info()
add_band(gdalinfo, band_type="UInt16")
add_band(gdalinfo, band_type="UInt16")
add_band(gdalinfo, band_type="UInt16")

band_type = get_gdal_band_type("some_file.tiff", gdalinfo)

assert band_type == "UInt16"
14 changes: 12 additions & 2 deletions scripts/gdal/tests/gdalinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ def fake_gdal_info() -> GdalInfo:
return cast(GdalInfo, {})


def add_band(gdalinfo: GdalInfo, color_interpretation: Optional[str] = None, no_data_value: Optional[int] = None) -> None:
def add_band(
gdalinfo: GdalInfo,
color_interpretation: Optional[str] = None,
no_data_value: Optional[int] = None,
band_type: Optional[str] = None,
) -> None:
if gdalinfo.get("bands", None) is None:
gdalinfo["bands"] = []

gdalinfo["bands"].append(
cast(
GdalInfoBand,
{"band": len(gdalinfo["bands"]) + 1, "colorInterpretation": color_interpretation, "noDataValue": no_data_value},
{
"band": len(gdalinfo["bands"]) + 1,
"colorInterpretation": color_interpretation,
"noDataValue": no_data_value,
"type": band_type,
},
)
)
9 changes: 6 additions & 3 deletions scripts/standardising.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
from scripts.files.file_tiff import FileTiff
from scripts.files.files_helper import get_file_name_from_path, is_tiff, is_vrt
from scripts.files.fs import read, write
from scripts.gdal.gdal_bands import get_gdal_band_offset
from scripts.gdal.gdal_bands import get_gdal_band_offset, get_gdal_band_type
from scripts.gdal.gdal_helper import get_gdal_version, run_gdal
from scripts.gdal.gdal_preset import get_cutline_command, get_gdal_command
from scripts.gdal.gdalinfo import gdal_info
from scripts.logging.time_helper import time_in_ms


Expand Down Expand Up @@ -100,8 +101,10 @@ def standardising(file: str, preset: str, cutline: Optional[str]) -> FileTiff:
run_gdal(get_cutline_command(input_cutline_path), input_file=input_file, output_file=target_vrt)
input_file = target_vrt

command = get_gdal_command(preset)
command.extend(get_gdal_band_offset(input_file))
# gdalinfo to get band offset and band type
info = gdal_info(file, False)
command = get_gdal_command(preset, convert_from=get_gdal_band_type(input_file, info))
command.extend(get_gdal_band_offset(input_file, info))

run_gdal(command, input_file=input_file, output_file=standardized_file_path)

Expand Down

0 comments on commit 1a07683

Please sign in to comment.