Search code examples
rgeocodinggoogleway

Error in batch geocoding using R googleway


Not sure why I keep getting this error:

Error in value[3L] : There was an error downloading results. Please manually check the following URL is valid by entering it into a browswer. If valid, please file a bug report citing this URL (note: your API key has been removed, so you will need to add that back in)

https://maps.googleapis.com/maps/api/geocode/json?&address=#211.+226+park+street,+brockville,+on,+k6v2h5&key=

 #A look at my dataframe called subset:

 ID<- c("XM-7393","XM-7138")
 Address<- c("175 College St #450, Toronto, ON M5T 1P7" ,"250 College St, Toronto, ON M5T 1R8")
 subset<-data.frame(ID,Address)
 subset$Address<- as.character(subset$Address)
  require(googleway) #using google to get coordinates
  gkey<-"INSERT GOOGLE API KEY HERE" #google API Key needed to get lat/lon coordinates

  #a lat and lon vector to store the coordinates from the geocode
  lat = vector("numeric", length = nrow(subset))
  lng = vector("numeric", length = nrow(subset))


  #Function for batch geocoding a list of addresses in a dataframe
  for (i in 1:nrow(subset)) {
  coord = googleway::google_geocode(subset$Address[i], key=gkey)

  if (coord$status == "OK") {
coord = googleway::geocode_coordinates(coord)
lat[i] = coord$lat[1]  # sometimes returns multiple coordinates
lng[i] = coord$lng[1]  # sometimes returns multiple coordinates
} else {
lat[i] = NA
lng[i] = NA
}

}



 #adding the lat and lon coordinates to subset dataset
  subset$lat = lat
  subset$lng = lng

Ok the code above works! But only if the data set does not have that many observations. The original dataset I was working with had 1000 observations and I know that I am not near my API limit. So not to sure why it won't work for when I have a 1000 observation dataset.

ANSWER: Some address fields had '#" to represent unit number. This needs to be removed (see comment below!)


Solution

  • You want to check that you don't have any illegal or reserved characters in your addresses, since any geocoding functions will be using your text to create URLs to query the geocoding API. google_geocode on its own doesn't give a very helpful error message, but by looking at the URL you posted above, the error message shows that the required parameters weren't included.

    In this case, # has a special meaning in URLs, so you'll get an error. FYI, my Google API key is saved as the environment variable GOOGLE_KEY:

    library(googleway)
    
    Address <- c("175 College St #450, Toronto, ON M5T 1P7", "250 College St, Toronto, ON M5T 1R8")
    set_key(Sys.getenv("GOOGLE_KEY"))
    
    geocode_results <- lapply(Address, google_geocode)
    sapply(geocode_results, function(x) x[["status"]])
    #> [1] "OVER_QUERY_LIMIT" "OK"
    

    The first address has an error; it also has a # character. Lists of reserved characters are around, including this language-agnostic SO question. Using a regex pattern, I'm removing any #, (, or ) characters that may be in the addresses, then trying again at the geocoding.

    clean_addresses <- gsub(pattern = "[#\\(\\)]", replacement = "", Address)
    geocode_cleaned <- lapply(clean_addresses, google_geocode)
    sapply(geocode_cleaned, function(x) x[["status"]])
    #> [1] "OK" "OK"