generated from linz/template-python-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: apply a cutline if supplied (#240)
* refactor: add basic types to gdalinfo response * feat: detect the band count in imagery * wip: attempt to apply a cutline * refactor: correct mypy typing * refactor: fixup lint/isort * feat: download tiff/cutlines if we have multiple s3 locations * feat: basic tests for presets * refactor: fix import sorting * refactor add more tests * refactor: apply formatter * docs: add docs * refactor: fix lint
- Loading branch information
Showing
14 changed files
with
415 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import List, Optional | ||
|
||
from linz_logger import get_log | ||
|
||
from scripts.gdal.gdalinfo import GdalInfo, GdalInfoBand, gdal_info | ||
|
||
|
||
def find_band(bands: List[GdalInfoBand], color: str) -> Optional[GdalInfoBand]: | ||
"""Look for a specific colorInterperation inside of a gdalinfo band output | ||
Args: | ||
bands: Bands to search | ||
color: Color to search, eg Red, Green, Gray | ||
Returns: | ||
Band if it exists, None otherwise | ||
""" | ||
for band in bands: | ||
if band["colorInterpretation"] == color: | ||
return band | ||
return None | ||
|
||
|
||
def get_gdal_band_offset(file: str, info: Optional[GdalInfo] = None) -> List[str]: | ||
"""Get the banding parameters for a gdal_translate command | ||
Args: | ||
file: file to check | ||
info: optional precomputed gdalinfo | ||
Returns: | ||
list of band mappings eg "-b 1 -b 1 -b 1" | ||
""" | ||
if info is None: | ||
info = gdal_info(file, False) | ||
|
||
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"])]) | ||
|
||
# Grey scale imagery, set R,G and B to just the grey_band | ||
grey_band = find_band(bands, "Gray") | ||
if grey_band: | ||
grey_band_index = str(grey_band["band"]) | ||
return ["-b", grey_band_index, "-b", grey_band_index, "-b", grey_band_index] + alpha_band_info | ||
|
||
band_red = find_band(bands, "Red") | ||
band_green = find_band(bands, "Green") | ||
band_blue = find_band(bands, "Blue") | ||
|
||
if band_red is None or band_green is None or band_blue is None: | ||
get_log().warn( | ||
"gdal_info_bands_failed", band_red=band_red is None, band_green=band_green is None, band_blue=band_blue is None | ||
) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.