Skip to content

Commit

Permalink
closes #338
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Mar 31, 2024
1 parent 92b3520 commit e8fd768
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
11 changes: 10 additions & 1 deletion r-package/R/read_urban_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#' \url{https://biblioteca.ibge.gov.br/visualizacao/livros/liv100639.pdf}
#'
#' @param year Numeric. Year of the data in YYYY format. Defaults to `2015`.
#' @param code_state The two-digit code of a state or a two-letter uppercase
#' abbreviation (e.g. 33 or "RJ"). If `code_state="all"` (the
#' default), the function downloads all states.
#' @template simplified
#' @template showProgress
#'
Expand All @@ -20,7 +23,10 @@
#' # Read urban footprint of Brazilian cities in an specific year
#' d <- read_urban_area(year=2005)
#'
read_urban_area <- function(year=2015, simplified=TRUE, showProgress=TRUE){
read_urban_area <- function(year = 2015,
code_state = "all",
simplified = TRUE,
showProgress = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(geography="urban_area", year=year, simplified=simplified)
Expand All @@ -34,5 +40,8 @@ read_urban_area <- function(year=2015, simplified=TRUE, showProgress=TRUE){
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

# filter state
temp_sf <- filter_state(temp_sf, code = code_state)

return(temp_sf)
}
11 changes: 10 additions & 1 deletion r-package/man/read_urban_area.Rd

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

22 changes: 22 additions & 0 deletions r-package/tests/testthat/test-read_urban_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@ test_that("read_urban_area", {

# read data and check sf object
test_sf <- read_urban_area()
expect_true(is(test_sf, "sf"))

# filter state code
test_sf <- read_urban_area(code_state = 33)
expect_true(is(test_sf, "sf"))
expect_true(33 %in% unique(test_sf$code_state))

test_sf <- read_urban_area(code_state = c(33, 35))
expect_true(is(test_sf, "sf"))
expect_true(all(c(33, 35) %in% unique(test_sf$code_state)))

# filter state abbrev
test_sf <- read_urban_area(code_state = 'RJ')
expect_true(is(test_sf, "sf"))
expect_true('RJ' %in% unique(test_sf$abbrev_state))

test_sf <- read_urban_area(code_state = c('RJ', 'SP'))
expect_true(is(test_sf, "sf"))
expect_true(all(c('RJ', 'SP') %in% unique(test_sf$abbrev_state)))

})

Expand All @@ -25,4 +42,9 @@ test_that("read_urban_area", {
# Wrong year
expect_error(read_urban_area(year=9999999))

# filter state
expect_error(read_urban_area(code_state = c('RJ', 33)))
expect_error(read_urban_area(code_state = 'banana'))
expect_error(read_urban_area(code_state = 999999999))

})

0 comments on commit e8fd768

Please sign in to comment.