20
20
# ' never both.
21
21
# ' For **filter_bbox**, the bounding box, which may include a vertical axis
22
22
# ' (see `base` and `height`).
23
+ # ' @param .year integer or list of integers stating the years you want to filter on the
24
+ # ' datacube.
25
+ # ' @param .month integer or list of integers referring to the months you want to filter
26
+ # ' on the datacube.
23
27
# ' @param .dimension (optional) For **filter_temporal** : The name of the temporal dimension
24
28
# ' to filter on. If no specific dimension is specified or it is set to `null`,
25
29
# ' the filter applies to all temporal dimensions. Fails with a `DimensionNotAvailable`
28
32
# ' filtering, specified as GeoJSON.
29
33
# ' @param .context (optional) : any Additional data to be passed to the condition.
30
34
# ' Mandatory for filter_labels or array_filter processes.
31
- # ' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
35
+ # ' @param .con (optional) openeo connection. Default to NULL
32
36
# ' @param .p (optional) processes available at .con
33
37
# ' @return datacube
34
38
# ' @import dplyr openeo cli
39
43
# ' library(sf)
40
44
# '
41
45
# ' con = connect(host = "https://openeo.cloud")
42
- # ' dc = datacube(id = "SENTINEL_5P_L2")
46
+ # ' dc = datacube(id = "SENTINEL1_GRD")
47
+ # '
48
+ # ' # filter_temporal
49
+ # ' dc_y = dc %>% filter(.year = c(2020, 2021, 2022))
50
+ # '
51
+ # ' dc_m = dc %>% filter(.month = c(6,7,8))
52
+ # '
53
+ # ' dc_ym = dc %>% filter(.year = c(2020, 2021, 2022), .month = c(6,7,8))
43
54
# '
44
55
# ' # filter_temporal and filter_bbox
45
- # ' dc = dc %>%
56
+ # ' dc2 = dc %>%
46
57
# ' filter(.extent = c("2021-01-01", "2021-03-03")) %>%
47
58
# ' filter(.extent = c(west = 6.09, east = 6.99, south = 46.15, north = 46.57))
48
59
# '
55
66
# ' st_bbox() %>%
56
67
# ' st_as_sfc()
57
68
# '
58
- # ' dc = dc %>% filter(.geometries = pol)
69
+ # ' dc = dc2 %>% filter(.geometries = pol)
59
70
# '
60
71
# ' # array_filter
61
72
# ' # ToDO...
65
76
filter.datacube <- function (.data = NULL , ... ,
66
77
.condition = NULL , .dimension = NULL , .context = NULL ,
67
78
.extent = NULL , .geometries = NULL ,
79
+ .year = NULL , .month = NULL ,
68
80
.p = openeo :: processes(.con ), .con = NULL ) {
69
81
70
82
# check dots ...
@@ -89,6 +101,170 @@ filter.datacube <- function(.data = NULL, ...,
89
101
dc = .p $ filter_temporal(data = .data , extent = .extent , dimension = .dimension )
90
102
cli :: cli_alert_success(" filter_temporal applied" )
91
103
104
+ # # Working with years
105
+ } else if (! is.null(.year ) & is.null(.month ) & is.null(.geometries ) & is.null(.condition )&
106
+ is.null(.context )){
107
+
108
+ # ## Extract starting year of collection
109
+ process_json = .p $ save_result(data = .data , format = list_file_formats()$ output $ JSON ) %> %
110
+ as(" Process" ) %> % openeo :: toJSON() %> % rjson :: fromJSON() %> % suppressWarnings()
111
+ id = process_json $ process_graph [[1 ]]$ arguments $ id
112
+
113
+ collections = list_collections() %> % suppressWarnings()
114
+ native_time_ext = c(collections [[id ]]$ extent $ temporal [[1 ]][[1 ]] %> % as.Date() %> % format(" %04Y-%m-%d" ),
115
+ collections [[id ]]$ extent $ temporal [[1 ]][[2 ]])
116
+ native_time_ext = ifelse(is.na(native_time_ext ), Sys.Date() %> % format(" %04Y-%m-%d" ), native_time_ext )
117
+
118
+ # ## stop if year outside of range
119
+ if (min(.year ) < native_time_ext [1 ] %> % substr(1 ,4 ) | max(.year ) > native_time_ext [2 ] %> % substr(1 ,4 )){
120
+ stop(
121
+ cli :: cli_alert_danger(" You're trying to filter outside of the range of the collection {native_time_ext}" )
122
+ )
123
+ }
124
+
125
+ # ## Iterate through the called years
126
+ dcs = list ()
127
+ for (i in 1 : length(.year )){
128
+ dcs [[i ]] <- .p $ filter_temporal(
129
+ data = .data ,
130
+ extent = c(paste(.year [i ], " 01-01" , sep = " -" ), paste(.year [i ], " 12-31" , sep = " -" )),
131
+ dimension = .dimension )
132
+ }
133
+
134
+ # ## merge generated data cubes
135
+ if (length(dcs ) > 1 ){
136
+ for (i in 1 : length(dcs )){
137
+ if (i == 1 ){i = i + 1 }
138
+ else if (i == 2 ){
139
+ dc = .p $ merge_cubes(dcs [[i ]], dcs [[i - 1 ]])
140
+ } else {
141
+ dc = .p $ merge_cubes(dc , dcs [[i ]])
142
+ }
143
+ }
144
+ cli :: cli_alert_success(" merge_cubes applied" )
145
+
146
+ # ## Do not merge --- simple filter for a single year
147
+ } else {
148
+ dc = .p $ filter_temporal(
149
+ data = .data ,
150
+ c(paste(.year , " 01-01" , sep = " -" ), paste(.year , " 12-31" , sep = " -" )),
151
+ dimension = .dimension )
152
+ cli :: cli_alert_success(" filter_temporal applied" )
153
+ }
154
+
155
+ # # Working with Months
156
+ } else if (is.null(.year ) & ! is.null(.month ) & is.null(.geometries ) & is.null(.condition )&
157
+ is.null(.context )){
158
+
159
+ # ## Extract time extent of collection
160
+ process_json = .p $ save_result(data = .data , format = list_file_formats()$ output $ JSON ) %> %
161
+ as(" Process" ) %> % openeo :: toJSON() %> % rjson :: fromJSON() %> % suppressWarnings()
162
+ id = process_json $ process_graph [[1 ]]$ arguments $ id
163
+
164
+ collections = list_collections() %> % suppressWarnings()
165
+ native_time_ext = c(collections [[id ]]$ extent $ temporal [[1 ]][[1 ]] %> % as.Date() %> % format(" %04Y-%m-%d" ),
166
+ collections [[id ]]$ extent $ temporal [[1 ]][[2 ]])
167
+ native_time_ext = ifelse(is.na(native_time_ext ), Sys.Date() %> % format(" %04Y-%m-%d" ), native_time_ext )
168
+
169
+ # ## Iterate through the existing years and called months
170
+ years = seq(native_time_ext [1 ] %> % substr(1 , 4 ), native_time_ext [2 ] %> % substr(1 , 4 ), 1 )
171
+ months = ifelse(.month < 10 , paste0(" 0" , .month ), .month ) %> % as.character()
172
+ next_months = ifelse((.month + 1 ) == 13 , 1 , .month )
173
+ next_months = ifelse(next_months < 10 , paste0(" 0" , next_months ), next_months ) %> % as.character()
174
+ dcs = list ()
175
+ for (y in 1 : length(years )){
176
+ for (m in 1 : length(months )){
177
+ date1 = paste(years [y ], months [m ], " 01" , sep = " -" )
178
+ date2 = paste(years [y ], next_months [m ], " 01" , sep = " -" )
179
+ date2 = as.Date(date2 ) - 1
180
+ date2 = date2 %> % format(" %04Y-%m-%d" )
181
+
182
+ if (date1 < native_time_ext [1 ] | date2 > native_time_ext [2 ]){
183
+ next
184
+ }
185
+
186
+ dcs <- append(dcs , .p $ filter_temporal(
187
+ data = .data , extent = c(date1 , date2 , dimension = .dimension )))
188
+ }
189
+ }
190
+ cli :: cli_alert_success(" filter_temporal applied" )
191
+
192
+ # ## merge generated data cubes
193
+ for (i in 1 : length(dcs )){
194
+ if (i == 1 ){i = i + 1 }
195
+ else if (i == 2 ){
196
+ dc = .p $ merge_cubes(dcs [[i ]], dcs [[i - 1 ]])
197
+ } else {
198
+ dc = .p $ merge_cubes(dc , dcs [[i ]])
199
+ }
200
+ }
201
+ cli :: cli_alert_success(" merge_cubes applied" )
202
+
203
+ # # Working with Months and Years
204
+ } else if (! is.null(.year ) & ! is.null(.month ) & is.null(.geometries ) & is.null(.condition )&
205
+ is.null(.context )){
206
+
207
+ # ## Extract time extent of collection
208
+ process_json = .p $ save_result(data = .data , format = list_file_formats()$ output $ JSON ) %> %
209
+ as(" Process" ) %> % openeo :: toJSON() %> % rjson :: fromJSON() %> % suppressWarnings()
210
+ id = process_json $ process_graph [[1 ]]$ arguments $ id
211
+
212
+ collections = list_collections() %> % suppressWarnings()
213
+ native_time_ext = c(collections [[id ]]$ extent $ temporal [[1 ]][[1 ]] %> % as.Date() %> % format(" %04Y-%m-%d" ),
214
+ collections [[id ]]$ extent $ temporal [[1 ]][[2 ]])
215
+ native_time_ext = ifelse(is.na(native_time_ext ), Sys.Date() %> % format(" %04Y-%m-%d" ), native_time_ext )
216
+
217
+ # ## stop if year outside of range
218
+ if (min(.year ) < native_time_ext [1 ] %> % substr(1 ,4 ) | max(.year ) > native_time_ext [2 ] %> % substr(1 ,4 )){
219
+ stop(
220
+ cli :: cli_alert_danger(" You're trying to filter outside of the range of the collection {native_time_ext}" )
221
+ )
222
+ }
223
+
224
+ # ## Iterate through the existing years and called months
225
+ years = .year
226
+ months = ifelse(.month < 10 , paste0(" 0" , .month ), .month ) %> % as.character()
227
+ next_months = ifelse((.month + 1 ) == 13 , 1 , .month )
228
+ next_months = ifelse(next_months < 10 , paste0(" 0" , next_months ), next_months ) %> % as.character()
229
+ dcs = list ()
230
+ for (y in 1 : length(years )){
231
+ for (m in 1 : length(months )){
232
+ date1 = paste(years [y ], months [m ], " 01" , sep = " -" )
233
+ date2 = paste(years [y ], next_months [m ], " 01" , sep = " -" )
234
+ date2 = as.Date(date2 ) - 1
235
+ date2 = date2 %> % format(. , " %04Y-%m-%d" )
236
+
237
+ if (date1 < native_time_ext [1 ] | date2 > native_time_ext [2 ]){
238
+ next
239
+ }
240
+
241
+ dcs <- append(dcs , .p $ filter_temporal(
242
+ data = .data , extent = c(date1 , date2 , dimension = .dimension )))
243
+ }
244
+ }
245
+ cli :: cli_alert_success(" filter_temporal applied" )
246
+
247
+ # ## merge generated data cubes
248
+ if (length(dcs ) > 1 ){
249
+ for (i in 1 : length(dcs )){
250
+ if (i == 1 ){i = i + 1 }
251
+ else if (i == 2 ){
252
+ dc = .p $ merge_cubes(dcs [[i ]], dcs [[i - 1 ]])
253
+ } else {
254
+ dc = .p $ merge_cubes(dc , dcs [[i ]])
255
+ }
256
+ }
257
+ cli :: cli_alert_success(" merge_cubes applied" )
258
+
259
+ # ## Do not merge --- simple filter for a single year
260
+ } else {
261
+ dc = .p $ filter_temporal(
262
+ data = .data ,
263
+ c(paste(.year , " 01-01" , sep = " -" ), paste(.year , " 12-31" , sep = " -" )),
264
+ dimension = .dimension )
265
+ cli :: cli_alert_success(" filter_temporal applied" )
266
+ }
267
+
92
268
# filter_bbox
93
269
} else if (length(.extent == 4 ) & is.null(.geometries ) & is.null(.condition )&
94
270
is.null(.context )) {
0 commit comments