Skip to content

Commit d3b81c3

Browse files
committed
fix inventory
1 parent c0b5ffa commit d3b81c3

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

R/forest-inventory.R

+6-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fd_inventory_spain <- function(province,
7272
cli::cli_process_failed()
7373
return(cli::cli_alert_danger("`fd_inventory_spain()` failed to retrieve the data. Service might be currently unavailable"))
7474
}
75-
if (!quiet) cli::cli_progress_step("Preparing data...", "Prepared")
75+
if (!quiet) cli::cli_progress_step("Preparing data...", "Prepared")
7676

7777
# 3. Download metadata?
7878
if (!is.null(path_metadata)) {
@@ -200,12 +200,14 @@ fd_inventory_spain <- function(province,
200200

201201
}
202202
## 4.7. Process level
203-
if (!quiet) cli::cli_process_done()
204203
if (database == "field" & ifn %in% c(3, 4) & process_level > 0) {
205-
process_ifn(data_lst, process_level = process_level, ifn = ifn)
204+
final_data <- process_ifn(data_lst, process_level = process_level, ifn = ifn, province_fix)
205+
206206
} else {
207-
return(data_lst)
207+
final_data <- data_lst
208208
}
209+
if (!quiet) cli::cli_process_done()
210+
return(final_data)
209211

210212
}
211213

R/utils-not-exported.R

+16-11
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ process_pmayores <- function(data) {
322322
## Remove NA
323323
dplyr::filter(
324324
!is.na(d_mm),
325-
h_m > 0
325+
h_m > 0,
326+
d_mm >= 75
326327
) |>
327328
## Get diametric classes
328329
dplyr::mutate(
@@ -331,7 +332,7 @@ process_pmayores <- function(data) {
331332
## Calculate number of trees per plot and species
332333
dplyr::summarise(
333334
# Ht = mean(Ht, na.rm = TRUE),
334-
n = n(),
335+
n = dplyr::n(),
335336
.by = c(plot, species_name, dclass, h_m)
336337
) |>
337338
## Calculate average height
@@ -412,7 +413,7 @@ nest_ifn_tree <- function(data, codes, agents, which = "current", ifn = 4, proce
412413
## PROCESS PREVIOUS IFN
413414
## PROCESS PREVIOUS IFN 4
414415
if (ifn == 4) {
415-
first_level <- data_lst$PCMayores3 |>
416+
first_level <- data$PCMayores3 |>
416417
dplyr::mutate(d_mm = (Dn1 + Dn2) / 2) |>
417418
merge(codes, by.x = "Especie", by.y = "species_code") |>
418419
merge(agents, by.x = "Agente", by.y = "agent_code") |>
@@ -430,7 +431,7 @@ nest_ifn_tree <- function(data, codes, agents, which = "current", ifn = 4, proce
430431

431432
} else {
432433
## PROCESS PREVIOUS IFN 3
433-
first_level <- data_lst$PCMayores2 |>
434+
first_level <- data$PCMayores2 |>
434435
dplyr::mutate(d_mm = (Diametro1 + Diametro2) / 2) |>
435436
merge(codes, by.x = "Especie", by.y = "species_code") |>
436437
dplyr::select(
@@ -515,7 +516,7 @@ nest_ifn_regeneration <- function(data, codes, process_level = 1) {
515516
#'
516517
#' @return An \code{sf} object
517518
#' @keywords internal
518-
process_ifn <- function(data, process_level = 1, ifn = 4) {
519+
process_ifn <- function(data, process_level = 1, ifn = 4, province_fix) {
519520
## process individually
520521
data_sf <- data$PCDatosMap_sf |>
521522
dplyr::select(province = Provincia, plot = Estadillo) |>
@@ -524,13 +525,17 @@ process_ifn <- function(data, process_level = 1, ifn = 4) {
524525
tree_tbl <- nest_ifn_tree(data, ifn_codes_tbl, ifn_agent_tbl, which = "current", ifn = ifn, process_level = process_level)
525526
tree_ifn3_tbl <- nest_ifn_tree(data, ifn_codes_tbl, ifn_agent_tbl, which = "prev", ifn = ifn, process_level = process_level)
526527
regeneration_tbl <- nest_ifn_regeneration(data, ifn_codes_tbl, process_level = process_level)
527-
528+
## rename previous ifn
529+
if (ifn == 4) names(tree_ifn3_tbl) <- c("plot", "tree_ifn3") else names(tree_ifn3_tbl) <- c("plot", "tree_ifn2")
528530
## merge all together
529-
dplyr::left_join(data_sf, tree_tbl) |>
530-
dplyr::left_join(tree_ifn3_tbl) |>
531-
dplyr::left_join(shrub_tbl) |>
532-
dplyr::left_join(regeneration_tbl) |>
533-
dplyr::relocate(geometry, .after = 7)
531+
suppressMessages({
532+
dplyr::left_join(data_sf, tree_tbl) |>
533+
dplyr::left_join(tree_ifn3_tbl) |>
534+
dplyr::left_join(shrub_tbl) |>
535+
dplyr::left_join(regeneration_tbl) |>
536+
dplyr::relocate(geometry, .after = 7)
537+
})
538+
534539
}
535540

536541

0 commit comments

Comments
 (0)