R/geocode_multiple_Colombia.R

Defines functions geocode_multiple_Colombia

Documented in geocode_multiple_Colombia

#' geocode in Colombia using the ESRI API geocoding server first
#'
#' This function allows you to geocode addresses in Colombia. The first attempt to geocode addresses is made using the ESRI
#' API geocoding server. If any address are not geocoded, then these directions are geocoded using the Google Maps API
#' (requires key). The result is a datframe with all the information recolected, specifying in column 'geocoded' which
#' server was used (1: ESRI, 2: Google Maps). A column for localidad is specified when the address is geocoded
#' using the Google Maps API, since the score of this geocoding process is not computated, so this may be the fastest way to
#' verify the results.
#'
#' @param df The dataframe containing the information.
#' @param key The google API key used for geocoding with the Google Maps API.
#' @param id_col The name of the column with the id for keeping track of your data (for each address).
#' @param join_id A string indicating the name of the column to join the results into the new dataframe.
#' @param direccion_gm The name of the column with the address in Google Maps format (addres, city, country)
#' @keywords geocode, Colombia, address, geocoding, Google Maps.
#' @return The coordinates of the address given.
#' @examples
#' ver <- geocode_multiple(bd, key)
#' @export

geocode_multiple_Colombia <- function(df, key, ..., id_col = NULL, join_id = NULL){

  if(is.null(id_col)){
    if("id" %in% names(df)){
      id_col = "id"
      warning('Se asume que la columna "id" es el identificador único de las direcciones. En caso contrario, especifíque el parámetro id_col')
    }
  }

  df2 <- df %>%
    geocode_Colombia(CRS = 4326, ...) %>%
    {. ->> df1} %>% filter(., is.na(lon_4326)) %>%
    geocode_google_Colombia(key = key, join_id = join_id) %>%
    full_join(., df1) %>%
    mutate(geocoded = case_when(!is.na(lon_4326) ~ 1,
                                !is.na(lon_gm) ~ 2,
                                T ~ NA_real_),
           lon = case_when(geocoded == 1 ~ lon_4326,
                           geocoded == 2 ~ lon_gm,
                           T ~ NA_real_),
           lat = case_when(geocoded == 1 ~ lat_4326,
                           geocoded == 2 ~ lat_gm,
                           T ~ NA_real_),
           matchAddr = case_when(geocoded == 1 ~ matchAddr,
                                 geocoded == 2 ~ direccion_gm,
                                 T ~ NA_character_)) %>%
    select(-ciudad_gm, -direccion_gm, -lat_gm, -lon_gm, -direccion_google) %>%
    {if(!is.null(id_col)) arrange(., .data[[id_col]]) else .}

  return(df2)

}
samesag/emu documentation built on Aug. 22, 2023, 3:19 p.m.