diff --git a/R/create_geocodebr_db.R b/R/create_geocodebr_db.R index e5d0f8f..5858e5d 100644 --- a/R/create_geocodebr_db.R +++ b/R/create_geocodebr_db.R @@ -57,6 +57,10 @@ create_geocodebr_db <- function(db_path = tempdir(), # DBI::dbExecute(con, "SET memory_limit = '20GB'") + if (DBI::dbExistsTable(con, 'cnefe')){ + duckdb::duckdb_unregister_arrow(con, 'cnefe') + } + # Load CNEFE data and write it to DuckDB cnefe <- arrow_open_dataset(cache_dir) duckdb::duckdb_register_arrow(con, "cnefe", cnefe) diff --git a/R/geocode.R b/R/geocode.R index 7412495..b7737f7 100644 --- a/R/geocode.R +++ b/R/geocode.R @@ -1,8 +1,8 @@ -#' Download microdata of population records from Brazil's census +#' Geocoding addresses based on CNEFE data #' #' @description -#' Download microdata of population records from Brazil's census. Data collected -#' in the sample component of the questionnaire. +#' Takes a data frame containing addresses as an input and returns the spatial +#' coordinates found based on CNEFE data. #' #' @param input_table A data frame. #' @param logradouro A string. @@ -26,16 +26,16 @@ #' data_path <- system.file("extdata/sample_1.csv", package = "geocodebr") #' input_df <- read.csv(data_path) #' -#' # df <- geocodebr::geocode( -#' # input_table = input_df, -#' # logradouro = "nm_logradouro", -#' # numero = "Numero", -#' # complemento = "Complemento", -#' # cep = "Cep", -#' # bairro = "Bairro", -#' # municipio = "nm_municipio", -#' # estado = "nm_uf" -#' # ) +#' df <- geocodebr::geocode( +#' input_table = input_df, +#' logradouro = "nm_logradouro", +#' numero = "Numero", +#' complemento = "Complemento", +#' cep = "Cep", +#' bairro = "Bairro", +#' municipio = "nm_municipio", +#' estado = "nm_uf" +#' ) #' geocode <- function(input_table, logradouro = NULL, @@ -657,6 +657,7 @@ geocode <- function(input_table, # Disconnect from DuckDB when done duckdb::dbDisconnect(con, shutdown=TRUE) + gc() # Return the result return(output_deterministic) diff --git a/R/reverse_geocode.R b/R/reverse_geocode.R index dd20535..d998c73 100644 --- a/R/reverse_geocode.R +++ b/R/reverse_geocode.R @@ -1,8 +1,10 @@ -#' Reverse geocoding +#' Reverse geocoding coordinates based on CNEFE data #' #' @description -#' Reverse geocode coordinate pairs to find the address in CNEFE that most -#' closely matches the input coordinates. +#' Takes a data frame containing coordinates (latitude and longitude) and +#' returns the address in CNEFE that is the closest to the input coordinates. +#' Latitude and longitude inputs are limited to possible values. Latitudes must +#' be between -90 and 90 and longitudes must be between -180 and 180. #' #' @param input_table A data frame. It must contain the columns `'id'`, `'lon'`, `'lat'` #' @template ncores @@ -15,14 +17,17 @@ #' @examplesIf identical(tolower(Sys.getenv("NOT_CRAN")), "true") #' #' # input data -#' lonlat_df <- data.frame( +#' df_coords <- data.frame( #' id = 1:6, #' lon = c(-67.83112, -67.83559, -67.81918, -43.47110, -51.08934, -67.8191), #' lat = c(-9.962392, -9.963436, -9.972736, -22.695578, -30.05981, -9.97273) #' ) #' #' # reverse geocode -#' df_addresses <- geocodebr::reverse_geocode(input_table = lonlat_df) +#' df_addresses <- geocodebr::reverse_geocode( +#' input_table = df_coords, +#' progress = TRUE +#' ) #' reverse_geocode <- function(input_table, progress = TRUE, @@ -146,6 +151,7 @@ reverse_geocode <- function(input_table, # Disconnect from DuckDB when done duckdb::dbDisconnect(con, shutdown=TRUE) + gc() return(output) } diff --git a/man/geocode.Rd b/man/geocode.Rd index 240b581..3fb78ed 100644 --- a/man/geocode.Rd +++ b/man/geocode.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/geocode.R \name{geocode} \alias{geocode} -\title{Download microdata of population records from Brazil's census} +\title{Geocoding addresses based on CNEFE data} \usage{ geocode( input_table, @@ -55,8 +55,8 @@ the function will download the data again and overwrite the local files.} An arrow \code{Dataset} or a \code{"data.frame"} object. } \description{ -Download microdata of population records from Brazil's census. Data collected -in the sample component of the questionnaire. +Takes a data frame containing addresses as an input and returns the spatial +coordinates found based on CNEFE data. } \examples{ \dontshow{if (identical(tolower(Sys.getenv("NOT_CRAN")), "true")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} @@ -65,16 +65,16 @@ in the sample component of the questionnaire. data_path <- system.file("extdata/sample_1.csv", package = "geocodebr") input_df <- read.csv(data_path) -# df <- geocodebr::geocode( -# input_table = input_df, -# logradouro = "nm_logradouro", -# numero = "Numero", -# complemento = "Complemento", -# cep = "Cep", -# bairro = "Bairro", -# municipio = "nm_municipio", -# estado = "nm_uf" -# ) +df <- geocodebr::geocode( + input_table = input_df, + logradouro = "nm_logradouro", + numero = "Numero", + complemento = "Complemento", + cep = "Cep", + bairro = "Bairro", + municipio = "nm_municipio", + estado = "nm_uf" + ) \dontshow{\}) # examplesIf} } \concept{Microdata} diff --git a/man/reverse_geocode.Rd b/man/reverse_geocode.Rd index b58f368..c1b983e 100644 --- a/man/reverse_geocode.Rd +++ b/man/reverse_geocode.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/reverse_geocode.R \name{reverse_geocode} \alias{reverse_geocode} -\title{Reverse geocoding} +\title{Reverse geocoding coordinates based on CNEFE data} \usage{ reverse_geocode(input_table, progress = TRUE, ncores = NULL, cache = TRUE) } @@ -26,21 +26,26 @@ the function will download the data again and overwrite the local files.} A \code{"data.frame"} object. } \description{ -Reverse geocode coordinate pairs to find the address in CNEFE that most -closely matches the input coordinates. +Takes a data frame containing coordinates (latitude and longitude) and +returns the address in CNEFE that is the closest to the input coordinates. +Latitude and longitude inputs are limited to possible values. Latitudes must +be between -90 and 90 and longitudes must be between -180 and 180. } \examples{ \dontshow{if (identical(tolower(Sys.getenv("NOT_CRAN")), "true")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # input data -lonlat_df <- data.frame( +df_coords <- data.frame( id = 1:6, lon = c(-67.83112, -67.83559, -67.81918, -43.47110, -51.08934, -67.8191), lat = c(-9.962392, -9.963436, -9.972736, -22.695578, -30.05981, -9.97273) ) # reverse geocode -df_addresses <- geocodebr::reverse_geocode(input_table = lonlat_df) +df_addresses <- geocodebr::reverse_geocode( + input_table = df_coords, + progress = TRUE + ) \dontshow{\}) # examplesIf} } \concept{Reverse geocoding}