rloopsggmaprgooglemapsgoogleway

Problems with reverse geocoding loops with latitude longitude co-ordinates using googleway: r gives the same results for different co-ordinates


Here is my sample dataset (called origAddress):

lat  lng
1.436316  103.8299
1.375093  103.8516
1.369347  103.8398
1.367353  103.8426

I have many more rows of latitude and longitude numbers (330) and I would like to find the address. I have used this for loop to do that:

for(i in 1:nrow(origAddress))
{
  # Print("Working...")
  result <- google_reverse_geocode(location = c(origAddress$lat[i],origAddress$lng[i]), 
  key = key,
  location_type = "rooftop")
  if(is.null(result) || length(dim(result)) < 2 || !nrow(result)) next
  origAddress$venadd <- geocode_address(result)
}

It works for the first three or four rows but then returns the same address as the first row although the latitude and longitude numbers are definitely different. I have looked at other stackoverflow questions(here) and tried to copy their approach with similar bad results.

Please help!


Solution

  • It looks like the calls to google_geocode can return more than one address for each lat/longitude pair thus you could be overwriting your data in the output data frame.
    Also, I am not sure your if statement is evaluating properly.
    Here is my attempt on your problem:

    library(googleway)
    
    origAddress<-read.table(header = TRUE, text = "lat  lng
    1.436316  103.8299
    1.375093  103.8516
    1.369347  103.8398
    1.367353  103.8426")
    
    #add the output column
    origAddress$venadd<-NA
    
    for(i in 1:nrow(origAddress))
    {
      # Print("Working...")
      result <- google_reverse_geocode(location = c(origAddress$lat[i],origAddress$lng[i]), 
                                       key=key,
                                       location_type = "rooftop")
    
      #add a slight pause so not to overload the call requests
      Sys.sleep(1)
      if(result$status =="OK" ){
        #multiple address can be returned with in gecode request picks the first one
        origAddress$venadd[i] <- result$results$formatted_address[1]
        #use this to collect all addresses:
        #paste(result$results$formatted_address, collapse = "  ")
      }
    }
    

    Since the call to google_reverse_geocode returns the address, I just pull the first address from the result saving a call to the internet (performance improvement). Also since the call returns a status, I check for an OK and if exist save the first address.

    Hope this helps.