What's the complete input I must set up for geocoding Colombian addresses using HERE with R

Viewed 31

I'm trying to geocode, aka obtaining latitude-longitude, for 8K Colombian addresses using here.com. Below I'm sharing an example of a simple address, could someone please advise on what else am I missing and how to better construct my full address to get right lat-lon pairs? enter image description here

Have used below code:


dist.pairs <- readxl::read_excel("INFORME CONTRATO.XLSX", 
                                  sheet = NULL) 
dist.pairs$id <- 1:nrow(dist.pairs)

set_key(api_key = "KEY")
names(dist.pairs)

# "label": "5 Rue Daunou, 75002 Paris, France",
# "countryCode": "FRA",
# "countryName": "France",
# "stateCode": "IDF",
# "state": "Île-de-France",
# "county": "Paris",
# "city": "Paris",
# "district": "2e Arrondissement",
# "street": "Rue Daunou",
# "postalCode": "75002",
# "houseNumber": "5"

dist.pairs$country <- "Colombia" 
dist.pairs <- dist.pairs %>%                         
                  unite(fulladdress,                                                Dirección, Ciudad, Departamento, country,    
                         sep = ", ",                       
                         remove = F,                       
                         na.rm = T)                       

dist.pairs$geo.id <- 1:nrow(dist.pairs)

  split.timer <- Sys.time()

  dist.pairs.geocode <- geocode(address = dist.pairs$fulladdress) %>%       
                  mutate(Check.geo.id = dist.pairs$geo.id[id]) %>%   
                  mutate(Lat = st_coordinates(.)[,2], 
                         Lon = st_coordinates(.)[,1],                   
                         Found = TRUE) %>%       
                  select(Check.geo.id, Found, everything()) %>%         
                  st_drop_geometry(.)                            
  
  names(dist.pairs.geocode) <- ifelse(
                               substr(names(dist.pairs.geocode),1,5)=="Check",
                               names(dist.pairs.geocode),
                               paste0("Geocode_",names(dist.pairs.geocode))
                               )
    dist.pairs <- dist.pairs %>% 
                    left_join(x = ., 
                              y = dist.pairs.geocode, 
                              by = c("geo.id" = "Check.geo.id")) %>% 
                    mutate(Geocode_Found = ifelse(!is.na(Geocode_Found), T, F)) %>% 
                    select(-Geocode_id)
  
  print(paste0("Time taken to geocode: ",round(Sys.time()-split.timer,2)))
1 Answers

The geocode API provide the option to formulate a qualified query. Qualified address input for the /geocode endpoint is an option when the address text is captured in separate fields. For instance, as part of a sign-up process or whenever structured input is a requirement.

For example, an address "425 W Randolph St, Chicago, IL 60606, United States" can be qualified as the following set of sub-parameters:

houseNumber=425 street=W Randolph St city=Chicago state=IL postalCode=60606 country=United States Note that country in this case is a qualified field of an address, which supports a single country name or upper-case country code. To limit the results by a list of upper-case ISO 3166-1 alpha-3 country codes, please use the in parameter.

The query can be formulated with the following:

GET https://geocode.search.hereapi.com/v1/ geocode ?qq= houseNumber=425; street=W+Randolph+St; city=Chicago; state=IL; postalCode=60606; country=United+States &apiKey={YOUR_API_KEY}

Please follow the below document for more details. https://developer.here.com/documentation/geocoding-search-api/dev_guide/topics-api/code-geocode-qualified.html

Related