Skip to content

Commit

Permalink
passing all checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Aug 30, 2023
1 parent 388d850 commit 19af685
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
10 changes: 7 additions & 3 deletions r-package/R/read_access.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@
#' all <- read_access(city = 'all', mode = 'walk', year = 2019, showProgress = FALSE)
#'

read_access <- function(city=NULL, mode = 'walk', peak = TRUE, year = 2019, geometry = FALSE, showProgress = TRUE){
read_access <- function(city = NULL,
mode = 'walk',
peak = TRUE,
year = 2019,
geometry = FALSE,
showProgress = TRUE){

# checks
checkmate::assert_logical(peak)
checkmate::assert_logical(geometry)
checkmate::assert_logical(showProgress)

# remove accents
city <- tolower(city)
Expand Down Expand Up @@ -193,7 +197,7 @@ read_access <- function(city=NULL, mode = 'walk', peak = TRUE, year = 2019, geom
file_url <- as.character(temp_meta$download_path2)

# download files
aop_access <- download_data(file_url, progress_bar = showProgress)
aop_access <- download_data(url = file_url, progress_bar = showProgress)

# check if download failed
if (is.null(aop_access)) { return(invisible(NULL)) } # nocov
Expand Down
5 changes: 3 additions & 2 deletions r-package/R/read_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
#' # all <- read_grid(city = 'all', showProgress = FALSE)
#'

read_grid <- function(city=NULL, showProgress = FALSE){
read_grid <- function(city = NULL,
showProgress = FALSE){

# checks
checkmate::assert_logical(showProgress)
Expand All @@ -68,7 +69,7 @@ read_grid <- function(city=NULL, showProgress = FALSE){
file_url <- as.character(temp_meta$download_path2)

# download files
aop_sf <- download_data(file_url, progress_bar = showProgress)
aop_sf <- download_data(url = file_url, progress_bar = showProgress)

# check if download failed
if (is.null(aop_sf)) { return(invisible(NULL)) } # nocov
Expand Down
8 changes: 5 additions & 3 deletions r-package/R/read_landuse.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@
#' # all cities
#' all <- read_landuse(city = 'all', year = 2019)
#'
read_landuse <- function(city=NULL, year = 2019, geometry = FALSE, showProgress = TRUE){
read_landuse <- function(city = NULL,
year = 2019,
geometry = FALSE,
showProgress = TRUE){

# checks
checkmate::assert_logical(geometry)
checkmate::assert_logical(showProgress)

# Get metadata with data url addresses
temp_meta <- select_metadata(t='land_use',
Expand All @@ -113,7 +115,7 @@ read_landuse <- function(city=NULL, year = 2019, geometry = FALSE, showProgress
file_url <- as.character(temp_meta$download_path2)

# download files
aop_landuse <- download_data(file_url, progress_bar = showProgress)
aop_landuse <- download_data(url = file_url, progress_bar = showProgress)

# check if download failed
if (is.null(aop_landuse)) { return(invisible(NULL)) } # nocov
Expand Down
8 changes: 5 additions & 3 deletions r-package/R/read_population.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@
#' # all cities
#' all <- read_population(city = 'all', year = 2010)
#'
read_population <- function(city=NULL, year = 2010, geometry = FALSE, showProgress = TRUE){
read_population <- function(city=NULL,
year = 2010,
geometry = FALSE,
showProgress = TRUE){

# checks
checkmate::assert_logical(geometry)
checkmate::assert_logical(showProgress)

# Get metadata with data url addresses
temp_meta <- select_metadata(t='population',
Expand All @@ -95,7 +97,7 @@ read_population <- function(city=NULL, year = 2010, geometry = FALSE, showProgre
file_url <- as.character(temp_meta$download_path2)

# download files
aop_population <- download_data(file_url, progress_bar = showProgress)
aop_population <- download_data(url = file_url, progress_bar = showProgress)

# check if download failed
if (is.null(aop_population)) { return(invisible(NULL)) }
Expand Down
28 changes: 17 additions & 11 deletions r-package/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ select_metadata <- function(t=NULL, c=NULL, y=NULL, m=NULL){
#'
download_data <- function(url, progress_bar = showProgress){

if (!(progress_bar %in% c(T, F))) { stop("Value to argument 'showProgress' has to be either TRUE or FALSE") }
# check showProgress input
if (!(progress_bar %in% c(TRUE, FALSE))) {
stop("Value to argument 'showProgress' has to be either TRUE or FALSE")
}

# get backup links
filenames <- basename(url)
Expand All @@ -182,7 +185,7 @@ download_data <- function(url, progress_bar = showProgress){
# location of temp_file
temps <- paste0(tempdir(),"/", unlist(lapply(strsplit(url,"/"),tail,n=1L)))

# check if file has not been downloaded already. If not, download it
# if file has not been downloaded already. If not, download it
if (!file.exists(temps) | file.info(temps)$size == 0) {

# test connection with server1
Expand All @@ -209,8 +212,8 @@ download_data <- function(url, progress_bar = showProgress){
if (any(!file.exists(temps) | file.info(temps)$size == 0)) { return(invisible(NULL)) }

# load gpkg to memory
temp_sf <- load_data(temps)
return(temp_sf)
temp_data <- load_data(temps)
return(temp_data)
}


Expand Down Expand Up @@ -263,8 +266,6 @@ download_data <- function(url, progress_bar = showProgress){
# load data
temp_data <- load_data(temps)
return(temp_data)


}

}
Expand Down Expand Up @@ -293,26 +294,31 @@ load_data <- function(temps=NULL){
# read file
if( fformat=='csv' ){ temp <- data.table::fread(temps) }
if( fformat=='gpkg' ){ temp <- sf::st_read(temps, quiet=T) }
return(temp)
}

### multiple files
else if (length(temps) > 1) {

# access csv
# csv
if( fformat=='csv' ){
files <- lapply(X=temps, FUN= data.table::fread)
temp <- data.table::rbindlist(files, fill = TRUE)
}

# grid geopackage
# geopackage
if( fformat=='gpkg' ){
files <- lapply(X=temps, FUN= sf::st_read, quiet=T)
temp <- sf::st_as_sf(data.table::rbindlist(files, fill = TRUE))
}
}

return(temp)
# check if data was read Ok
if (nrow(temp)==0) {
message("A file must have been corrupted during download. Please restart your R session and download the data again.")
return(invisible(NULL))
}

return(temp)

# load data to memory
temp_data <- load_data(temps)
return(temp_data)
Expand Down

0 comments on commit 19af685

Please sign in to comment.