Skip to content

Commit

Permalink
Merge pull request #145 from descarteslabs/feat/derived-bands
Browse files Browse the repository at this point in the history
Endpoints for derived bands
  • Loading branch information
hrfuller authored Jul 18, 2017
2 parents f5c08a8 + 6ccdbc6 commit 65a4df8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
45 changes: 43 additions & 2 deletions descarteslabs/services/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ def sources(self):
r = self.session.get('/sources')
return r.json()

def bands(self, limit=None, products=None, offset=None, wavelength=None, resolution=None, tags=None):
"""Seach for imagery data bands that you have access to.
def bands(self, products=None, limit=None, offset=None, wavelength=None, resolution=None, tags=None):
"""Search for imagery data bands that you have access to.
:param list(str) products: A list of product(s) to return bands for.
:param int limit: Number of results to return.
:param int offset: Index to start at when returning results.
:param float wavelenth: A wavelength in nm e.g 700 that the band sensor must measure.
Expand All @@ -81,6 +82,26 @@ def bands(self, limit=None, products=None, offset=None, wavelength=None, resolut
r = self.session.post('/bands/search', json=kwargs)
return r.json()

def derived_bands(self, bands=None, limit=None, offset=None):
"""Search for predefined derived bands that you have access to.
:param list(str) bands: A list of source bands that must be part of
the derived band i.e ["nir"]
:param int limit: Number of results to return.
:param int offset: Index to start at when returning results.
"""
params = ['bands', 'limit', 'offset']

args = locals()
kwargs = {
param: args[param]
for param in params
if args[param] is not None
}

r = self.session.post('/bands/derived/search', json=kwargs)
return r.json()

def products(self, bands=None, limit=None, offset=None):
"""Search products that are available on the platform.
Expand Down Expand Up @@ -118,6 +139,17 @@ def available_products(self):

return r.json()

def translate(self, const_id):
"""Translate a deprecated constellation identifier
into a new-style product identifier.
:param string const_id: The constellation identifier to translate.
"""

r = self.session.get('/products/translate/{}'.format(const_id))

return r.json()

def summary(self, products=None, const_id=None, sat_id=None, date='acquired', part=None,
place=None, geom=None, start_time=None, end_time=None, cloud_fraction=None,
cloud_fraction_0=None, fill_fraction=None, pixels=None, params=None,
Expand Down Expand Up @@ -513,3 +545,12 @@ def get_band(self, band_id):
"""
r = self.session.get('/bands/%s' % band_id)
return r.json()

def get_derived_band(self, derived_band_id):
"""Get information about a single product.
:param str derived_band_id: Derived band identifier.
"""
r = self.session.get('/bands/derived/%s' % derived_band_id)
return r.json()
24 changes: 18 additions & 6 deletions descarteslabs/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def test_cloud_fraction(self):
for feature in r['features']:
self.assertEqual(feature['properties']['cloud_fraction'], 0.0)

def translate_to_product(self):
r = self.instance.translate('l8')
self.assertEqual(r['product'], 'landsat:LC08:PRE:TOAR')

def test_search_by_product(self):
r = self.instance.search(start_time='2016-07-06', end_time='2016-07-07', products=['landsat:LC08:PRE:TOAR'])
self.assertGreater(len(r['features']), 0)
Expand Down Expand Up @@ -178,11 +182,6 @@ def test_search_products(self):
r = self.instance.products(limit=1)
self.assertEqual(len(r), 1)

def test_search_bands(self):
r = self.instance.bands()
# no bands are publicly available.
self.assertEqual(len(r), 0)

def test_products_get(self):
product_id = 'landsat:LC08:PRE:TOAR'
r = self.instance.get_product(product_id)
Expand All @@ -191,10 +190,23 @@ def test_products_get(self):
def test_bands_get(self):
band_id = 'landsat:LC08:PRE:TOAR:red'
try:
self.instance.get_product(band_id)
band = self.instance.get_band(band_id)
self.assertEqual(band_id, band['id'])
except NotFoundError:
pass

def test_derived_bands_get(self):
band_id = 'derived:ndvi'
try:
d_band = self.instance.get_derived_band(band_id)
self.assertIn('bands', d_band)
except NotFoundError:
pass

def test_derived_bands_search(self):
bands = ['red', 'nir']
bands = self.instance.derived_bands(bands=bands)


if __name__ == '__main__':
unittest.main()

0 comments on commit 65a4df8

Please sign in to comment.