Example #1
0
def test_empty_mask():
    from shapely import geometry
    polygon = geometry.Polygon([[1.0, 1.0], [2.0, 1.0], [2.0, 1.0], [1.0,
                                                                     1.0]])

    client = ImageCollectionClient(node_id=None,
                                   builder=GraphBuilder(),
                                   session=None)

    with pytest.raises(ValueError, match=r"Mask .+ has an area of 0.0"):
        client.mask(polygon)
Example #2
0
    def setUp(self):
        builder = GraphBuilder()
        id = builder.process("get_collection", {'name': 'S1'})

        connection = MagicMock(spec=Connection)
        capabilities = MagicMock(spec=Capabilities)

        connection.capabilities.return_value = capabilities
        capabilities.version.return_value = "0.4.0"
        self.img = ImageCollectionClient(id, builder, connection)

        builder = GraphBuilder()
        mask_id = builder.process("get_collection", {'name': 'S1_Mask'})
        self.mask = ImageCollectionClient(mask_id, builder, connection)
Example #3
0
def test_metadata_load_collection_040(session040, requests_mock):
    requests_mock.get(API_URL + "/collections/SENTINEL2",
                      json={
                          "properties": {
                              "cube:dimensions": {
                                  "bands": {
                                      "type": "bands",
                                      "values": ["B2", "B3"]
                                  }
                              },
                              "eo:bands": [
                                  {
                                      "name": "B2",
                                      "common_name": "blue"
                                  },
                                  {
                                      "name": "B3",
                                      "common_name": "green"
                                  },
                              ]
                          }
                      })
    im = ImageCollectionClient.load_collection('SENTINEL2', session=session040)
    assert im.metadata.bands == [
        openeo.metadata.Band("B2", "blue", None),
        openeo.metadata.Band("B3", "green", None)
    ]
Example #4
0
    def load_collection(
        self,
        collection_id: str,
        spatial_extent: Optional[Dict[str, float]] = None,
        temporal_extent: Optional[List[Union[str, datetime.datetime,
                                             datetime.date]]] = None,
        bands: Optional[List[str]] = None,
        properties: Optional[Dict[str, Union[str, PGNode, Callable]]] = None
    ) -> DataCube:
        """
        Load a DataCube by collection id.

        :param collection_id: image collection identifier
        :param spatial_extent: limit data to specified bounding box or polygons
        :param temporal_extent: limit data to specified temporal interval
        :param bands: only add the specified bands
        :param properties: limit data by metadata property predicates
        :return: a datacube containing the requested data
        """
        if self._api_version.at_least("1.0.0"):
            return DataCube.load_collection(collection_id=collection_id,
                                            connection=self,
                                            spatial_extent=spatial_extent,
                                            temporal_extent=temporal_extent,
                                            bands=bands,
                                            properties=properties)
        else:
            return ImageCollectionClient.load_collection(
                collection_id=collection_id,
                session=self,
                spatial_extent=spatial_extent,
                temporal_extent=temporal_extent,
                bands=bands)
Example #5
0
def image_collection():
    builder = GraphBuilder()
    id = builder.process("get_collection", {'name': 'S1'})

    connection = MagicMock(spec=Connection)
    capabilities = MagicMock(spec=Capabilities)

    connection.capabilities.return_value = capabilities
    capabilities.version.return_value = "0.4.0"
    return ImageCollectionClient(id, builder, connection)
Example #6
0
def eodc_script(output_filename):
    """
    
    """

    backend_url = 'https://openeo.eodc.eu'

    session = openeo.connect(backend_url,
                             auth_type='basic',
                             auth_options={
                                 "username": os.environ["EODC_USERNAME"],
                                 "password": os.environ["EODC_PASSWORD"]
                             })
    minx = 11.279182434082033
    maxx = 11.406898498535158
    maxy = 46.522729291844286
    miny = 46.464349400461145
    epsg = "EPSG:4326"
    spatial_extent = {
        'west': minx,
        'east': maxx,
        'north': maxy,
        'south': miny,
        'crs': epsg
    }

    temporal_extent = ["2018-06-04T00:00:00.000Z", "2018-06-23T00:00:00.000Z"]

    spectral_extent = [8, 4, 2]

    s2a = ImageCollectionClient.load_collection(
        session=session,
        collection_id='s2a_prd_msil1c',
        temporal_extent=temporal_extent,
        spatial_extent=spatial_extent,
        bands=spectral_extent)
    s2b = s2a.load_collection(session=session,
                              collection_id='s2b_prd_msil1c',
                              temporal_extent=temporal_extent,
                              spatial_extent=spatial_extent,
                              bands=spectral_extent)
    s2_radiometry = s2a.merge(s2b)

    #s2_radiometry =
    B02 = s2_radiometry.band(2)
    B04 = s2_radiometry.band(1)
    B08 = s2_radiometry.band(0)

    evi_cube = (2.5 * (B08 - B04)) / ((B08 + 6.0 * B04 - 7.5 * B02) + 10000)

    min_evi = evi_cube.min_time()

    output = min_evi.download(output_filename, format="GTiff")
Example #7
0
    def load_collection(self, collection_id: str,
                        **kwargs) -> ImageCollectionClient:
        """
        Load an image collection by collection id

        see :py:meth:`openeo.rest.imagecollectionclient.ImageCollectionClient.load_collection`
        for available arguments.

        :param collection_id: image collection identifier (string)
        :return: ImageCollectionClient
        """
        return ImageCollectionClient.load_collection(
            collection_id=collection_id, session=self, **kwargs)
Example #8
0
    def load_disk_collection(self,
                             format: str,
                             glob_pattern: str,
                             options: dict = {}) -> ImageCollectionClient:
        """
        Loads image data from disk as an ImageCollection.

        :param format: the file format, e.g. 'GTiff'
        :param glob_pattern: a glob pattern that matches the files to load from disk
        :param options: options specific to the file format
        :return: the data as an ImageCollection
        """
        return ImageCollectionClient.load_disk_collection(
            self, format, glob_pattern, **options)
Example #9
0
    def load_collection(self, collection_id: str, **kwargs) -> Union[ImageCollectionClient, DataCube]:
        """
        Load an image collection by collection id

        see :py:meth:`openeo.rest.imagecollectionclient.ImageCollectionClient.load_collection`
        for available arguments.

        :param collection_id: image collection identifier (string)
        :return: ImageCollectionClient
        """
        if self._api_version.at_least("1.0.0"):
            return DataCube.load_collection(collection_id=collection_id, connection=self, **kwargs)
        else:
            return ImageCollectionClient.load_collection(collection_id=collection_id, session=self, **kwargs)
Example #10
0
    def load_disk_collection(self, format: str, glob_pattern: str, options: dict = {}) -> ImageCollectionClient:
        """
        Loads image data from disk as an ImageCollection.

        :param format: the file format, e.g. 'GTiff'
        :param glob_pattern: a glob pattern that matches the files to load from disk
        :param options: options specific to the file format
        :return: the data as an ImageCollection
        """

        if self._api_version.at_least("1.0.0"):
            return DataCube.load_disk_collection(self, format, glob_pattern, **options)
        else:
            return ImageCollectionClient.load_disk_collection(self, format, glob_pattern, **options)
Example #11
0
def eurac_script(input_data, output_filename):
    """
    
    """

    backend_url = 'https://openeo.eurac.edu'

    if input_data == 'L1C':
        collection_id = 'openEO_S2_32632_10m_L1C_D22'
    elif input_data == 'L2A':
        collection_id = 'openEO_S2_32632_10m_L2A_D22'

    session = openeo.connect(backend_url,
                             auth_type='basic',
                             auth_options={
                                 "username": os.environ["EURAC_USERNAME"],
                                 "password": os.environ["EURAC_PASSWORD"]
                             })

    minx = 11.279182434082033
    maxx = 11.406898498535158
    maxy = 46.522729291844286
    miny = 46.464349400461145
    epsg = "EPSG:4326"

    s2_radiometry = ImageCollectionClient.load_collection(
        session=session,
        collection_id=collection_id,
        temporal_extent=[
            "2018-06-04T00:00:00.000Z", "2018-06-23T00:00:00.000Z"
        ],
        spatial_extent={
            'west': minx,
            'east': maxx,
            'north': maxy,
            'south': miny,
            'crs': epsg
        },
        bands=["B08", "B04", "B02"])
    B02 = s2_radiometry.band(2)
    B04 = s2_radiometry.band(1)
    B08 = s2_radiometry.band(0)

    evi_cube = (2.5 * (B08 - B04)) / ((B08 + 6.0 * B04 - 7.5 * B02) + 10000)

    min_evi = evi_cube.min_time()

    output = min_evi.download(output_filename, format="GTiff")
Example #12
0
def vito_script(input_data, output_filename):
    """
    
    """

    backend_url = 'https://openeo.vito.be/openeo/0.4.2'

    if input_data == 'L1C':
        collection_id = 'SENTINEL2_L1C_SENTINELHUB'
    elif input_data == 'L2A':
        collection_id = 'SENTINEL2_L2A_SENTINELHUB'

    session = openeo.connect(backend_url)

    minx = 11.279182434082033
    maxx = 11.406898498535158
    maxy = 46.522729291844286
    miny = 46.464349400461145
    epsg = "EPSG:4326"
    spatial_extent = {
        'west': minx,
        'east': maxx,
        'north': maxy,
        'south': miny,
        'crs': epsg
    }

    temporal_extent = ["2018-06-04T00:00:00.000Z", "2018-06-22T00:00:00.000Z"]

    spectral_extent = ["B08", "B04", "B02"]

    s2_radiometry = ImageCollectionClient.load_collection(
        session=session,
        collection_id=collection_id,
        temporal_extent=temporal_extent,
        spatial_extent=spatial_extent)

    B02 = s2_radiometry.band(spectral_extent[2])
    B04 = s2_radiometry.band(spectral_extent[1])
    B08 = s2_radiometry.band(spectral_extent[0])

    evi_cube = (2.5 * (B08 - B04)) / ((B08 + 6.0 * B04 - 7.5 * B02) + 1.0)

    min_evi = evi_cube.min_time()

    output = min_evi.download(output_filename, format="GeoTiff")
Example #13
0
def sinergise_script(output_filename):
    """
    
    """

    backend_url = 'https://e3oind87n9.execute-api.eu-central-1.amazonaws.com/production/'
    #'https://openeo.sentinel-hub.com/production'
    collection_id = 'S2L1C'

    session = openeo.connect(backend_url)

    minx = 11.279182434082033
    maxx = 11.406898498535158
    maxy = 46.522729291844286
    miny = 46.464349400461145
    epsg = "EPSG:4326"
    spatial_extent = {
        'west': minx,
        'east': maxx,
        'north': maxy,
        'south': miny,
        'crs': epsg
    }

    temporal_extent = ["2018-06-04T00:00:00.000Z", "2018-06-23T00:00:00.000Z"]

    spectral_extent = ["B08", "B04", "B02"]

    s2_radiometry = ImageCollectionClient.load_collection(
        session=session,
        collection_id=collection_id,
        temporal_extent=temporal_extent,
        spatial_extent=spatial_extent)

    B02 = s2_radiometry.band(spectral_extent[2])
    B04 = s2_radiometry.band(spectral_extent[1])
    B08 = s2_radiometry.band(spectral_extent[0])

    evi_cube = (2.5 * (B08 - B04)) / ((B08 + 6.0 * B04 - 7.5 * B02) + 1.0)

    min_evi = evi_cube.min_time()

    min_evi.download(output_filename,
                     format="GTiff",
                     options={"datatype": "float32"})
Example #14
0
def mundialis_script(output_filename):
    """
    
    """

    backend_url = 'https://openeo.mundialis.de'

    collection_id = 'utm32n.openeo_bolzano.strds.openeo_bolzano_S2'

    session = openeo.connect(backend_url,
                             auth_type='basic',
                             auth_options={
                                 "username": os.environ["MUNDIALIS_USERNAME"],
                                 "password": os.environ["MUNDIALIS_PASSWORD"]
                             })

    minx = 11.279182434082033
    maxx = 11.406898498535158
    maxy = 46.522729291844286
    miny = 46.464349400461145
    epsg = "EPSG:4326"

    s2_radiometry = ImageCollectionClient.load_collection(
        session=session,
        collection_id=collection_id,
        temporal_extent=[
            "2018-06-04T00:00:00.000Z", "2018-06-23T00:00:00.000Z"
        ],
        spatial_extent={
            'west': minx,
            'east': maxx,
            'north': maxy,
            'south': miny,
            'crs': epsg
        })
    B02 = s2_radiometry.band("S2_2")
    B04 = s2_radiometry.band("S2_4")
    B08 = s2_radiometry.band("S2_8")

    evi_cube = (2.5 * (B08 - B04)) / ((B08 + 6.0 * B04 - 7.5 * B02) + 10000)

    min_evi = evi_cube.min_time()

    output = min_evi.download(output_filename, format="GTiff")
Example #15
0
class TestRasterCube(TestCase):

    def setUp(self):
        builder = GraphBuilder()
        id = builder.process("get_collection", {'name': 'S1'})

        connection = MagicMock(spec=Connection)
        capabilities = MagicMock(spec=Capabilities)

        connection.capabilities.return_value = capabilities
        capabilities.version.return_value = "0.4.0"
        self.img = ImageCollectionClient(id, builder, connection)

        builder = GraphBuilder()
        mask_id = builder.process("get_collection", {'name': 'S1_Mask'})
        self.mask = ImageCollectionClient(mask_id, builder, connection)

    def test_filter_bbox(self):
        im = self.img.filter_bbox(
            west=652000, east=672000, north=5161000, south=5181000, crs="EPSG:32632"
        )
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000, "east": 672000, "north": 5161000, "south": 5181000, "crs": "EPSG:32632"
        }

    def test_filter_bbox_base_height(self):
        im = self.img.filter_bbox(
            west=652000, east=672000, north=5161000, south=5181000, crs="EPSG:32632",
            base=100, height=200,
        )
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000, "east": 672000, "north": 5161000, "south": 5181000, "crs": "EPSG:32632",
            "base": 100, "height": 200,
        }

    def test_bbox_filter_nsew(self):
        im = self.img.bbox_filter(
            west=652000, east=672000, north=5161000, south=5181000, crs="EPSG:32632"
        )
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000, "east": 672000, "north": 5161000, "south": 5181000, "crs": "EPSG:32632"
        }

    def test_bbox_filter_tblr(self):
        im = self.img.bbox_filter(
            left=652000, right=672000, top=5161000, bottom=5181000, srs="EPSG:32632"
        )
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000, "east": 672000, "north": 5161000, "south": 5181000, "crs": "EPSG:32632"
        }

    def test_bbox_filter_nsew_zero(self):
        im = self.img.bbox_filter(
            west=0, east=0, north=0, south=0, crs="EPSG:32632"
        )
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 0, "east": 0, "north": 0, "south": 0, "crs": "EPSG:32632"
        }

    def test_min_time(self):
        img = self.img.min_time()
        graph = img.graph[img.node_id]
        assert graph["process_id"] == "reduce"
        assert graph["arguments"]["data"] == {'from_node': 'getcollection1'}
        assert graph["arguments"]["dimension"] == "temporal"
        callback, = graph["arguments"]["reducer"]["callback"].values()
        assert callback == {'arguments': {'data': {'from_argument': 'data'}}, 'process_id': 'min', 'result': True}

    def test_max_time(self):
        img = self.img.max_time()
        graph = img.graph[img.node_id]
        assert graph["process_id"] == "reduce"
        assert graph["arguments"]["data"] == {'from_node': 'getcollection1'}
        assert graph["arguments"]["dimension"] == "temporal"
        callback, = graph["arguments"]["reducer"]["callback"].values()
        assert callback == {'arguments': {'data': {'from_argument': 'data'}}, 'process_id': 'max', 'result': True}

    def test_reduce_time_udf(self):
        img = self.img.reduce_tiles_over_time("my custom code")
        graph = img.graph[img.node_id]
        self.assertEqual(graph["process_id"], "reduce")
        self.assertIn("data", graph['arguments'])

    def test_ndvi(self):
        img = self.img.ndvi()
        graph = img.graph[img.node_id]
        assert graph["process_id"] == "ndvi"
        assert graph["arguments"] == {
            'data': {'from_node': 'getcollection1'}, 'name': 'ndvi'
        }

    def test_mask(self):
        polygon = shapely.geometry.Polygon([[0, 0], [1.9, 0], [1.9, 1.9], [0, 1.9]])
        img = self.img.mask(polygon)
        graph = img.graph[img.node_id]
        assert graph["process_id"] == "mask"
        assert graph["arguments"] == {
            "data": {'from_node': 'getcollection1'},
            "mask": {
                'coordinates': (((0.0, 0.0), (1.9, 0.0), (1.9, 1.9), (0.0, 1.9), (0.0, 0.0)),),
                'crs': {'properties': {'name': 'EPSG:4326'}, 'type': 'name'},
                'type': 'Polygon'
            }
        }

    def test_mask_raster(self):
        img = self.img.mask(rastermask=self.mask, replacement=102)
        graph = img.graph[img.node_id]
        assert graph == {
            "process_id": "mask",
            "arguments": {
                "data": {
                    "from_node": "getcollection1"
                },
                "mask": {
                    "from_node": "getcollection2"
                },
                "replacement": 102
            },
            "result": False
        }

    def test_stretch_colors(self):
        img = self.img.stretch_colors(-1, 1)
        graph = img.graph[img.node_id]
        assert graph["process_id"] == "stretch_colors"
        assert graph["arguments"] == {
            'data': {'from_node': 'getcollection1'},
            'max': 1,
            'min': -1,
        }

    def test_apply_kernel(self):
        kernel = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
        img = self.img.apply_kernel(np.asarray(kernel), 3)
        graph = img.graph[img.node_id]
        assert graph["process_id"] == "apply_kernel"
        assert graph["arguments"] == {
            'data': {'from_node': 'getcollection1'},
            'factor': 3,
            'kernel': [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
        }

    def test_resample_spatial(self):
        new_imagery = self.img.resample_spatial(resolution=[2.0,3.0],projection=4578)

        graph = new_imagery.graph[new_imagery.node_id]

        self.assertEqual(graph["process_id"], "resample_spatial")
        self.assertIn("data", graph["arguments"])
        self.assertEqual(graph["arguments"]["resolution"], [2.0,3.0])
        self.assertEqual(graph["arguments"]["projection"], 4578)
Example #16
0
def test_filter_bands(image_collection: ImageCollectionClient):
    im = image_collection.filter_bands(["red", "nir"])
    graph = im.graph[im.node_id]
    assert graph['process_id'] == 'filter_bands'
    assert graph['arguments']['bands'] == ["red", "nir"]
Example #17
0
def test_filter_temporal_generic(image_collection: ImageCollectionClient, args, kwargs, extent):
    im = image_collection.filter_temporal(*args, **kwargs)
    graph = im.graph[im.node_id]
    assert graph['process_id'] == 'filter_temporal'
    assert graph['arguments']['extent'] == extent
Example #18
0
def test_filter_temporal_extent(image_collection: ImageCollectionClient):
    im = image_collection.filter_temporal(extent=("2016-01-01", "2016-03-10"))
    graph = im.graph[im.node_id]
    assert graph['process_id'] == 'filter_temporal'
    assert graph['arguments']['extent'] == ["2016-01-01", "2016-03-10"]
Example #19
0
maxx = 10.62927246093749
maxy = 46.84516443029275
miny = 46.72574176193996
epsg = "EPSG:4326"
spatial_extent = {'west':minx,'east':maxx,'north':maxy,'south':miny,'crs':epsg}
temporal_extent=["2018-05-01T00:00:00.000Z","2018-10-01T00:00:00.000Z"]

# To find the band names in GRASS GIS: `g.bands pattern="S2"`
spectral_extent = ["S2_8", "S2_4", "S2_2"]

# connect to mundialis backend
session = openeo.connect(backend_url).authenticate_basic(auth_id, password = auth_pwd)

# TODO change to s2_radiometry = session.load_collection( ...)
s2_radiometry = ImageCollectionClient.load_collection(
                    session=session,
                    collection_id=collection_id,
                    temporal_extent=temporal_extent,
                    spatial_extent=spatial_extent
                    )            
# g.bands pattern="S2"
B02 = s2_radiometry.band("S2_2")
B04 = s2_radiometry.band("S2_4")
B08 = s2_radiometry.band("S2_8")

evi_cube = (2.5 * (B08 - B04)) / ((B08 + 6.0 * B04 - 7.5 * B02) + 10000)

min_evi = evi_cube.min_time()

output = min_evi.download_results("min-evi_mundialis.tiff",format="GTiff")
Example #20
0
class TestRasterCube(TestCase):
    @patch.multiple(Connection, __abstractmethods__=set())
    def setUp(self):
        builder = GraphBuilder()
        id = builder.process("get_collection", {'name': 'S1'})

        connection = MagicMock(spec=Connection)
        capabilities = MagicMock(spec=Capabilities)

        connection.capabilities.return_value = capabilities
        capabilities.version.return_value = "0.4.0"
        self.imagery = ImageCollectionClient(id, builder, connection)

        builder = GraphBuilder()
        mask_id = builder.process("get_collection", {'name': 'S1_Mask'})
        self.mask = ImageCollectionClient(mask_id, builder, connection)

    def test_filter_bbox(self):
        im = self.imagery.filter_bbox(west=652000,
                                      east=672000,
                                      north=5161000,
                                      south=5181000,
                                      crs="EPSG:32632")
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000,
            "east": 672000,
            "north": 5161000,
            "south": 5181000,
            "crs": "EPSG:32632"
        }

    def test_filter_bbox_base_height(self):
        im = self.imagery.filter_bbox(
            west=652000,
            east=672000,
            north=5161000,
            south=5181000,
            crs="EPSG:32632",
            base=100,
            height=200,
        )
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000,
            "east": 672000,
            "north": 5161000,
            "south": 5181000,
            "crs": "EPSG:32632",
            "base": 100,
            "height": 200,
        }

    def test_bbox_filter_nsew(self):
        im = self.imagery.bbox_filter(west=652000,
                                      east=672000,
                                      north=5161000,
                                      south=5181000,
                                      crs="EPSG:32632")
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000,
            "east": 672000,
            "north": 5161000,
            "south": 5181000,
            "crs": "EPSG:32632"
        }

    def test_bbox_filter_tblr(self):
        im = self.imagery.bbox_filter(left=652000,
                                      right=672000,
                                      top=5161000,
                                      bottom=5181000,
                                      srs="EPSG:32632")
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 652000,
            "east": 672000,
            "north": 5161000,
            "south": 5181000,
            "crs": "EPSG:32632"
        }

    def test_bbox_filter_nsew_zero(self):
        im = self.imagery.bbox_filter(west=0,
                                      east=0,
                                      north=0,
                                      south=0,
                                      crs="EPSG:32632")
        graph = im.graph[im.node_id]
        assert graph["process_id"] == "filter_bbox"
        assert graph["arguments"]["extent"] == {
            "west": 0,
            "east": 0,
            "north": 0,
            "south": 0,
            "crs": "EPSG:32632"
        }

    def test_min_time(self):
        new_imagery = self.imagery.min_time()

        graph = new_imagery.graph[new_imagery.node_id]

        self.assertEqual(graph["process_id"], "reduce")
        self.assertIn("data", graph['arguments'])

    def test_max_time(self):
        new_imagery = self.imagery.max_time()

        graph = new_imagery.graph[new_imagery.node_id]

        self.assertEqual(graph["process_id"], "reduce")
        self.assertIn("data", graph['arguments'])

    def test_reduce_time_udf(self):
        new_imagery = self.imagery.reduce_tiles_over_time("my custom code")

        graph = new_imagery.graph[new_imagery.node_id]

        import json
        print(json.dumps(graph, indent=2))

        self.assertEqual(graph["process_id"], "reduce")
        self.assertIn("data", graph['arguments'])

    def test_ndvi(self):
        new_imagery = self.imagery.ndvi("B04", "B8A")

        graph = new_imagery.graph[new_imagery.node_id]

        self.assertEqual(graph["process_id"], "ndvi")
        self.assertIn("data", graph['arguments'])

    def test_mask(self):
        from shapely import geometry
        polygon = geometry.Polygon([[0, 0], [1.9, 0], [1.9, 1.9], [0, 1.9]])
        new_imagery = self.imagery.mask(polygon)

        graph = new_imagery.graph[new_imagery.node_id]

        self.assertEqual(graph["process_id"], "mask")
        self.assertEqual(
            graph["arguments"]["mask"], {
                'coordinates': (((0.0, 0.0), (1.9, 0.0), (1.9, 1.9),
                                 (0.0, 1.9), (0.0, 0.0)), ),
                'crs': {
                    'properties': {
                        'name': 'EPSG:4326'
                    },
                    'type': 'name'
                },
                'type':
                'Polygon'
            })

    def test_mask_raster(self):
        new_imagery = self.imagery.mask(rastermask=self.mask, replacement=102)

        graph = new_imagery.graph[new_imagery.node_id]
        import json
        print(json.dumps(new_imagery.graph, indent=4))

        expected_mask_node = {
            "process_id": "mask",
            "arguments": {
                "data": {
                    "from_node": "getcollection1"
                },
                "mask": {
                    "from_node": "getcollection2"
                },
                "replacement": 102
            },
            "result": False
        }

        self.assertDictEqual(expected_mask_node, graph)

    def test_strech_colors(self):
        new_imagery = self.imagery.stretch_colors(-1, 1)

        graph = new_imagery.graph[new_imagery.node_id]

        self.assertEqual(graph["process_id"], "stretch_colors")
        self.assertIn("data", graph['arguments'])
        self.assertEqual(graph["arguments"]["min"], -1)
        self.assertEqual(graph["arguments"]["max"], 1)

    def test_apply_kernel(self):

        kernel = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
        new_imagery = self.imagery.apply_kernel(np.asarray(kernel), 3)

        graph = new_imagery.graph[new_imagery.node_id]

        self.assertEqual(graph["process_id"], "apply_kernel")
        self.assertIn("data", graph["arguments"])
        self.assertEqual(graph["arguments"]["factor"], 3)
        self.assertEqual(graph["arguments"]["kernel"], kernel)
Example #21
0
def execute(
    out_dir,
    user,
    password,
    provider,
    driver_url,
    image_collection,
    all_bands,
    bands,
    band_format,
    bbox_string,
    temporal_extent,
):
    """
    Identification:
    Name -- OpenEO PoC
    Description -- Retrieve Sentinel 2 bands in GeoTIFF
    Version -- 1-hbo
    Author -- Alexander Jacob, Eurac Research, openEO
    Mission -- hackathon
    
    Inputs:
    user -- User -- 45/User String -- openeo
    password -- Password -- 45/User String -- XXXXXXXXX
    provider -- Provider -- 45/User String -- mundialis
    driver_url -- Driver URL -- 45/User String -- https://openeo.mundialis.de
    image_collection -- Image Collection -- 45/User String -- utm32n.openeo_bolzano.strds.openeo_bolzano_S2
    all_bands -- Bands -- 45/User String -- B02 B03 B04 B05 B06 B07 B8A B11 B12
    bands -- Bands -- 45/User String -- B03 B11
    band_format -- Band Format -- 45/User String -- GTiff
    bbox_string -- BBox -- 45/User String -- 10.446624755859375, 46.72574176193996, 10.629272460937498, 46.845164430292755
    temporal_extent -- Temporal Extent -- 44/DateRange -- 2018-05-01T00:00:00.000Z/2018-10-01T00:00:00.000Z
    
    Outputs:
    band_dir -- Band Directory -- 45/User String
    band_files -- Band Files -- 45/User String
    out_dir -- directory to store output
    
    Main Dependency:
    python-3

    Software Dependencies:
    openeo-0.4
    
    Processing Resources:
    ram -- 1
    disk -- 10
    cpu -- 1
    """
    all_bands = all_bands.split()
    bands = bands.split()
    temporal_extent = temporal_extent.split("/")
    west, south, east, north = list(
        map(lambda value: value.strip(), bbox_string.split(",")))
    bbox = {"west": west, "east": east, "south": south, "north": north}
    logger.info("Demo user: %s", user)
    logger.info("Provider: %s with driver URL %s", provider, driver_url)
    logger.info("Image Collection: %s", image_collection)
    logger.info("Bands: %s", bands)
    logger.info(
        "Using BBox in str format - raw input (west, south, east, north): %s",
        bbox_string)
    logger.info("BBox: %s", bbox)
    logger.info("Temporal extent in string format start/end: %s",
                temporal_extent)
    connection = openeo.connect(driver_url)
    connection.authenticate_basic(user, password)

    logger.info(
        "describe_collection('%s'):\n %s\n",
        image_collection,
        json.dumps(connection.describe_collection(image_collection), indent=2),
    )
    # TODO: change to con.load_collection()
    cube = ImageCollectionClient.load_collection(
        session=connection, collection_id=image_collection, bands=all_bands)
    cube = cube.filter_bbox(**bbox)
    cube = cube.filter_temporal(extent=temporal_extent)

    logger.info("cube.to_json: \n%s\n", cube.to_json())

    logger.info("File Format: %s", band_format)
    logger.info(
        "File Name Format: {provider}.{image_collection}.{west}.{south}.{east}.{north}."
        "{temporal_extent[0]}.{temporal_extent[1]}.B{band.zfill(2)}.{band_format}"
    )
    band_dir = out_dir
    logger.info("Downloading bands in %s", out_dir)
    band_files = []
    for band in bands:
        cube_band = cube.band(band)
        band_file = (
            f"{provider}.{image_collection}.{west}.{south}.{east}.{north}."
            f"{temporal_extent[0]}.{temporal_extent[1]}.B{band.zfill(2)}.{band_format.lower()}"
        )
        logger.info("Downloading band %s: %s", band, band_file)
        band_path = f"{out_dir}/{band_file}"
        band_files.append(band_file)
        logger.info("Starting download at %s", datetime.now())
        cube_band.download(band_path, format=band_format)
        logger.info("Download finished for band %s at %s", band,
                    datetime.now())

    logger.info("Downloads finished at %s", datetime.now())

    return {"band_dir": band_dir, "band_files": band_files}