From 5a288b1e56310ebde4278146397c3b483f3fe501 Mon Sep 17 00:00:00 2001 From: Cidree Date: Fri, 11 Oct 2024 15:15:26 +0200 Subject: [PATCH] revert RODBC --- DESCRIPTION | 3 ++- NEWS.md | 2 ++ R/forest-inventory.R | 28 +++++++++++++++------------- cran-comments.md | 4 ++-- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b4564f6..f37d849 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,8 +41,9 @@ Collate: 'pathogens.R' Suggests: aws.s3, + DBI, giscoR, - RODBC, + odbc, testthat (>= 3.0.0) Config/testthat/edition: 3 URL: https://cidree.github.io/forestdata/ diff --git a/NEWS.md b/NEWS.md index 983cd9f..8c49da7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,6 +34,8 @@ - Improve documentation, fix mistakes in urls to other functions, add unit tests for all functions, correct wrong `fd_forest_extent_glad()` reference. +- Remove dependency on `RODBC`, and use `DBI` and `odbc` instead in `fd_inventory_spain()`. Improve error message when data is not available. + - `fd_canopy_height()`: now crops and then merges multiples tiles, so it's much faster when using `crop = TRUE`. It acquires argument `mask`, and losses `...` passed to `terra::crop()` because it fails to mask when used within `crop()`. - Now every function have the argument `quiet = FALSE` by default, and retrieves the citation of the dataset. diff --git a/R/forest-inventory.R b/R/forest-inventory.R index a39ed9d..c2892f8 100644 --- a/R/forest-inventory.R +++ b/R/forest-inventory.R @@ -242,7 +242,8 @@ fd_inventory_spain <- function(province, path_metadata = NULL, quiet = FALSE) { # 0. Handle errors - if (!requireNamespace("RODBC", quietly = TRUE)) stop("Package `RODBC` is required to access the inventory data. Please, install it.") + if (!requireNamespace("DBI", quietly = TRUE)) stop("Package `DBI` is required to access the inventory data. Please, install it.") + if (!requireNamespace("odbc", quietly = TRUE)) stop("Package `odbc` is required to access the inventory data. Please, install it.") # 1. Filter province ## 1.1. Fix province province_fix <- province |> @@ -318,23 +319,24 @@ fd_inventory_spain <- function(province, ## 4.1. File name filename <- list.files(dir_unzip, full.names = TRUE, pattern = "\\.accdb$") ## 4.2. Connect to DB - ## 4.2. Connect to DB - conn <- RODBC::odbcConnectAccess2007(filename) - ## 4.3. Table names - tables_vec <- RODBC::sqlTables(conn) |> - dplyr::filter(TABLE_TYPE != "SYSTEM TABLE") |> - dplyr::pull(TABLE_NAME) - ## 4.4. Read data into a list + conn <- odbc::dbConnect(odbc::odbc(), + .connection_string = paste0( + "Driver={Microsoft Access Driver (*.mdb, *.accdb)};", + "Dbq=", filename, ";" + )) + ## 4.3. Disconnect on exit + on.exit(DBI::dbDisconnect(conn)) + ## 4.4. Table names + tables_vec <- DBI::dbListTables(conn)[!grepl("^MSys", DBI::dbListTables(conn))] + ## 4.5. Read data into a list data_lst <- purrr::map( .x = tables_vec, - .f = \(x) RODBC::sqlFetch(conn, x) |> + .f = \(x) DBI::dbReadTable(conn, x) |> tibble::as_tibble() ) - ## 4.5. Disconnect from DB - RODBC::odbcClose(conn) - ## 4.5. Rename list + ## 4.6. Rename list names(data_lst) <- tables_vec - ## 4.6. Convert IFN4 PCDatosMap to projected SF (only in field database) + ## 4.7. Convert IFN4 PCDatosMap to projected SF (only in field database) ## -> IFN3 doesn't have huso column if (ifn == 4 & database == "field") { data_lst$PCDatosMap_sf <- sf::st_as_sf( diff --git a/cran-comments.md b/cran-comments.md index 858617d..fa2104a 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,5 +1,5 @@ ## R CMD check results -0 errors | 0 warnings | 1 note +0 errors | 0 warnings | 0 note + -* This is a new release.