Skip to content

Commit e8fd768

Browse files
committed
closes #338
1 parent 92b3520 commit e8fd768

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

r-package/R/read_urban_area.R

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#' \url{https://biblioteca.ibge.gov.br/visualizacao/livros/liv100639.pdf}
88
#'
99
#' @param year Numeric. Year of the data in YYYY format. Defaults to `2015`.
10+
#' @param code_state The two-digit code of a state or a two-letter uppercase
11+
#' abbreviation (e.g. 33 or "RJ"). If `code_state="all"` (the
12+
#' default), the function downloads all states.
1013
#' @template simplified
1114
#' @template showProgress
1215
#'
@@ -20,7 +23,10 @@
2023
#' # Read urban footprint of Brazilian cities in an specific year
2124
#' d <- read_urban_area(year=2005)
2225
#'
23-
read_urban_area <- function(year=2015, simplified=TRUE, showProgress=TRUE){
26+
read_urban_area <- function(year = 2015,
27+
code_state = "all",
28+
simplified = TRUE,
29+
showProgress = TRUE){
2430

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

43+
# filter state
44+
temp_sf <- filter_state(temp_sf, code = code_state)
45+
3746
return(temp_sf)
3847
}

r-package/man/read_urban_area.Rd

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r-package/tests/testthat/test-read_urban_area.R

+22
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,25 @@ test_that("read_urban_area", {
1212

1313
# read data and check sf object
1414
test_sf <- read_urban_area()
15+
expect_true(is(test_sf, "sf"))
16+
17+
# filter state code
18+
test_sf <- read_urban_area(code_state = 33)
19+
expect_true(is(test_sf, "sf"))
20+
expect_true(33 %in% unique(test_sf$code_state))
21+
22+
test_sf <- read_urban_area(code_state = c(33, 35))
23+
expect_true(is(test_sf, "sf"))
24+
expect_true(all(c(33, 35) %in% unique(test_sf$code_state)))
25+
26+
# filter state abbrev
27+
test_sf <- read_urban_area(code_state = 'RJ')
28+
expect_true(is(test_sf, "sf"))
29+
expect_true('RJ' %in% unique(test_sf$abbrev_state))
1530

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

1835
})
1936

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

45+
# filter state
46+
expect_error(read_urban_area(code_state = c('RJ', 33)))
47+
expect_error(read_urban_area(code_state = 'banana'))
48+
expect_error(read_urban_area(code_state = 999999999))
49+
2850
})

0 commit comments

Comments
 (0)