Skip to content

Commit 85a1392

Browse files
committed
streamline micro, meso and muni
1 parent e841431 commit 85a1392

File tree

5 files changed

+97
-79
lines changed

5 files changed

+97
-79
lines changed

r-package/R/read_meso_region.R

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,63 @@ read_meso_region <- function(code_meso = "all",
4343
# check if download failed
4444
if (is.null(temp_meta)) { return(invisible(NULL)) }
4545

46-
# Verify code_meso input
47-
48-
# if code_meso=="all", read the entire country
49-
if(code_meso=="all"){
46+
# Verify code_meso input
47+
if (!any(code_meso == 'all' |
48+
code_meso %in% temp_meta$code |
49+
substring(code_meso, 1, 2) %in% temp_meta$code |
50+
code_meso %in% temp_meta$code_abbrev
51+
)) {
52+
stop("Error: Invalid Value to argument code_meso.")
53+
}
5054

51-
# list paths of files to download
55+
# get file url
56+
if (code_meso=="all") {
5257
file_url <- as.character(temp_meta$download_path)
5358

54-
# download files
55-
temp_sf <- download_gpkg(file_url = file_url,
56-
showProgress = showProgress,
57-
cache = cache)
58-
59-
# check if download failed
60-
if (is.null(temp_sf)) { return(invisible(NULL)) }
61-
62-
return(temp_sf)
59+
} else if (is.numeric(code_meso)) { # if using numeric code_meso
60+
file_url <- as.character(subset(temp_meta, code==substr(code_meso, 1, 2))$download_path)
6361

62+
} else if (is.character(code_meso)) { # if using chacracter code_abbrev
63+
file_url <- as.character(subset(temp_meta, code_abbrev==substr(code_meso, 1, 2))$download_path)
6464
}
6565

66-
if( !(substr(x = code_meso, 1, 2) %in% temp_meta$code) & !(substr(x = code_meso, 1, 2) %in% temp_meta$code_abbrev)){
67-
stop("Error: Invalid Value to argument code_meso.")
68-
69-
} else{
70-
71-
# list paths of files to download
72-
if (is.numeric(code_meso)){ file_url <- as.character(subset(temp_meta, code==substr(code_meso, 1, 2))$download_path) }
73-
if (is.character(code_meso)){ file_url <- as.character(subset(temp_meta, code_abbrev==substr(code_meso, 1, 2))$download_path) }
66+
# download gpkg
67+
temp_sf <- download_gpkg(file_url = file_url,
68+
showProgress = showProgress,
69+
cache = cache)
7470

71+
# check if download failed
72+
if (is.null(temp_sf)) { return(invisible(NULL)) }
7573

74+
# return all municipalities
75+
if (code_meso =='all' ){
76+
return(temp_sf)
77+
}
7678

77-
# download files
78-
temp_sf <- download_gpkg(file_url = file_url,
79-
showProgress = showProgress,
80-
cache = cache)
79+
# FILTER particular region
80+
x <- code_meso
8181

82-
# check if download failed
83-
if (is.null(temp_sf)) { return(invisible(NULL)) }
82+
if (!any(code_meso %in% temp_sf$code_meso |
83+
code_meso %in% temp_sf$code_state |
84+
code_meso %in% temp_sf$abbrev_state)) {
85+
stop("Error: Invalid value to argument code_meso.")
86+
}
8487

88+
# particular state
89+
if(nchar(code_meso)==2){
8590

86-
if(nchar(code_meso)==2){
87-
return(temp_sf)
91+
if (is.numeric(code_meso)) {
92+
temp_sf <- subset(temp_sf, code_state == x)
93+
}
8894

89-
} else if(code_meso %in% temp_sf$code_meso){ # Get meso region
90-
x <- code_meso
91-
temp_sf <- subset(temp_sf, code_meso==x)
92-
return(temp_sf)
93-
} else{
94-
stop("Error: Invalid Value to argument code_meso.")
95+
if (is.character(code_meso)) {
96+
temp_sf <- subset(temp_sf, abbrev_state == x)
9597
}
9698
}
99+
100+
# particular meso
101+
if(nchar(code_meso)==4 & is.numeric(code_meso)){
102+
temp_sf <- subset(temp_sf, code_meso == x)
103+
}
104+
return(temp_sf)
97105
}

r-package/R/read_micro_region.R

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,52 +42,62 @@ read_micro_region <- function(code_micro = "all",
4242
if (is.null(temp_meta)) { return(invisible(NULL)) }
4343

4444
# Verify code_micro input
45+
if (!any(code_micro == 'all' |
46+
code_micro %in% temp_meta$code |
47+
substring(code_micro, 1, 2) %in% temp_meta$code |
48+
code_micro %in% temp_meta$code_abbrev
49+
)) {
50+
stop("Error: Invalid Value to argument code_micro.")
51+
}
4552

46-
# if code_micro=="all", read the entire country
47-
if(code_micro=="all"){
48-
49-
# list paths of files to download
53+
# get file url
54+
if (code_micro=="all") {
5055
file_url <- as.character(temp_meta$download_path)
5156

52-
# download files
53-
temp_sf <- download_gpkg(file_url = file_url,
54-
showProgress = showProgress,
55-
cache = cache)
56-
57-
# check if download failed
58-
if (is.null(temp_sf)) { return(invisible(NULL)) }
57+
} else if (is.numeric(code_micro)) { # if using numeric code_micro
58+
file_url <- as.character(subset(temp_meta, code==substr(code_micro, 1, 2))$download_path)
5959

60-
return(temp_sf)
61-
}
62-
63-
if( !(substr(x = code_micro, 1, 2) %in% temp_meta$code) & !(substr(x = code_micro, 1, 2) %in% temp_meta$code_abbrev)){
60+
} else if (is.character(code_micro)) { # if using chacracter code_abbrev
61+
file_url <- as.character(subset(temp_meta, code_abbrev==substr(code_micro, 1, 2))$download_path)
62+
}
6463

65-
stop("Error: Invalid Value to argument code_micro.")
64+
# download gpkg
65+
temp_sf <- download_gpkg(file_url = file_url,
66+
showProgress = showProgress,
67+
cache = cache)
6668

67-
} else{
69+
# check if download failed
70+
if (is.null(temp_sf)) { return(invisible(NULL)) }
6871

69-
# list paths of files to download
70-
if (is.numeric(code_micro)){ file_url <- as.character(subset(temp_meta, code==substr(code_micro, 1, 2))$download_path) }
71-
if (is.character(code_micro)){ file_url <- as.character(subset(temp_meta, code_abbrev==substr(code_micro, 1, 2))$download_path) }
72+
# return all municipalities
73+
if (code_micro =='all' ){
74+
return(temp_sf)
75+
}
7276

77+
# FILTER particular region
78+
x <- code_micro
7379

74-
# download files
75-
sf <- download_gpkg(file_url = file_url,
76-
showProgress = showProgress,
77-
cache = cache)
80+
if (!any(code_micro %in% temp_sf$code_micro |
81+
code_micro %in% temp_sf$code_state |
82+
code_micro %in% temp_sf$abbrev_state)) {
83+
stop("Error: Invalid value to argument code_micro.")
84+
}
7885

79-
# check if download failed
80-
if (is.null(sf)) { return(invisible(NULL)) }
86+
# particular state
87+
if(nchar(code_micro)==2){
8188

82-
if(nchar(code_micro)==2){
83-
return(sf)
89+
if (is.numeric(code_micro)) {
90+
temp_sf <- subset(temp_sf, code_state == x)
91+
}
8492

85-
} else if(code_micro %in% sf$code_micro){ # Get micro region
86-
x <- code_micro
87-
sf <- subset(sf, code_micro==x)
88-
return(sf)
89-
} else{
90-
stop("Error: Invalid Value to argument code_micro. There was no micro region with this code in this year")
93+
if (is.character(code_micro)) {
94+
temp_sf <- subset(temp_sf, abbrev_state == x)
9195
}
9296
}
97+
98+
# particular micro
99+
if(nchar(code_micro)==5 & is.numeric(code_micro)){
100+
temp_sf <- subset(temp_sf, code_micro == x)
101+
}
102+
return(temp_sf)
93103
}

r-package/R/read_municipality.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,8 @@ read_municipality <- function(code_muni = "all",
100100
}
101101

102102
# particular muni
103-
if(nchar(code_muni)>2){
104-
if (is.numeric(code_muni)) {
105-
temp_sf <- subset(temp_sf, code_state == x)
103+
if(nchar(code_muni)>2 & is.numeric(code_muni)){
104+
temp_sf <- subset(temp_sf, code_muni == x)
106105
}
107-
}
108106
return(temp_sf)
109107
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ test_that("read_micro_region", {
1313
# expect_true(is( read_micro_region(code_micro=11, year=2010) , "sf"))
1414
expect_true(is( read_micro_region(code_micro="all", year=2010) , "sf"))
1515

16-
test_micro_code <- read_micro_region(code_micro=11008, year=2010)
17-
18-
# check number of micro
19-
expect_equal( nrow(test_micro_code), 1)
16+
# check filter
17+
test_filter <- read_micro_region(code_micro=11008, year=2010)
18+
expect_equal( nrow(test_filter), 1)
2019

2120
})
2221

@@ -25,7 +24,6 @@ test_that("read_micro_region", {
2524
# ERRORS
2625
test_that("read_micro_region", {
2726

28-
2927
expect_error(read_micro_region(code_micro=9999999, year=9999999))
3028

3129
# Wrong code

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ test_that("read_municipality", {
3232
testthat::expect_true(is( read_municipality(code_muni='AC', year=2010) , "sf"))
3333
testthat::expect_true(is( read_municipality(code_muni=1200179, year=2010) , "sf"))
3434

35+
# check filter
36+
test_filter <- read_municipality(code_muni=1200179, year=2010)
37+
expect_equal( nrow(test_filter), 1)
38+
3539
})
3640

3741

0 commit comments

Comments
 (0)