Skip to content

Commit

Permalink
Move "get_band_index" logic to CollectionMetadata for better reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Nov 4, 2019
1 parent df367c0 commit c0f47ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
19 changes: 19 additions & 0 deletions openeo/imagecollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ def band_names(self) -> List[str]:
def band_common_names(self) -> List[str]:
return [b.common_name for b in self.bands]

def get_band_index(self, band: Union[int, str]) -> int:
"""
Resolve a band name/index to band index
:param band: band name, common name or index
:return int: band index
"""
band_names = self.band_names
if isinstance(band, int) and 0 <= band < len(band_names):
return band
elif isinstance(band, str):
common_names = self.band_common_names
# First try common names if possible
if band in common_names:
return common_names.index(band)
if band in band_names:
return band_names.index(band)
raise ValueError("Band {b!r} not available in collection. Valid names: {n!r}".format(b=band, n=band_names))



class ImageCollection(ABC):
"""Class representing Processes. """
Expand Down
21 changes: 1 addition & 20 deletions openeo/rest/imagecollectionclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def band(self, band: Union[str, int]) -> 'ImageCollection':
"""

process_id = 'reduce'
band_index = self._band_index(band)
band_index = self.metadata.get_band_index(band)

args = {
'data': {'from_node': self.node_id},
Expand All @@ -162,25 +162,6 @@ def band(self, band: Union[str, int]) -> 'ImageCollection':

return self.graph_add_process(process_id, args)

def _band_index(self, band: Union[str, int]):
"""
Helper to resolve/check a band name/index to a band index
:param band: band name, band common name or band index
:return int: band index
"""
band_names = self.metadata.band_names
if isinstance(band, int) and 0 <= band < len(band_names):
return band
elif isinstance(band, str):
common_names = self.metadata.band_common_names
# First try common names if possible
if band in common_names:
return common_names.index(band)
if band in band_names:
return band_names.index(band)
raise ValueError("Band {b!r} not available in collection. Valid names: {n!r}".format(b=band, n=band_names))

def resample_spatial(self, resolution: Union[Union[int, float], Sequence[Union[int, float]]],
projection: Union[int, str] = None, method: str = 'near', align: str = 'lower-left'):
return self.graph_add_process('resample_spatial', {
Expand Down

0 comments on commit c0f47ca

Please sign in to comment.