From e8fd7687ce69803eb31daab82f51a60a25f91525 Mon Sep 17 00:00:00 2001 From: rafapereirabr Date: Sun, 31 Mar 2024 11:05:10 -0300 Subject: [PATCH] closes #338 --- r-package/R/read_urban_area.R | 11 +++++++++- r-package/man/read_urban_area.Rd | 11 +++++++++- .../tests/testthat/test-read_urban_area.R | 22 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/r-package/R/read_urban_area.R b/r-package/R/read_urban_area.R index 76975ddc..89067600 100644 --- a/r-package/R/read_urban_area.R +++ b/r-package/R/read_urban_area.R @@ -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 #' @@ -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) @@ -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) } diff --git a/r-package/man/read_urban_area.Rd b/r-package/man/read_urban_area.Rd index 3ff94e31..cad3d207 100644 --- a/r-package/man/read_urban_area.Rd +++ b/r-package/man/read_urban_area.Rd @@ -4,11 +4,20 @@ \alias{read_urban_area} \title{Download spatial data of urbanized areas in Brazil} \usage{ -read_urban_area(year = 2015, simplified = TRUE, showProgress = TRUE) +read_urban_area( + year = 2015, + code_state = "all", + simplified = TRUE, + showProgress = TRUE +) } \arguments{ \item{year}{Numeric. Year of the data in YYYY format. Defaults to \code{2015}.} +\item{code_state}{The two-digit code of a state or a two-letter uppercase +abbreviation (e.g. 33 or "RJ"). If \code{code_state="all"} (the +default), the function downloads all states.} + \item{simplified}{Logic \code{FALSE} or \code{TRUE}, indicating whether the function should return the data set with 'original' spatial resolution or a data set with 'simplified' geometry. Defaults to \code{TRUE}. For spatial analysis and diff --git a/r-package/tests/testthat/test-read_urban_area.R b/r-package/tests/testthat/test-read_urban_area.R index 4e01d41d..05203087 100644 --- a/r-package/tests/testthat/test-read_urban_area.R +++ b/r-package/tests/testthat/test-read_urban_area.R @@ -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))) }) @@ -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)) + })