From 624f1b605b0c549f919a8ceafe968108b063e7bd Mon Sep 17 00:00:00 2001 From: rafapereirabr Date: Wed, 4 Sep 2024 13:51:04 -0300 Subject: [PATCH] check cache input --- r-package/NEWS.md | 8 +++++--- r-package/R/read_municipality.R | 18 ++++++++++++++++-- r-package/R/utils.R | 1 + r-package/man/read_municipality.Rd | 7 ++++++- .../tests/testthat/test-read_municipality.R | 13 +++++++++++++ 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/r-package/NEWS.md b/r-package/NEWS.md index 1ae79361..a820760f 100644 --- a/r-package/NEWS.md +++ b/r-package/NEWS.md @@ -3,11 +3,13 @@ **Minor changes** -- Functions now include a `cache` parameter that allows users to decide whehter to keep files in cache or to force downloading them again. At the moment, files are only cached during the R session, but this is a step toward future version of geobr that will be based on permanent caching. +- The `read_municipality()` has a new parameter `keep_areas_operacionais`, which allows users to control wether the data should keep the polygons of Lagoas dos Patos and Lagoa Mirim in the State of Rio Grande do Sul (considered as areas estaduais operacionais). The default `FALSE` drops these two polygons. Closes #176. +- Functions now include a `cache` parameter that allows users to decide whehter to keep files in cache or to force downloading them again. At the moment, files are only cached during the R session, but this is a step towards a future version of {geobr} when files will be based on permanent caching. - Now using `curl::multi_download()` to download files in parallel -- {geobr} now imports {fs} to use robust cross-platform file system operations - Removed dependency on the {httr} package -- Simplified internal functions +- {geobr} now imports {fs} to use robust cross-platform file system operations +- Simplified and streamlined internal functions + diff --git a/r-package/R/read_municipality.R b/r-package/R/read_municipality.R index ffb8e187..807c4875 100644 --- a/r-package/R/read_municipality.R +++ b/r-package/R/read_municipality.R @@ -14,7 +14,10 @@ #' @template simplified #' @template showProgress #' @template cache -#' +#' @param keep_areas_operacionais Logic. Whether the function should keep the +#' polygons of Lagoas dos Patos and Lagoa Mirim in the State of Rio Grande +#' do Sul (considered as areas estaduais operacionais). Defaults to `FALSE`. + #' @return An `"sf" "data.frame"` object #' #' @export @@ -35,7 +38,12 @@ read_municipality <- function(code_muni = "all", year = 2010, simplified = TRUE, showProgress = TRUE, - cache = TRUE) { + cache = TRUE, + keep_areas_operacionais = FALSE) { + + # check input + if (!is.logical(keep_areas_operacionais)) { stop("'keep_areas_operacionais' must be of type 'logical'") } + # Get metadata with data url addresses temp_meta <- select_metadata(geography="municipality", year=year, simplified=simplified) @@ -92,5 +100,11 @@ read_municipality <- function(code_muni = "all", } else {stop(paste0("Error: Invalid Value to argument 'code_muni'",collapse = " "))} + # keep_areas_operacionais + if(isFALSE(keep_areas_operacionais)){ + temp_sf <- subset(temp_sf, code_muni != 4300001) + temp_sf <- subset(temp_sf, code_muni != 4300002) + } + return(temp_sf) } diff --git a/r-package/R/utils.R b/r-package/R/utils.R index 36bcd3c6..6efa1177 100644 --- a/r-package/R/utils.R +++ b/r-package/R/utils.R @@ -169,6 +169,7 @@ download_gpkg <- function(file_url = parent.frame()$file_url, cache = parent.frame()$cache){ if (!is.logical(showProgress)) { stop("'showProgress' must be of type 'logical'") } + if (!is.logical(cache)) { stop("'cache' must be of type 'logical'") } # get backup links filenames <- basename(file_url) diff --git a/r-package/man/read_municipality.Rd b/r-package/man/read_municipality.Rd index d6f88f7a..6d7b9b32 100644 --- a/r-package/man/read_municipality.Rd +++ b/r-package/man/read_municipality.Rd @@ -9,7 +9,8 @@ read_municipality( year = 2010, simplified = TRUE, showProgress = TRUE, - cache = TRUE + cache = TRUE, + keep_areas_operacionais = FALSE ) } \arguments{ @@ -37,6 +38,10 @@ locally, which is faster. Defaults to \code{cache = TRUE}. By default, \code{geobr} stores data files in a temporary directory that exists only within each R session. If \code{cache = FALSE}, the function will download the data again and overwrite the local file.} + +\item{keep_areas_operacionais}{Logic. Whether the function should keep the +polygons of Lagoas dos Patos and Lagoa Mirim in the State of Rio Grande +do Sul (considered as areas estaduais operacionais). Defaults to \code{FALSE}.} } \value{ An \verb{"sf" "data.frame"} object diff --git a/r-package/tests/testthat/test-read_municipality.R b/r-package/tests/testthat/test-read_municipality.R index 9c556fde..b3e75535 100644 --- a/r-package/tests/testthat/test-read_municipality.R +++ b/r-package/tests/testthat/test-read_municipality.R @@ -36,6 +36,15 @@ test_that("read_municipality", { test_filter <- read_municipality(code_muni=1200179, year=2010) expect_equal( nrow(test_filter), 1) + # check keep_areas_operacionais + n22f <- read_municipality(code_muni = 'all', year = 2022) |> nrow() + n22t <- read_municipality(code_muni = 'all', year = 2022, keep_areas_operacionais = TRUE) |> nrow() + testthat::expect_true(n22t > n22f) + + # test cache + cache_true <- system.time(read_municipality(cache = TRUE)) + cache_false <- system.time(read_municipality(cache = FALSE)) + cache_false[[3]] > cache_true[[3]] }) @@ -63,5 +72,9 @@ test_that("read_municipality", { testthat::expect_error(read_municipality( showProgress = NULL)) testthat::expect_error(read_municipality( simplified = 'aaaaa')) testthat::expect_error(read_municipality( simplified = NULL)) + testthat::expect_error(read_municipality( cache = 'aaaaa')) + testthat::expect_error(read_municipality( cache = NULL)) + testthat::expect_error(read_municipality( keep_areas_operacionais = 'aaaaa')) + testthat::expect_error(read_municipality( keep_areas_operacionais = NULL)) })