Skip to content

Commit d5b7b0d

Browse files
authored
Merge pull request #52 from BiodiversiteQuebec/feature/inject-teledetection-data
Feature/inject teledetection data #50
2 parents b73dbb2 + b82cb4f commit d5b7b0d

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

R/coleo_inject.R

+13-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ coleo_inject <- function(df, media_path = NULL, schema = 'public') {
4949
#==========================================================================
5050
# 1. Extract tables to be injected
5151
#==========================================================================
52-
campaign_type <- unique(df$campaigns_type)
52+
campaign_type <- coleo_return_campaign_type(df)
5353
tables <- coleo_return_required_tables(campaign_type)
5454

5555
#==========================================================================
@@ -70,13 +70,21 @@ coleo_inject <- function(df, media_path = NULL, schema = 'public') {
7070
tables <- tables[!tables %in% "observations_landmarks_lookup"]
7171
}
7272
df_id <- coleo_inject_vegetation_transect_campaigns(df)
73-
} else {
73+
## Inject campaigns
74+
} else if ("campaigns_type" %in% colnames(df)) {
7475
df_id <- coleo_inject_table(df, "campaigns", schema = schema)
75-
}
76-
77-
if(!any(sapply(df_id$campaign_error, is.null))) {
76+
if(!any(sapply(df_id$campaign_error, is.null))) {
7877
cat("Only data for successfully injected campaigns are injected in the next tables. These following lines failed to inject: ", paste0(which(!sapply(df_id$campaign_error, is.null)), collapse = ", "), "\n")
7978
}
79+
## Inject remote sensing events
80+
} else if ("remote_sensing_events_date_start" %in% colnames(df)) {
81+
df_id <- coleo_inject_table(df, "remote_sensing_events", schema = schema)
82+
if(!any(sapply(df_id$remote_sensing_event_error, is.null))) {
83+
cat("Only data for successfully injected remote sensing indicators are injected in the next tables. These following lines failed to inject: ", paste0(which(!sapply(df_id$remote_sensing_indicators_error, is.null)), collapse = ", "), "\n")
84+
}
85+
}
86+
87+
8088

8189
#==========================================================================
8290
# 3. Inject other tables

R/coleo_prep_input_data.R

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ coleo_prep_input_data <- function(df, db_table, schema = "public") {
3333
suppressMessages()
3434
}
3535

36+
# Add remote_sensing_indicator_id to required table
37+
if (db_table == "remote_sensing_events") {
38+
df <- df |>
39+
dplyr::nest_by(remote_sensing_indicators_name) |>
40+
dplyr::mutate(coleo_id = list(coleo_request_by_code(human_code = remote_sensing_indicators_name, table = "remote_sensing_indicators", schema = schema)),
41+
remote_sensing_indicator_id = coleo_extract_id(coleo_id)) |>
42+
dplyr::select(-remote_sensing_indicators_name, -coleo_id) |>
43+
dplyr::relocate(remote_sensing_indicator_id) |>
44+
tidyr::unnest(cols = c(data)) |>
45+
dplyr::ungroup() |>
46+
suppressMessages()
47+
}
48+
3649
# Campaigns table specific manipulations
3750
if (db_table == "campaigns") {
3851
## Add site_id to campaigns table

R/coleo_request.R

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ coleo_request_by_code <- function(human_code, table, schema = "public", perform
7575
names(requested_code) <- switch(table,
7676
cells = "cell_code",
7777
sites = "site_code",
78+
remote_sensing_indicators = "name",
7879
stop("idk what to do with that"))
7980

8081
written_req <- coleo_begin_req(schema) |>

tests/testthat/test-coleo_inject.R

+15
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ test_that("coleo_inject injects all data", {
9595
"obs_species_variable"))
9696
})
9797

98+
# Expect one dataframe with two row being returned
99+
test_that("coleo_inject injects remote sensing data", {
100+
101+
# Mock data
102+
rs_data <- structure(list(remote_sensing_indicators_name = "NDSI", cells_cell_code = "105_101",
103+
remote_sensing_events_date_start = paste0(sample(1000:3000,1),"-02-24"), remote_sensing_events_date_end = NA_character_,
104+
remote_sensing_obs_metric = "max", remote_sensing_obs_value = 90), row.names = 1L, class = "data.frame")
105+
106+
# Perform injection
107+
out_inject <- coleo_inject(rs_data, schema = "coleo_test")
108+
109+
# Expect a dataframe with 1 rows
110+
expect_true(is.null(out_inject$remote_sensing_event_error[[1]]))
111+
})
112+
98113

99114
#############################################
100115
# Test coleo_inject_general

0 commit comments

Comments
 (0)