Skip to content

Commit

Permalink
Open-EO#76: ImageCollectionClient.band(): support both band names and…
Browse files Browse the repository at this point in the history
… indices
  • Loading branch information
soxofaan committed Sep 5, 2019
1 parent fa34e88 commit c7edcb6
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions openeo/rest/imagecollectionclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ def band_filter(self, bands) -> 'ImageCollection':
}
return self.graph_add_process(process_id, args)

def band(self, band_name) -> 'ImageCollection':
def band(self, band: Union[str, int]) -> 'ImageCollection':
"""Filter the imagery by the given bands
:param bands: List of band names or single band name as a string.
:param band: band name or band index.
:return An ImageCollection instance
"""

process_id = 'reduce'
band_index = self._band_index(band_name)
band_index = self._band_index(band)

args = {
'data': {'from_node': self.node_id},
Expand All @@ -200,11 +200,20 @@ def band(self, band_name) -> 'ImageCollection':

return self.graph_add_process(process_id, args)

def _band_index(self,name:str):
try:
return self.bands.index(name)
except ValueError as e:
raise ValueError("Given band name: " + name + " not available in this image collection. Valid band names are: " + str(self.bands))
def _band_index(self, band: Union[str, int]):
"""
Helper to resolve/check a band name/index to a band index
:param band: band name or band index
:return int: band index
"""
bands = self.bands
if isinstance(band, str) and band in bands:
return bands.index(band)
elif isinstance(band, int) and 0 <= band < len(bands):
return band
else:
raise ValueError("Band {b!r} not available in collection. Valid names: {n!r}".format(b=band, n=bands))

def subtract(self, other:Union[ImageCollection,Union[int,float]]):
"""
Expand Down

0 comments on commit c7edcb6

Please sign in to comment.