Skip to content

Commit

Permalink
ENH: Add a BandType alias for any types that could be a band: a str…
Browse files Browse the repository at this point in the history
…ing, a `BandNames` or any of its children: Spectral, SAR, DEM or Cloud band names
  • Loading branch information
remi-braun committed Aug 13, 2024
1 parent d109c50 commit bbc8238
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 68 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.21.3 (2024-mm-dd)

- ENH: Add a `BandType` alias for any types tyhat could be a band: a string, a `BandNames` or any of its children: Spectral, SAR, DEM or Cloud band names
- FIX: Get better window name (if available) when writing bands on disk (in tmp folder)

## 0.21.2 (2024-07-30)
Expand Down
168 changes: 100 additions & 68 deletions eoreader/bands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
>>> NDVI
<function NDVI at 0x00000261F6FFA950>
"""
from typing import Any, Union

# flake8: noqa
from eoreader.bands.bands import Band, BandMap

Expand Down Expand Up @@ -212,16 +210,30 @@
"to_str",
]

from typing import Any, Union

from eoreader.exceptions import InvalidTypeError as _ite

BandType = Union[
str, SpectralBandNames, SarBandNames, CloudsBandNames, DemBandNames, BandNames
]
""" EOReader band type, either a string, a BandName or its children: Spectral, SAR, DEM or Cloud band names """


def is_spectral_band(band: Any) -> bool:
def is_spectral_band(band: BandType) -> bool:
"""
Returns True if is a spectral band (from :code:`SpectralBandNames`)
.. code-block:: python
Args:
band (BandType): Anything that could be an optical band
Returns:
bool: True if the band asked is an optical band
Examples:
>>> from eoreader.bands import *
>>> from eoreader.bands import NDVI, HH, GREEN, SLOPE, CLOUDS
>>>
>>> is_spectral_band(NDVI)
False
>>> is_spectral_band(HH)
Expand All @@ -233,12 +245,6 @@ def is_spectral_band(band: Any) -> bool:
>>> is_spectral_band(CLOUDS)
False
Args:
band (Any): Anything that could be an optical band
Returns:
bool: True if the band asked is an optical band
"""
is_valid = True
try:
Expand All @@ -248,41 +254,51 @@ def is_spectral_band(band: Any) -> bool:
return is_valid


def is_thermal_band(band: Any) -> bool:
def is_thermal_band(band: BandType) -> bool:
"""
Returns True if is a spectral and a thermal band (from :code:`SpectralBandNames`)
.. code-block:: python
Args:
band (BandType): Anything that could be an optical band
>>> from eoreader.bands import *
>>> is_spectral_band(NDVI)
Returns:
bool: True if the band asked is an optical band
Examples:
>>> from eoreader.bands import SWIR_1, NDVI, HH, GREEN, SLOPE, CLOUDS
>>>
>>> is_thermal_band(SWIR_1)
True
>>> is_thermal_band(NDVI)
False
>>> is_spectral_band(HH)
>>> is_thermal_band(HH)
False
>>> is_spectral_band(GREEN)
True
>>> is_spectral_band(SLOPE)
>>> is_thermal_band(GREEN)
False
>>> is_spectral_band(CLOUDS)
>>> is_thermal_band(SLOPE)
False
>>> is_thermal_band(CLOUDS)
False
Args:
band (Any): Anything that could be an optical band
Returns:
bool: True if the band asked is an optical band
"""
return is_spectral_band(band) and band in [TIR_1, TIR_2, F1, F2, S7]


def is_sar_band(band: Any) -> bool:
def is_sar_band(band: BandType) -> bool:
"""
Returns True if is a SAR band (from :code:`SarBandNames`)
.. code-block:: python
Args:
band (BandType): Anything that could be a SAR band
Returns:
bool: True if the band asked is a SAR band
>>> from eoreader.bands import *
Examples:
>>> from eoreader.bands import NDVI, HH, GREEN, SLOPE, CLOUDS
>>>
>>> is_sar_band(NDVI)
False
>>> is_sar_band(HH)
Expand All @@ -294,12 +310,6 @@ def is_sar_band(band: Any) -> bool:
>>> is_sar_band(CLOUDS)
False
Args:
band (Any): Anything that could be a SAR band
Returns:
bool: True if the band asked is a SAR band
"""
is_valid = True
try:
Expand All @@ -309,13 +319,20 @@ def is_sar_band(band: Any) -> bool:
return is_valid


def is_sat_band(band) -> bool:
def is_sat_band(BandType) -> bool:
"""
Returns True if is a band (from both :code:`SarBandNames` or :code:`SpectralBandNames`)
Returns True if is a satellite band (from both :code:`SarBandNames` or :code:`SpectralBandNames`)
Args:
band (BandType): Anything that could be a band
Returns:
bool: True if the band asked is a satellite band (Spectral or SAR)
.. code-block:: python
Examples:
>>> from eoreader.bands import *
>>> from eoreader.bands import NDVI, HH, GREEN, SLOPE, CLOUDS
>>>
>>> is_sat_band(NDVI)
False
>>> is_sat_band(HH)
Expand All @@ -327,23 +344,24 @@ def is_sat_band(band) -> bool:
>>> is_sat_band(CLOUDS)
False
Args:
band (Any): Anything that could be a band
Returns:
bool: True if the band asked is a band
"""
return is_sar_band(band) or is_spectral_band(band)


def is_clouds(clouds: Any) -> bool:
def is_clouds(clouds: BandType) -> bool:
"""
Returns True if we have a Clouds-related keyword
Returns True if is a cloud band (from :code:`CloudBandNames`)
Args:
clouds (BandType): Anything that could be a cloud band
Returns:
bool: True if the band asked is a cloud band
.. code-block:: python
Examples:
>>> from eoreader.bands import *
>>> from eoreader.bands import NDVI, HH, GREEN, SLOPE, CLOUDS
>>>
>>> is_clouds(NDVI)
False
>>> is_clouds(HH)
Expand All @@ -354,6 +372,7 @@ def is_clouds(clouds: Any) -> bool:
False
>>> is_clouds(CLOUDS)
True
"""
is_valid = True
try:
Expand All @@ -363,13 +382,20 @@ def is_clouds(clouds: Any) -> bool:
return is_valid


def is_dem(dem: Any) -> bool:
def is_dem(dem: BandType) -> bool:
"""
Returns True if we have a DEM-related keyword
Returns True if is a DEM band (from :code:`DemBandNames`)
.. code-block:: python
Args:
clouds (BandType): Anything that could be a DEM band
Returns:
bool: True if the band asked is a DEM band
>>> from eoreader.bands import *
Examples:
>>> from eoreader.bands import NDVI, HH, GREEN, SLOPE, CLOUDS
>>>
>>> is_dem(NDVI)
False
>>> is_dem(HH)
Expand All @@ -390,15 +416,24 @@ def is_dem(dem: Any) -> bool:


def to_band(
to_convert: Union[list, BandNames, str], as_list: bool = True
to_convert: Union[list[BandType], BandType], as_list: bool = True
) -> Union[list, BandNames]:
"""
Convert a string (or real value) to any alias, band or index.
You can pass the name or the value of the bands.
.. code-block:: python
Args:
to_convert (Union[list, BandNames, str]): Values to convert into band objects
as_list (bool): Return the result as a list
Returns:
list: Bands as BandNames
Examples:
>>> from eoreader.bands import RED, DEM, CLOUDS
>>>
>>> to_band(["NDVI", "GREEN", RED, "VH_DSPK", "SLOPE", DEM, "CLOUDS", CLOUDS])
[<function NDVI at 0x00000154DDB12488>,
<SpectralBandNames.GREEN: 'GREEN'>,
Expand All @@ -409,12 +444,6 @@ def to_band(
<ClassifBandNames.CLOUDS: 'CLOUDS'>,
<ClassifBandNames.CLOUDS: 'CLOUDS'>]
Args:
to_convert (Union[list, BandNames, str]): Values to convert into band objects
Returns:
list: converted values
"""
from sertit import types

Expand Down Expand Up @@ -474,23 +503,26 @@ def convert_to_band(tc) -> BandNames:


def to_str(
to_convert: Union[list, BandNames, str], as_list: bool = True
to_convert: Union[list[BandType], BandType], as_list: bool = True
) -> Union[list, str]:
"""
Convert a string (or real value) to any alias, band or index.
You can pass the name or the value of the bands.
.. code-block:: python
>>> to_str(["NDVI", "GREEN", RED, "VH_DSPK", "SLOPE", DEM, "CLOUDS", CLOUDS])
['NDVI', 'GREEN', 'RED', 'VH_DSPK', 'SLOPE', 'DEM', 'CLOUDS', 'CLOUDS']
Args:
to_convert (Union[list, BandNames, str]): Values to convert into str
as_list (bool): Return the result as a list
Returns:
list: str bands
list: Bands as strings
Examples:
>>> from eoreader.bands import RED, DEM, CLOUDS
>>>
>>> to_str(["NDVI", "GREEN", RED, "VH_DSPK", "SLOPE", DEM, "CLOUDS", CLOUDS])
['NDVI', 'GREEN', 'RED', 'VH_DSPK', 'SLOPE', 'DEM', 'CLOUDS', 'CLOUDS']
"""
from sertit import types

Expand Down

0 comments on commit bbc8238

Please sign in to comment.