Skip to content

Commit

Permalink
EP-2151 Open-EO#117 Open-EO#125 fix DataCube.ndvi
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Mar 11, 2020
1 parent 6977064 commit 229a4c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
21 changes: 12 additions & 9 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from openeo.internal.graphbuilder import GraphBuilder
from openeo.imagecollection import ImageCollection, CollectionMetadata
from openeo.job import Job
from openeo.util import get_temporal_extent
from openeo.util import get_temporal_extent, dict_no_none

if hasattr(typing, 'TYPE_CHECKING') and typing.TYPE_CHECKING:
# Only import this for type hinting purposes. Runtime import causes circular dependency issues.
Expand Down Expand Up @@ -692,19 +692,22 @@ def count_time(self) -> 'ImageCollection':
"""
return self._reduce_time(reduce_function="count")

def ndvi(self, name="ndvi") -> 'ImageCollection':
def ndvi(self, nir: str = None, red: str = None, target_band: str = None) -> 'ImageCollection':
""" Normalized Difference Vegetation Index (NDVI)
:param name: Name of the newly created band
:param nir: name of NIR band
:param red: name of red band
:param target_band: (optional) name of the newly created band
:return: An ImageCollection instance
"""
process_id = 'ndvi'
args = {
'data': {'from_node': self.node_id},
'name': name
}
return self.graph_add_process(process_id, args)
return self.graph_add_process(
process_id='ndvi',
args=dict_no_none(
data={'from_node': self.builder.result_node},
nir=nir, red=red, target_band=target_band
)
)

@deprecated("use 'linear_scale_range' instead")
def stretch_colors(self, min, max) -> 'ImageCollection':
Expand Down
11 changes: 11 additions & 0 deletions openeo/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def date_to_rfc3339(d: Any) -> str:
raise NotImplementedError("TODO")


def dict_no_none(**kwargs):
"""
Helper to build a dict containing given key-value pairs where the value is not None.
"""
return {
k: v
for k, v in kwargs.items()
if v is not None
}


def first_not_none(*args):
"""Return first item from given arguments that is not None."""
for item in args:
Expand Down
20 changes: 20 additions & 0 deletions tests/rest/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ def test_merge_cubes(con100: Connection):
}


def test_ndvi_simple(con100: Connection):
ndvi = con100.load_collection("S2").ndvi()
assert sorted(ndvi.graph.keys()) == ["loadcollection1", "ndvi1"]
assert ndvi.graph["ndvi1"] == {
"process_id": "ndvi",
"arguments": {"data": {"from_node": "loadcollection1"}},
"result": True,
}


def test_ndvi_args(con100: Connection):
ndvi = con100.load_collection("S2").ndvi(nir="nirr", red="rred", target_band="ndvii")
assert sorted(ndvi.graph.keys()) == ["loadcollection1", "ndvi1"]
assert ndvi.graph["ndvi1"] == {
"process_id": "ndvi",
"arguments": {"data": {"from_node": "loadcollection1"}, "nir": "nirr", "red": "rred", "target_band": "ndvii"},
"result": True,
}


def test_viewing_service(con100: Connection, requests_mock):
def check_request(request):
assert request.json() == {
Expand Down
10 changes: 9 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@

import pytest

from openeo.util import first_not_none, get_temporal_extent, TimingLogger, ensure_list, ensure_dir
from openeo.util import first_not_none, get_temporal_extent, TimingLogger, ensure_list, ensure_dir, dict_no_none


def test_dict_no_none():
assert dict_no_none() == {}
assert dict_no_none(a=3) == {"a": 3}
assert dict_no_none(a=3, b=0, c="foo") == {"a": 3, "b": 0, "c": "foo"}
assert dict_no_none(a=3, b="", c="foo") == {"a": 3, "b": "", "c": "foo"}
assert dict_no_none(a=3, b=None, c="foo") == {"a": 3, "c": "foo"}


@pytest.mark.parametrize(['input', 'expected'], [
Expand Down

0 comments on commit 229a4c2

Please sign in to comment.