Skip to content

Commit

Permalink
reverse geocoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Dec 15, 2024
1 parent c11c45e commit 147ff88
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(download_cnefe)
export(geocode)
export(get_cache_dir)
export(list_cached_data)
export(reverse_geocode)
export(set_cache_dir)
export(setup_address_fields)
importFrom(data.table,"%chin%")
Expand Down
13 changes: 7 additions & 6 deletions tests/tests_rafa/reverse_geocode.R → R/reverse_geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
#' closely matches the input coordinates.
#'
#' @param input_table A data frame. It must contain the columns `'id'`, `'lon'`, `'lat'`
#' @param ncores Number of cores to be used in parallel execution. Defaults to
#' the number of available cores minus 1.
#' @param progress A logical. Whether to display a download progress bar.
#' Defaults to `TRUE`.
#' @template ncores
#' @template progress
#' @template cache
#'
#' @return A `"data.frame"` object.
Expand Down Expand Up @@ -57,14 +55,17 @@ reverse_geocode <- function(input_table,
# prep input -------------------------------------------------------

# create a small range around coordinates

data.table::setDT(input_table)
margin <- 0.001 # 0.0001
input_table[, lon_min := lon - margin]
input_table[, lon_max := lon + margin]
input_table[, lat_min := lat - margin]
input_table[, lat_max := lat + margin]


# Narrow search scope in cnefe to bounding box

bbox_lon_min <- min(input_table$lon_min)
bbox_lon_max <- max(input_table$lon_max)
bbox_lat_min <- min(input_table$lat_min)
Expand All @@ -83,7 +84,7 @@ reverse_geocode <- function(input_table,



# comeca cada linha -------------------------------------------------------
# find each row in the input data -------------------------------------------------------

# function to reverse geocode one row of the data
reverse_geocode_single_row <- function(df, row_number){
Expand Down Expand Up @@ -134,7 +135,7 @@ reverse_geocode <- function(input_table,
return(temp_output)
}


# apply function to all rows in the input table
output <- pbapply::pblapply(
X = 1:nrow(input_table),
FUN = reverse_geocode_single_row,
Expand Down
46 changes: 46 additions & 0 deletions man/reverse_geocode.Rd

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

0 comments on commit 147ff88

Please sign in to comment.