Skip to content

Commit

Permalink
Functions now include a cache parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Sep 2, 2024
1 parent a8b35b0 commit 8235a1f
Show file tree
Hide file tree
Showing 56 changed files with 471 additions and 96 deletions.
1 change: 1 addition & 0 deletions r-package/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

**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.
- 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
Expand Down
10 changes: 8 additions & 2 deletions r-package/R/read_amazon.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param year Numeric. Year of the data in YYYY format. Defaults to `2012`.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -18,7 +19,10 @@
#' # Read Brazilian Legal Amazon
#' a <- read_amazon(year = 2012)
#'
read_amazon <- function(year=2012, simplified=TRUE, showProgress=TRUE){
read_amazon <- function(year = 2012,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="amazonia_legal", year=year, simplified=simplified)
Expand All @@ -30,7 +34,9 @@ read_amazon <- function(year=2012, simplified=TRUE, showProgress=TRUE){
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
10 changes: 8 additions & 2 deletions r-package/R/read_biomes.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @param year Numeric. Year of the data in YYYY format. Defaults to `2019`.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -19,7 +20,10 @@
#' # Read biomes
#' b <- read_biomes(year = 2019)
#'
read_biomes <- function(year=2019, simplified=TRUE, showProgress=TRUE){
read_biomes <- function(year = 2019,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="biomes", year=year, simplified=simplified)
Expand All @@ -28,7 +32,9 @@ read_biomes <- function(year=2019, simplified=TRUE, showProgress=TRUE){
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
3 changes: 2 additions & 1 deletion r-package/R/read_capitals.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
#' # Read simple data.frame of state capitals
#' capitals_df <- read_capitals(as_sf = FALSE)
#'
read_capitals <- function(as_sf = TRUE, showProgress = TRUE){
read_capitals <- function(as_sf = TRUE,
showProgress = TRUE){

# check input
if (!is.logical(as_sf)) { stop("'as_sf' must be of type 'logical'") }
Expand Down
16 changes: 13 additions & 3 deletions r-package/R/read_census_tract.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#' are separate data sets.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -35,7 +36,12 @@
#' # Read all census tracts of the country at a given year
#' c <- read_census_tract(code_tract="all", year=2010)
#'
read_census_tract <- function(code_tract, year=2010, zone = "urban", simplified=TRUE, showProgress=TRUE){
read_census_tract <- function(code_tract,
year = 2010,
zone = "urban",
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="census_tract", year=year, simplified=simplified)
Expand Down Expand Up @@ -70,7 +76,9 @@ read_census_tract <- function(code_tract, year=2010, zone = "urban", simplified=
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down Expand Up @@ -105,7 +113,9 @@ read_census_tract <- function(code_tract, year=2010, zone = "urban", simplified=

}
# download files
sf <- download_gpkg(file_url, showProgress = showProgress)
sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(sf)) { return(invisible(NULL)) }
Expand Down
11 changes: 9 additions & 2 deletions r-package/R/read_comparable_areas.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' to `2010`.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -30,7 +31,11 @@
#' @examplesIf identical(tolower(Sys.getenv("NOT_CRAN")), "true")
#' amc <- read_comparable_areas(start_year=1970, end_year=2010)
#'
read_comparable_areas <- function(start_year=1970, end_year=2010, simplified=TRUE, showProgress=TRUE){
read_comparable_areas <- function(start_year = 1970,
end_year = 2010,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# tests
years_available <- c(1872,1900,1911,1920,1933,1940,1950,1960,1970,1980,1991,2000,2010)
Expand All @@ -55,7 +60,9 @@ read_comparable_areas <- function(start_year=1970, end_year=2010, simplified=TRU
file_url <- file_url[ file_url %like% target_year]

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
10 changes: 8 additions & 2 deletions r-package/R/read_conservation_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param date Numeric. Date of the data in YYYYMM format. Defaults to `201909`.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -18,7 +19,10 @@
#' # Read conservation_units
#' b <- read_conservation_units(date = 201909)
#'
read_conservation_units <- function(date=201909, simplified=TRUE, showProgress=TRUE){
read_conservation_units <- function(date = 201909,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="conservation_units", year=date, simplified=simplified)
Expand All @@ -27,7 +31,9 @@ read_conservation_units <- function(date=201909, simplified=TRUE, showProgress=T
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
10 changes: 8 additions & 2 deletions r-package/R/read_country.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param year Numeric. Year of the data in YYYY format. Defaults to `2010`.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -16,7 +17,10 @@
#' # Read specific year
#' br <- read_country(year = 2018)
#'
read_country <- function(year=2010, simplified=TRUE, showProgress=TRUE){
read_country <- function(year = 2010,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="country", year=year, simplified=simplified)
Expand All @@ -28,7 +32,9 @@ read_country <- function(year=2010, simplified=TRUE, showProgress=TRUE){
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
10 changes: 8 additions & 2 deletions r-package/R/read_disaster_risk_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' @param year Numeric. Year of the data in YYYY format. Defaults to `2010`.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -24,7 +25,10 @@
#' # Read all disaster risk areas in an specific year
#' d <- read_disaster_risk_area(year=2010)
#'
read_disaster_risk_area <- function(year=2010, simplified=TRUE, showProgress=TRUE){
read_disaster_risk_area <- function(year = 2010,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="disaster_risk_area", year=year, simplified=simplified)
Expand All @@ -33,7 +37,9 @@ read_disaster_risk_area <- function(year=2010, simplified=TRUE, showProgress=TRU
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
9 changes: 7 additions & 2 deletions r-package/R/read_health_facilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' @param date Numeric. Date of the data in YYYYMM format. Defaults to `202303`,
#' which was the latest data available by the time of this update.
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -30,7 +31,9 @@
#' # Read all health facilities of the whole country
#' h <- read_health_facilities( date = 202303)
#'
read_health_facilities <- function(date = 202303, showProgress = TRUE){
read_health_facilities <- function(date = 202303,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="health_facilities", year=date, simplified=F)
Expand All @@ -39,7 +42,9 @@ read_health_facilities <- function(date = 202303, showProgress = TRUE){
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
11 changes: 9 additions & 2 deletions r-package/R/read_health_region.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#' regions data. If `TRUE`, the function downloads macro regions data.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -26,7 +27,11 @@
#' # Read all macro health regions
#' mhr <- read_health_region( year=2013, macro =TRUE)
#'
read_health_region <- function(year=2013, macro=FALSE, simplified=TRUE, showProgress=TRUE){
read_health_region <- function(year = 2013,
macro = FALSE,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

if(!is.logical(macro)){stop(paste0("Parameter 'macro' must be either TRUE or FALSE"))}

Expand All @@ -41,7 +46,9 @@ read_health_region <- function(year=2013, macro=FALSE, simplified=TRUE, showProg
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
11 changes: 9 additions & 2 deletions r-package/R/read_immediate_region.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' immediate regions of the country.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -30,7 +31,11 @@
#' im <- read_immediate_region()
#' im <- read_immediate_region(code_immediate="all")
#'
read_immediate_region <- function(code_immediate="all", year=2019, simplified=TRUE, showProgress=TRUE){
read_immediate_region <- function(code_immediate = "all",
year = 2019,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="immediate_regions", year=year, simplified=simplified)
Expand All @@ -42,7 +47,9 @@ read_immediate_region <- function(code_immediate="all", year=2019, simplified=TR
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
10 changes: 8 additions & 2 deletions r-package/R/read_indigenous_land.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @param date Numeric. Date of the data in YYYYMM format. Defaults to `201907`.
#' @template simplified
#' @template showProgress
#' @template cache
#'
#' @return An `"sf" "data.frame"` object
#'
Expand All @@ -21,7 +22,10 @@
#' # Read all indigenous land in an specific date
#' i <- read_indigenous_land(date=201907)
#'
read_indigenous_land <- function(date=201907, simplified=TRUE, showProgress=TRUE){
read_indigenous_land <- function(date = 201907,
simplified = TRUE,
showProgress = TRUE,
cache = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="indigenous_land", year=date, simplified=simplified)
Expand All @@ -30,7 +34,9 @@ read_indigenous_land <- function(date=201907, simplified=TRUE, showProgress=TRUE
file_url <- as.character(temp_meta$download_path)

# download files
temp_sf <- download_gpkg(file_url, showProgress = showProgress)
temp_sf <- download_gpkg(file_url = file_url,
showProgress = showProgress,
cache = cache)

# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }
Expand Down
Loading

0 comments on commit 8235a1f

Please sign in to comment.