Skip to content

Commit

Permalink
Issue Open-EO#72 drop pre-0.4 support for loading an image(collection)
Browse files Browse the repository at this point in the history
also related to Open-EO#76 Open-EO#77
  • Loading branch information
soxofaan committed Sep 16, 2019
1 parent 2a32c03 commit 17440df
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 126 deletions.
11 changes: 0 additions & 11 deletions openeo/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,6 @@ def imagecollection(self, image_collection_id:str) -> ImageCollection:
"""
pass

@abstractmethod
def image(self, image_product_id) -> 'ImageCollection':
"""
Get imagery by id.
DEPRECATED
:param image_collection_id: String image collection identifier
:return: collection: RestImagery the imagery with the id
"""

@abstractmethod
def download(self, graph, outputfile, format_options):
"""
Expand Down
34 changes: 17 additions & 17 deletions openeo/rest/imagecollectionclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Dict, Union

from shapely.geometry import Polygon, MultiPolygon, mapping
from deprecated import deprecated

from openeo.connection import Connection
from openeo.graphbuilder import GraphBuilder
Expand All @@ -28,7 +29,7 @@ def _api_version(self):
return self.session.capabilities().api_version_check

@classmethod
def create_collection(
def load_collection(
cls, collection_id: str, session: Connection = None,
spatial_extent: Union[Dict, None] = None,
temporal_extent: Union[List, None] = None,
Expand All @@ -45,25 +46,24 @@ def create_collection(
:return:
"""
# TODO: rename function to load_collection for better similarity with corresponding process id?
assert session.capabilities().api_version_check.at_least('0.4.0')
builder = GraphBuilder()
process_id = 'load_collection'
arguments = {
'id': collection_id,
'spatial_extent': spatial_extent,
'temporal_extent': temporal_extent,
}
if bands:
arguments['bands'] = bands
node_id = builder.process(process_id, arguments)
return cls(node_id, builder, session)

if session.capabilities().api_version_check.at_least('0.4.0'):
process_id = 'load_collection'
arguments = {
'id': collection_id,
'spatial_extent': spatial_extent,
'temporal_extent': temporal_extent,
}
if bands:
arguments['bands'] = bands
else:
process_id = 'get_collection'
arguments = {
'name': collection_id
}
@classmethod
@deprecated("use load_collection instead")
def create_collection(cls, *args, **kwargs):
return cls.load_collection(*args, **kwargs)

id = builder.process(process_id, arguments)
return ImageCollectionClient(id, builder, session)

def _filter_temporal(self, start: str, end: str) -> 'ImageCollection':
return self.graph_add_process(
Expand Down
39 changes: 4 additions & 35 deletions openeo/rest/rest_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,44 +234,13 @@ def imagecollection(self, image_collection_id) -> 'ImageCollection':
"""
Get imagecollection by id.
:param image_collection_id: String image collection identifier
:return: collection: RestImageCollection the imagecollection with the id
:return: collection: ImageCollectionClient the imagecollection with the id
"""
if self._api_version.at_least('0.4.0'):
return self._image_040(image_collection_id)
else:
from .imagery import RestImagery
collection = RestImagery({'name': image_collection_id, 'process_id': 'get_collection'}, self)

self.fetch_metadata(image_collection_id, collection)
return collection

def _image_040(self, image_product_id) -> 'ImageCollection':
"""
Get imagery by id.
:param image_collection_id: String image collection identifier
:return: collection: RestImagery the imagery with the id
"""
#new implementation: https://github.com/Open-EO/openeo-api/issues/160
assert self._api_version.at_least('0.4.0')
# TODO avoid local imports
from .imagecollectionclient import ImageCollectionClient
image = ImageCollectionClient.create_collection(image_product_id, self)
self.fetch_metadata(image_product_id, image)
return image

def image(self, image_product_id) -> 'ImageCollection':
"""
Get imagery by id.
DEPRECATED
:param image_collection_id: String image collection identifier
:return: collection: RestImagery the imagery with the id
"""
# TODO avoid local imports
from .rest_processes import RESTProcesses

image = RESTProcesses( self)

self.fetch_metadata(image_product_id, image)
image = ImageCollectionClient.load_collection(image_collection_id, self)
self.fetch_metadata(image_collection_id, image)
return image

def fetch_metadata(self, image_product_id, image_collection):
Expand Down
86 changes: 23 additions & 63 deletions tests/test_bandmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,67 +46,14 @@ def test_evi(self,m):
expected_graph = load_json_resource('data/evi_graph.json')
self.assertDictEqual(actual_graph,expected_graph)

def test_ndvi(self, m):
#configuration phase: define username, endpoint, parameters?
session = openeo.connect("http://localhost:8000/api")
session.post = MagicMock()
session.download = MagicMock()

m.get("http://localhost:8000/api/", json={"version": "0.3.1"})
m.get("http://localhost:8000/api/collections", json={"collections":[{"product_id": "sentinel2_subset"}]})
m.get("http://localhost:8000/api/collections/SENTINEL2_RADIOMETRY_10M", json={"product_id": "sentinel2_subset",
"bands": [{'band_id': 'B0'}, {'band_id': 'B1'},
{'band_id': 'B2'}, {'band_id': 'B3'}],
})

#discovery phase: find available data
#basically user needs to find available data on a website anyway?
#like the imagecollection ID on: https://earthengine.google.com/datasets/

#access multiband 4D (x/y/time/band) coverage
s2_radio = session.imagecollection("SENTINEL2_RADIOMETRY_10M")

#how to find out which bands I need?
#are band id's supposed to be consistent across endpoints? Is that possible?

#define a computation to perform
#combinebands to REST: udf_type:apply_pixel, lang: Python
bandFunction = lambda band1, band2, band3: band1 + band2
ndvi_coverage = s2_radio.apply_pixel([s2_radio.bands[0], s2_radio.bands[1], s2_radio.bands[2]], bandFunction)

#materialize result in the shape of a geotiff
#REST: WCS call
ndvi_coverage.download("out.geotiff")

#get result as timeseries for a single point
#How to define a point? Ideally it should also have the CRS?
ndvi_coverage.timeseries(4, 51)

import base64
import cloudpickle
expected_function = str(base64.b64encode(cloudpickle.dumps(bandFunction)),"UTF-8")
expected_graph = {
'process_graph': {
'process_id': 'apply_pixel',
'imagery':{
'name': 'SENTINEL2_RADIOMETRY_10M',
'process_id': 'get_collection'
},
'bands': ['B0', 'B1', 'B2'],
'function': expected_function

}
}
session.post.assert_called_once_with("/timeseries/point?x=4&y=51&srs=EPSG:4326",expected_graph)
session.download.assert_called_once()

def test_ndvi_udf(self, m):
#configuration phase: define username, endpoint, parameters?
session = openeo.connect("http://localhost:8000/api")
session.post = MagicMock()
session.download = MagicMock()

m.get("http://localhost:8000/api/", json={"version": "0.3.1"})
m.get("http://localhost:8000/api/", json={"version": "0.4.1"})
m.get("http://localhost:8000/api/collections", json={"collections": [{"product_id": "sentinel2_subset"}]})
m.get("http://localhost:8000/api/collections/SENTINEL2_RADIOMETRY_10M", json={"product_id": "sentinel2_subset",
"bands": [{'band_id': 'B0'},
Expand All @@ -130,21 +77,34 @@ def test_ndvi_udf(self, m):

#materialize result in the shape of a geotiff
#REST: WCS call
ndvi_coverage.download("out.geotiff")
ndvi_coverage.download("out.geotiff", format="GTIFF")

#get result as timeseries for a single point
#How to define a point? Ideally it should also have the CRS?
ndvi_coverage.timeseries(4, 51)

expected_graph = {'process_graph': {'process_id': 'apply_tiles',
'imagery': {'name': 'SENTINEL2_RADIOMETRY_10M', 'process_id': 'get_collection'},
'code': {'language': 'python',
'source': 'def myfunction(tile): print(tile)'
}
expected_graph = {
'process_graph': {
'loadcollection1': {
'result': False, 'process_id': 'load_collection',
'arguments': {'temporal_extent': None, 'id': 'SENTINEL2_RADIOMETRY_10M', 'spatial_extent': None}},
'reduce1': {
'result': True, 'process_id': 'reduce',
'arguments': {
'reducer': {'callback': {
'udf': {'result': True, 'process_id': 'run_udf',
'arguments': {'version': 'latest',
'udf': 'def myfunction(tile): print(tile)',
'runtime': 'Python',
'data': {'from_argument': 'data'}}}}},
'dimension': 'spectral_bands', 'binary': False,
'data': {'from_node': 'loadcollection1'}
}
}
}
}

}
}
session.post.assert_called_once_with("/timeseries/point?x=4&y=51&srs=EPSG:4326",expected_graph)
session.post.assert_called_once_with("/timeseries/point?x=4&y=51&srs=EPSG:4326", expected_graph)
session.download.assert_called_once()

def test_ndvi_udf_0_4_0(self, m):
Expand Down

0 comments on commit 17440df

Please sign in to comment.