Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ipeaGIT/geocodebr
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Dec 18, 2024
2 parents bbfe504 + 858be22 commit 3c2ca83
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
4 changes: 2 additions & 2 deletions R/download_cnefe.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' Defaults to `TRUE`.
#' @template cache
#'
#' @return A directory path where the data was saved.
#' @return Invisibly returns the path to the file where the data was saved.
#'
#' @family Support
#'
Expand Down Expand Up @@ -62,7 +62,7 @@ download_cnefe <- function(state = "all", progress = TRUE, cache = TRUE) {
grepl(glue::glue("estado=({paste(state, collapse = '|')})"), parquet_files)
]

return(state_parquet_files)
return(invisible(state_parquet_files))
}

assert_and_assign_state <- function(state) {
Expand Down
63 changes: 30 additions & 33 deletions R/geocode2.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ geocode2 <- function(addresses_table,
# include only the municipalities present in the input table, reducing the
# search scope and consequently reducing processing time and memory usage

download_cnefe(present_states, progress = progress, cache = cache)

present_states <- unique(standard_locations$estado_padr)

cnefe_path <- download_cnefe(
present_states,
progress = progress,
cache = cache
)
cnefe <- arrow::open_dataset(cnefe_path)
cnefe <- arrow::open_dataset(get_cache_dir())
cnefe <- dplyr::filter(cnefe, estado %in% present_states)

# creating a temporary db and registering both the input table and the cnefe
# data
Expand Down Expand Up @@ -97,7 +95,7 @@ geocode2 <- function(addresses_table,
"cep_padr", "cep",
"bairro_padr", "localidade",
"municipio_padr", "municipio",
"estado_padr", "estado" # INCLUDE, NOT IN CNEFE YET
"estado_padr", "estado"
)

lookup_vector <- equivalent_colnames$cnefe
Expand All @@ -106,18 +104,18 @@ geocode2 <- function(addresses_table,
# when merging the data, we have several different cases with different confidence
# levels. from best to worst, they are:
#
# - case 01: match municipio, logradouro, numero, cep, localidade
# - case 02: match municipio, logradouro, numero, cep
# - case 03: match municipio, logradouro, numero, localidade
# - case 04: match municipio, logradouro, cep, localidade
# - case 05: match municipio, logradouro, numero
# - case 06: match municipio, logradouro, cep
# - case 07: match municipio, logradouro, localidade
# - case 08: match municipio, logradouro
# - case 09: match municipio, cep, localidade
# - case 10: match municipio, cep
# - case 11: match municipio, localidade
# - case 12: match municipio
# - case 01: match estado, municipio, logradouro, numero, cep, localidade
# - case 02: match estado, municipio, logradouro, numero, cep
# - case 03: match estado, municipio, logradouro, numero, localidade
# - case 04: match estado, municipio, logradouro, cep, localidade
# - case 05: match estado, municipio, logradouro, numero
# - case 06: match estado, municipio, logradouro, cep
# - case 07: match estado, municipio, logradouro, localidade
# - case 08: match estado, municipio, logradouro
# - case 09: match estado, municipio, cep, localidade
# - case 10: match estado, municipio, cep
# - case 11: match estado, municipio, localidade
# - case 12: match estado, municipio

DBI::dbExecute(con, "ALTER TABLE standard_locations ADD COLUMN match_type VARCHAR DEFAULT NULL")
DBI::dbExecute(con, "ALTER TABLE standard_locations ADD COLUMN lon DOUBLE DEFAULT NULL")
Expand All @@ -128,8 +126,7 @@ geocode2 <- function(addresses_table,
n_rows_affected <- 0
}


for (case in 1:12) {
for (case in c(1, 2, 4:11)) {
relevant_cols <- get_relevant_cols(case)
formatted_case <- formatC(case, width = 2, flag = "0")

Expand Down Expand Up @@ -202,29 +199,29 @@ assert_address_fields <- function(address_fields, addresses_table) {

get_relevant_cols <- function(case) {
relevant_cols <- if (case == 1) {
c("municipio_padr", "logradouro_padr", "numero_padr", "cep_padr", "bairro_padr")
c("estado_padr", "municipio_padr", "logradouro_padr", "numero_padr", "cep_padr", "bairro_padr")
} else if (case == 2) {
c("municipio_padr", "logradouro_padr", "numero_padr", "cep_padr")
c("estado_padr", "municipio_padr", "logradouro_padr", "numero_padr", "cep_padr")
} else if (case == 3) {
c("municipio_padr", "logradouro_padr", "numero_padr", "bairro_padr")
c("estado_padr", "municipio_padr", "logradouro_padr", "numero_padr", "bairro_padr")
} else if (case == 4) {
c("municipio_padr", "logradouro_padr", "cep_padr", "bairro_padr")
c("estado_padr", "municipio_padr", "logradouro_padr", "cep_padr", "bairro_padr")
} else if (case == 5) {
c("municipio_padr", "logradouro_padr", "numero_padr")
c("estado_padr", "municipio_padr", "logradouro_padr", "numero_padr")
} else if (case == 6) {
c("municipio_padr", "logradouro_padr", "cep_padr")
c("estado_padr", "municipio_padr", "logradouro_padr", "cep_padr")
} else if (case == 7) {
c("municipio_padr", "logradouro_padr", "bairro_padr")
c("estado_padr", "municipio_padr", "logradouro_padr", "bairro_padr")
} else if (case == 8) {
c("municipio_padr", "logradouro_padr")
c("estado_padr", "municipio_padr", "logradouro_padr")
} else if (case == 9) {
c("municipio_padr", "cep_padr", "bairro_padr")
c("estado_padr", "municipio_padr", "cep_padr", "bairro_padr")
} else if (case == 10) {
c("municipio_padr", "cep_padr")
c("estado_padr", "municipio_padr", "cep_padr")
} else if (case == 11) {
c("municipio_padr", "bairro_padr")
c("estado_padr", "municipio_padr", "bairro_padr")
} else if (case == 12) {
c("municipio_padr")
c("estado_padr", "municipio_padr")
}

return(relevant_cols)
Expand Down
2 changes: 1 addition & 1 deletion R/geocodebr.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ cache_config_file <- fs::path(
utils::globalVariables(
c(
"year", "temp_local_file", "lon_min", "lon_max", "lat_min", "lat_max", ".",
"lon_diff", "lat_diff", "lon", "lat"
"lon_diff", "lat_diff", "lon", "lat", "estado"
)
)
2 changes: 1 addition & 1 deletion man/download_cnefe.Rd

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

0 comments on commit 3c2ca83

Please sign in to comment.