Skip to content

Commit

Permalink
draft of geometry fun
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed May 5, 2024
1 parent 1f0520b commit 87dd841
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 0 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@
.RData
^_pkgdown\.yml$
^pkgdown$
^data_prep
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: censobr
Title: Download Data from Brazil's Population Census
Version: 0.3.2
Version: 0.3.29999
Authors@R:
c(person(given="Rafael H. M.", family="Pereira",
email="[email protected]",
Expand Down
41 changes: 41 additions & 0 deletions tests/tests_rafa/geometry.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Add geometry information of census tracts
#'
#' @param df An arrow `Dataset` passed from function above.
#' @param year Numeric. Passed from function above.
#' @param showProgress Logical. Passed from function above.
#'
#' @return An arrow `Dataset` with additional geometry variable.
#'
#' @keywords internal
geometry <- function(df,
year = parent.frame()$year,
showProgress = parent.frame()$showProgress){

# df <- censobr::read_tracts(year = 2022, dataset = 'PessoaRenda')
# df <- dplyr::collect(df)

# download tracts geometry from geobr
tracts_sf <- geobr::read_census_tract(code_tract = 'all',
year = year,
showProgress = showProgress)

# hamonize col classes
df <- dplyr::mutate(df, code_muni = as.numeric(code_muni),
code_state = as.numeric(code_state))

if (year == 2010) {

# determine key vars
key_vars <- c("code_tract", "code_muni", "code_state")
}

# drop repeated vars
all_common_vars <- names(df)[names(df) %in% names(tracts_sf)]
vars_to_drop <- setdiff(all_common_vars, key_vars)
tracts_sf <- dplyr::select(tracts_sf, -all_of(vars_to_drop))

# merge
temp_sf <- dplyr::left_join(df, tracts_sf)

return(temp_sf)
}

0 comments on commit 87dd841

Please sign in to comment.