File size: 3,674 Bytes
4fcc331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
""" Main file for extractions and pre-processing of data through OpenEO
"""

from enum import Enum
from typing import Callable

import openeo

from openeo_gfmap import BackendContext
from openeo_gfmap.spatial import SpatialContext
from openeo_gfmap.temporal import TemporalContext


class FetchType(Enum):
    """Enumerates the different types of extraction. There are three types of
    enumerations.

    * TILE: Tile based extractions, getting the data for a dense part. The
    output of such fetching process in a dense DataCube.
    * POINT: Point based extractions. From a datasets of polygons, gets sparse
    extractions and performs spatial aggregation on the selected polygons. The
    output of such fetching process is a VectorCube, that can be used to get
    a pandas.DataFrame
    * POLYGON: Patch based extractions, returning a VectorCube of sparsed
    patches. This can be retrieved as multiple NetCDF files from one job.
    """

    TILE = "tile"
    POINT = "point"
    POLYGON = "polygon"


class CollectionFetcher:
    """Base class to fetch a particular collection.

    Parameters
    ----------
    backend_context: BackendContext
        Information about the backend in use, useful in certain cases.
    bands: list
        List of band names to load from that collection.
    collection_fetch: Callable
        Function defining how to fetch a collection for a specific backend,
        the function accepts the following parameters: connection,
        spatial extent, temporal extent, bands and additional parameters.
    collection_preprocessing: Callable
        Function defining how to harmonize the data of a collection in a
        backend. For example, this function could rename the bands as they
        can be different for every backend/collection (SENTINEL2_L2A or
        SENTINEL2_L2A_SENTINELHUB). Accepts the following parameters:
        datacube (of pre-fetched collection) and additional parameters.
    colection_params: dict
        Additional parameters encoded within a dictionnary that will be
        passed in the fetch and preprocessing function.
    """

    def __init__(
        self,
        backend_context: BackendContext,
        bands: list,
        collection_fetch: Callable,
        collection_preprocessing: Callable,
        **collection_params,
    ):
        self.backend_contect = backend_context
        self.bands = bands
        self.fetcher = collection_fetch
        self.processing = collection_preprocessing
        self.params = collection_params

    def get_cube(
        self,
        connection: openeo.Connection,
        spatial_context: SpatialContext,
        temporal_context: TemporalContext,
    ) -> openeo.DataCube:
        """Retrieve a data cube from the given spatial and temporal context.

        Parameters
        ----------
        connection: openeo.Connection
            A connection to an OpenEO backend. The backend provided must be the
            same as the one this extractor class is configured for.
        spatial_extent: SpatialContext
            Either a GeoJSON collection on which spatial filtering will be
            applied or a bounding box with an EPSG code. If a bounding box is
            provided, no filtering is applied and the entirety of the data is
            fetched for that region.
        temporal_extent: TemporalContext
            The begin and end date of the extraction.
        """
        collection_data = self.fetcher(
            connection, spatial_context, temporal_context, self.bands, **self.params
        )

        preprocessed_data = self.processing(collection_data, **self.params)

        return preprocessed_data