Skip to content

Commit 55df987

Browse files
committed
#4 filter has now year and month as args
1 parent 8880354 commit 55df987

30 files changed

+241
-29
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: tidyopeneo
22
Type: Package
3-
Title: tidyopeneo : Wrap dplyr functions for openEO datacubes (OpenEOWrap)
3+
Title: tidyopeneo : Wrap dplyr functions for openEO datacubes (tidyopeneo)
44
Version: 0.1.0
55
Author: Huriel Reichel
66
Maintainer: The package maintainer <huriel.reichel@protonmail.com>
@@ -13,7 +13,7 @@ Encoding: UTF-8
1313
LazyData: true
1414
Depends: R (>= 3.3.3), dplyr, openeo, cli, rjson
1515
Imports: sf, methods
16-
RoxygenNote: 7.2.1
16+
RoxygenNote: 7.2.3
1717
Suggests:
1818
knitr,
1919
rmarkdown,

R/datacube.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#' openeo::processes()$load_collection() (https://processes.openeo.org/#load_collection).
55
#' This is understood as the **starting point** when working with tidyopeneo. (optional)
66
#' @param data ProcessNode datacube from openeo (optional)
7-
#' @param .con (optional) character link to openeo connection. Default to "https://openeo.cloud"
7+
#' @param .con (optional) character link to openeo connection. Default to NULL
88
#' @param .p (optional) processes available at .con
99
#' @return datacube
1010
#' @import openeo

R/filter.R

+180-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#' never both.
2121
#' For **filter_bbox**, the bounding box, which may include a vertical axis
2222
#' (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.
2327
#' @param .dimension (optional) For **filter_temporal** : The name of the temporal dimension
2428
#' to filter on. If no specific dimension is specified or it is set to `null`,
2529
#' the filter applies to all temporal dimensions. Fails with a `DimensionNotAvailable`
@@ -28,7 +32,7 @@
2832
#' filtering, specified as GeoJSON.
2933
#' @param .context (optional) : any Additional data to be passed to the condition.
3034
#' 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
3236
#' @param .p (optional) processes available at .con
3337
#' @return datacube
3438
#' @import dplyr openeo cli
@@ -39,10 +43,17 @@
3943
#' library(sf)
4044
#'
4145
#' 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))
4354
#'
4455
#' # filter_temporal and filter_bbox
45-
#' dc = dc %>%
56+
#' dc2 = dc %>%
4657
#' filter(.extent = c("2021-01-01", "2021-03-03")) %>%
4758
#' filter(.extent = c(west = 6.09, east = 6.99, south = 46.15, north = 46.57))
4859
#'
@@ -55,7 +66,7 @@
5566
#' st_bbox() %>%
5667
#' st_as_sfc()
5768
#'
58-
#' dc = dc %>% filter(.geometries = pol)
69+
#' dc = dc2 %>% filter(.geometries = pol)
5970
#'
6071
#' # array_filter
6172
#' # ToDO...
@@ -65,6 +76,7 @@
6576
filter.datacube <- function(.data = NULL, ...,
6677
.condition = NULL, .dimension = NULL, .context = NULL,
6778
.extent = NULL, .geometries = NULL,
79+
.year = NULL, .month = NULL,
6880
.p = openeo::processes(.con), .con = NULL) {
6981

7082
#check dots ...
@@ -89,6 +101,170 @@ filter.datacube <- function(.data = NULL, ...,
89101
dc = .p$filter_temporal(data = .data, extent = .extent, dimension = .dimension)
90102
cli::cli_alert_success("filter_temporal applied")
91103

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+
92268
#filter_bbox
93269
} else if (length(.extent == 4) & is.null(.geometries) & is.null(.condition)&
94270
is.null(.context)) {

R/group_by.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
#' and/or times. Is only required to be specified if the values for the start of
7272
#' the temporal intervals are not distinct and thus the default labels would not
7373
#' be unique. The number of labels and the number of groups need to be equal.
74-
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
74+
#' @param .con (optional) openeo connection. Default to NULL
7575
#' @param .p (optional) processes available at .con
7676
#' @return datacube
7777
#' @import dplyr openeo cli sf

R/mutate.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#' after, so an overlap of 8 pixels will add 8 pixels on both sides of the window,
5858
#' so 16 in total.Be aware that large overlaps increase the need for computational
5959
#' resources and modifying overlapping data in subsequent operations have no effect.
60-
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
60+
#' @param .con (optional) openeo connection. Default to NULL
6161
#' @param .p (optional) processes available at .con
6262
#' @return datacube
6363
#' @import dplyr openeo cli

R/rename.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' `DimensionNotAvailable` exception if the specified dimension does not exist.
1010
#' @param .target A new Name for the dimension. Fails with a `DimensionExists`
1111
#' exception if a dimension with the specified name exists.
12-
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
12+
#' @param .con (optional) openeo connection. Default to NULL
1313
#' @param .p (optional) processes available at .con
1414
#' @return datacube
1515
#' @import dplyr openeo cli

R/resample.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#' to no-data (`null`).
5454
#' @param .target_process (optional) either "spatial" to apply **resample_cube_spatial** or
5555
#' "temporal" to apply **resample_cube_temporal**.
56-
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
56+
#' @param .con (optional) openeo connection. Default to NULL
5757
#' @param .p (optional) processes available at .con
5858
#' @return datacube
5959
#' @details if arg .target is not defined, resample_spatial is gonna be run.

R/select.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#' specified in micrometers. The order of the specified array defines the
1919
#' order of the bands in the data cube. If multiple bands match the wavelengths,
2020
#' all matched bands are included in the original order.
21-
#' @param .con openeo connection. Default to "https://openeo.cloud"
21+
#' @param .con openeo connection. Default to NULL
2222
#' @param .p processes available at .con
2323
#' @return datacube
2424
#' @import dplyr openeo cli

R/slice.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#' If n is greater than the number of days available in the collection or datacube (or prop > 1), the result
2121
#' will be truncated to the group size. If the proportion of a group size does not yield an integer number of
2222
#'rows, the absolute value of prop * ndays(.data) is rounded down.
23-
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
23+
#' @param .con (optional) openeo connection. Default to NULL
2424
#' @param .p (optional) processes available at .con
2525
#' @return datacube
2626
#' @import dplyr openeo cli

R/summarise.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @param .dimension The name of the dimension over which to reduce. Fails with
1313
#' a `DimensionNotAvailable` exception if the specified dimension does not exist.
1414
#' @param .context (optional) Additional data to be passed to the reducer (optional).
15-
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
15+
#' @param .con (optional) openeo connection. Default to NULL
1616
#' @param .p (optional) processes available at .con
1717
#' @return datacube
1818
#' @import dplyr openeo cli

R/summarize.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @param .dimension The name of the dimension over which to reduce. Fails with
1313
#' a `DimensionNotAvailable` exception if the specified dimension does not exist.
1414
#' @param .context (optional) Additional data to be passed to the reducer (optional).
15-
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
15+
#' @param .con (optional) openeo connection. Default to NULL
1616
#' @param .p (optional) processes available at .con
1717
#' @return datacube
1818
#' @import dplyr openeo cli

man/datacube.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/filter.Rd

+19-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/group_by.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)