From dab4565ca058a857865f96bf88eff83bb10b63cb Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Mon, 19 Dec 2022 16:36:04 +1300 Subject: [PATCH] fix: force a alpha band on all imagery if a cutline is present (#264) * fix: force a alpha band on all imagery if a cutline is present * refactor: fixup lint * refactor: missing import --- scripts/gdal/gdal_preset.py | 15 +++++++++------ scripts/standardising.py | 8 ++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/scripts/gdal/gdal_preset.py b/scripts/gdal/gdal_preset.py index ce396943e..e2dc95d18 100644 --- a/scripts/gdal/gdal_preset.py +++ b/scripts/gdal/gdal_preset.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Optional from linz_logger import get_log @@ -82,19 +82,22 @@ def get_gdal_command(preset: str) -> List[str]: return gdal_command -def get_cutline_command(cutline: str) -> List[str]: +def get_cutline_command(cutline: Optional[str]) -> List[str]: """ Get a "gdalwarp" command to create a virtual file (.vrt) which has a cutline applied and alpha added """ - return [ + gdal_command = [ "gdalwarp", # Outputting a VRT makes things faster as its not recomputing everything "-of", "VRT", - # Apply the cutline - "-cutline", - cutline, # Ensure the target has a alpha channel "-dstalpha", ] + + # Apply the cutline + if cutline: + gdal_command += ["-cutline", cutline] + + return gdal_command diff --git a/scripts/standardising.py b/scripts/standardising.py index 932b84002..00e5eca1d 100644 --- a/scripts/standardising.py +++ b/scripts/standardising.py @@ -99,10 +99,10 @@ def standardising(file: str, preset: str, cutline: Optional[str]) -> FileTiff: optimized_cutline = optimize_cutline(input_file, input_cutline_path) get_log().info("optimize_cutline", optimized_cutline=optimized_cutline, path=file) - if optimized_cutline: - target_vrt = os.path.join(tmp_path, str(ulid.ULID()) + ".vrt") - run_gdal(get_cutline_command(optimized_cutline), input_file=input_file, output_file=target_vrt) - input_file = target_vrt + + target_vrt = os.path.join(tmp_path, str(ulid.ULID()) + ".vrt") + run_gdal(get_cutline_command(optimized_cutline), 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))