Skip to content

Commit

Permalink
check cache input
Browse files Browse the repository at this point in the history
rafapereirabr committed Sep 4, 2024
1 parent fe6460b commit 624f1b6
Showing 5 changed files with 41 additions and 6 deletions.
8 changes: 5 additions & 3 deletions r-package/NEWS.md
Original file line number Diff line number Diff line change
@@ -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




18 changes: 16 additions & 2 deletions r-package/R/read_municipality.R
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions r-package/R/utils.R
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 6 additions & 1 deletion r-package/man/read_municipality.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions r-package/tests/testthat/test-read_municipality.R
Original file line number Diff line number Diff line change
@@ -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))

})

0 comments on commit 624f1b6

Please sign in to comment.