Datasets:

ArXiv:
File size: 8,153 Bytes
3063af8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1d8c4b
3063af8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0a94a4
3063af8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0a94a4
3063af8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1d8c4b
3063af8
 
d1d8c4b
 
 
 
 
3063af8
 
 
 
 
c85fe75
3063af8
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from itertools import product
import numpy as np
import xarray as xr
import dask
import netCDF4
import datasets
from pathlib import Path


_CITATION = """\
@ARTICLE{
  9749916,
  author={Sykas, Dimitrios and Sdraka, Maria and Zografakis, Dimitrios and Papoutsis, Ioannis},
  journal={IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing},
  title={A Sentinel-2 multi-year, multi-country benchmark dataset for crop classification and segmentation with deep learning},
  year={2022},
  doi={10.1109/JSTARS.2022.3164771}
}
"""

_DESCRIPTION = """\
Sen4AgriNet is a Sentinel-2 based time series multi country benchmark dataset, tailored for 
agricultural monitoring applications with Machine and Deep Learning. It is annotated from 
farmer declarations collected via the Land Parcel Identification System (LPIS) for harmonizing 
country wide labels. These declarations have only recently been made available as open data, 
allowing for the first time the labelling of satellite imagery from ground truth data. 
We proceed to propose and standardise a new crop type taxonomy across Europe that address 
Common Agriculture Policy (CAP) needs, based on the Food and Agriculture Organization (FAO) 
Indicative Crop Classification scheme. Sen4AgriNet is the only multi-country, multi-year dataset 
that includes all spectral information. It is constructed to cover the period 2016-2020 for 
Catalonia and France, while it can be extended to include additional countries.
"""

_HOMEPAGE = "https://www.sen4agrinet.space.noa.gr/"

_LICENSE = "MIT License"

_URL = 'https://huggingface.co/datasets/paren8esis/S4A/resolve/main/data'

# The tiles of Catalonia
CAT_TILES = ['31TBF', '31TCF', '31TCG', '31TDF', '31TDG']

# The tiles of France
FR_TILES = ['31TCJ', '31TDK', '31TCL', '31TDM', '31UCP', '31UDR']

# The maximum indices for each patch 
PATCH_IDX = {
    '2019': {
        '31TBF': [29, 29],
        '31TCF': [29, 27],
        '31TCG': [29, 29],
        '31TDF': [15, 9], 
        '31TDG': [29, 29],
        '31TCJ': [29, 29], 
        '31TDK': [29, 29], 
        '31TCL': [29, 29], 
        '31TDM': [29, 29], 
        '31UCP': [29, 29], 
        '31UDR': [29, 29]
    },
    '2020': {
        '31TBF': [29, 29],
        '31TCF': [29, 27],
        '31TCG': [29, 29],
        '31TDF': [15, 9], 
        '31TDG': [29, 29]
    }
}


class S4A(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("0.0.1")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="complete", version=VERSION, description="All Sen4AgriNet data."),
        datasets.BuilderConfig(name="tiny", version=VERSION, description="Just three samples for testing."),
        datasets.BuilderConfig(name="cat_2019", version=VERSION, description="Sen4AgriNet data for Catalonia 2019."),
    ]

    DEFAULT_CONFIG_NAME = "complete"

    def _info(self):
        features = datasets.Features(
            {
                "patch_full_name": datasets.Value("string"),
                "patch_year": datasets.Value("string"),
                "patch_name": datasets.Value("string"),
                "patch_country_code": datasets.Value("string"),
                "patch_tile": datasets.Value("string"),
                "B01": datasets.Array3D(shape=(None, 61, 61), dtype="uint16"),
                "B02": datasets.Array3D(shape=(None, 366, 366), dtype="uint16"),
                "B03": datasets.Array3D(shape=(None, 366, 366), dtype="uint16"),
                "B04": datasets.Array3D(shape=(None, 366, 366), dtype="uint16"),
                "B05": datasets.Array3D(shape=(None, 183, 183), dtype="uint16"),
                "B06": datasets.Array3D(shape=(None, 183, 183), dtype="uint16"),
                "B07": datasets.Array3D(shape=(None, 183, 183), dtype="uint16"),
                "B08": datasets.Array3D(shape=(None, 366, 366), dtype="uint16"),
                "B09": datasets.Array3D(shape=(None, 61, 61), dtype="uint16"),
                "B10": datasets.Array3D(shape=(None, 61, 61), dtype="uint16"),
                "B11": datasets.Array3D(shape=(None, 183, 183), dtype="uint16"),
                "B12": datasets.Array3D(shape=(None, 183, 183), dtype="uint16"),
                "B8A": datasets.Array3D(shape=(None, 183, 183), dtype="uint16"),
                "labels": datasets.Array2D(shape=(366, 366), dtype="uint32"),
                "parcels": datasets.Array2D(shape=(366, 366), dtype="uint32"),
                "timestamp": datasets.Sequence(datasets.Value("timestamp[ns]"))
            }
        )
    
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )


    def _split_generators(self, dl_manager):
        root_paths = []
        if self.config.name == "complete":
            for year, tile in list(product(['2019'], FR_TILES)) + list(product(['2019', '2020'], CAT_TILES)):
                x, y = PATCH_IDX[year][tile]
                for x_i in range(x + 1):
                    for y_i in range(y + 1):
                        downloaded_paths = dl_manager.download(_URL + f'/{year}' + f'/{tile}' + f'/{year}_{tile}_patch_{str(x_i).zfill(2)}_{str(y_i).zfill(2)}.nc')
                        root_paths.append(downloaded_paths)

            return [
                datasets.SplitGenerator(
                    name='complete',
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "root_paths": root_paths,
                    },
                ),
            ]
        elif self.config.name == 'tiny':
            selected_files = ['2019_31TCF_patch_00_11.nc', '2019_31UCP_patch_01_19.nc', '2020_31TDG_patch_11_17.nc']

            for file in selected_files:
                year, tile = file.split('_')[:2]

                downloaded_paths = dl_manager.download(_URL + f'/{year}' + f'/{tile}' + f'/{file}')
                root_paths.append(downloaded_paths)

            return [
                datasets.SplitGenerator(
                    name='tiny',
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "root_paths": root_paths,
                    },
                ),
            ]


    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, root_paths):
        for file in root_paths:
            data = xr.open_dataset(file, chunks=-1, engine='netcdf4')

            res = {
                "patch_full_name": data.patch_full_name,
                "patch_year": data.patch_year,
                "patch_name": data.patch_name,
                "patch_country_code": data.patch_country_code,
                "patch_tile": data.patch_tile
            }

            time_recorded = False

            for variable in ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B10', 'B11', 'B12', 'B8A', 'labels', 'parcels']:
                v = xr.open_dataset(file, chunks=-1, engine='netcdf4', group=variable)
                if not time_recorded:
                    res['timestamp'] = (v.time.values.astype(np.int64) // 10 ** 9).tolist()
                    time_recorded = True

                res[variable] = getattr(v, variable).values

            key = res['patch_full_name']
            yield key, res