Skip to content

Commit

Permalink
fix: standardised tiff shows white when using add alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfouquet committed Jul 16, 2023
1 parent b09c894 commit 7de6699
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
25 changes: 3 additions & 22 deletions scripts/gdal/gdal_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def get_gdal_command(preset: str, epsg: str) -> List[str]:
return gdal_command


def get_cutline_command(cutline: Optional[str]) -> List[str]:
def get_alpha_command(cutline: Optional[str]) -> List[str]:
"""Get a `gdalwarp` command to create a virtual file (`.vrt`) which has a cutline applied and alpha added.
Args:
Expand All @@ -148,33 +148,14 @@ def get_cutline_command(cutline: Optional[str]) -> List[str]:
return gdal_command


def get_build_vrt_command(files: List[str], output: str = "output.vrt", add_alpha: bool = False) -> List[str]:
gdal_command = ["gdalbuildvrt", "-hidenodata", "-strict"]
if add_alpha:
gdal_command.append("-addalpha")
def get_build_vrt_command(files: List[str], output: str = "output.vrt") -> List[str]:
gdal_command = ["gdalbuildvrt", "-strict"]
gdal_command.append(output)
gdal_command += files

return gdal_command


def get_alpha_command() -> List[str]:
"""Get a `gdalwarp` command to create a virtual file (.vrt) which has an alpha added.
Returns:
a list of arguments to run `gdalwarp`
"""

return [
"gdalwarp",
# Outputting a VRT makes things faster as its not recomputing everything
"-of",
"VRT",
# Ensure the target has a alpha channel
"-dstalpha",
]


def get_transform_srs_command(source_epsg: str, target_epsg: str) -> List[str]:
"""Get a `gdalwarp` command to transform the srs.
Expand Down
4 changes: 2 additions & 2 deletions scripts/gdal/tests/gdal_preset_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from scripts.gdal.gdal_preset import get_cutline_command, get_gdal_command
from scripts.gdal.gdal_preset import get_alpha_command, get_gdal_command


def test_preset_webp() -> None:
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_preset_dem_lerc() -> None:


def test_cutline_params() -> None:
gdal_command = get_cutline_command("cutline.fgb")
gdal_command = get_alpha_command("cutline.fgb")

assert "-cutline" in gdal_command
assert "cutline.fgb" in gdal_command
Expand Down
16 changes: 9 additions & 7 deletions scripts/standardising.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from scripts.files.fs import exists, read, write
from scripts.gdal.gdal_bands import get_gdal_band_offset
from scripts.gdal.gdal_helper import get_gdal_version, run_gdal
from scripts.gdal.gdal_preset import get_build_vrt_command, get_cutline_command, get_gdal_command, get_transform_srs_command
from scripts.gdal.gdal_preset import get_alpha_command, get_build_vrt_command, get_gdal_command, get_transform_srs_command
from scripts.gdal.gdalinfo import gdal_info
from scripts.logging.time_helper import time_in_ms
from scripts.tile.tile_index import Bounds, get_bounds_from_name
Expand Down Expand Up @@ -107,10 +107,10 @@ def download_tiffs(files: List[str], target: str) -> List[str]:
return downloaded_files


def create_vrt(source_tiffs: List[str], target_path: str, add_alpha: bool = False) -> str:
def create_vrt(source_tiffs: List[str], target_path: str) -> str:
# Create the `vrt` file
vrt_path = os.path.join(target_path, "source.vrt")
run_gdal(command=get_build_vrt_command(files=source_tiffs, output=vrt_path, add_alpha=add_alpha))
run_gdal(command=get_build_vrt_command(files=source_tiffs, output=vrt_path))
return vrt_path


Expand Down Expand Up @@ -166,7 +166,9 @@ def standardising(
vrt_add_alpha = False

# Start from base VRT
input_file = create_vrt(source_tiffs, tmp_path, add_alpha=vrt_add_alpha)
input_file = create_vrt(source_tiffs, tmp_path)
target_vrt = "target.vrt"
input_cutline_path = ""

# Apply cutline
if cutline:
Expand All @@ -178,9 +180,9 @@ def standardising(
# Ensure the input cutline is a easy spot for GDAL to read
write(input_cutline_path, read(cutline))

target_vrt = os.path.join(tmp_path, "cutline.vrt")
# TODO: test a cutline with a VRT file
run_gdal(get_cutline_command(input_cutline_path), input_file=input_file, output_file=target_vrt)
# TODO: test a cutline with a VRT file
if cutline or vrt_add_alpha:
run_gdal(get_alpha_command(input_cutline_path), input_file=input_file, output_file=target_vrt)
input_file = target_vrt

# Reproject tiff if needed
Expand Down

0 comments on commit 7de6699

Please sign in to comment.