2

I get a random number of errors that come back as NA when I try to geocode a lot of places using rgooglemaps getGeoCode function. Can anyone tell me why? (Reproducible code below)

library(RgoogleMaps)
library(foreach)
###Replicating a large search data###
PlaceVector <- c(rep("Anchorage,Alaska", 20), rep("Baltimore,Maryland", 20), 
rep("Birmingham,Alabama", 20))
iters <- length(PlaceVector)
###Looping to get each geocode###
geoadd <- foreach(a=1:iters, .combine=rbind) %do% {
  getGeoCode(paste(PlaceVector[a]))
}
geoadd <- as.data.frame(geoadd)
geoadd$Place <- PlaceVector

I get a random number of errors, usually around 15 where the latitude and longitudes in data frame geoadd come back as NA. I could loop it back on the NA's but that seems utterly inefficient. Do others have the same problem with the sample code provided?

  • Same here, both on Windows and Ubuntu. In the latter case, the following warning is thrown: In min(iLng[iLng > iLoc]) : no non-missing arguments to min; returning Inf. – fdetsch Jan 10 '18 at 9:32
1

As described here, rate limiting can be circumvented by combining an outer loop structure (eg. for or *apply) with an inner while statement testing whether coordinates for a given location were successfully retrieved. In your case, this would look as follows:

geoadd <- foreach(a = 1:iters, .combine = rbind) %do% {
  gcd = getGeoCode(paste(PlaceVector[a]))

  while (all(is.na(gcd))) {
    gcd = getGeoCode(paste(PlaceVector[a]))
  }

  return(gcd)
}

all(!is.na(geoadd))
# [1] TRUE
  • 1
    I switched to the googleway package and life got a lot better – Neal Barsch Feb 26 '18 at 15:22
0

This is google's rate limiting.

If you run getGeoCode with verbose=1 it will show you the URL it is accessing:

> getGeoCode("Brooklyn",verbose=1)
fetching  http://maps.google.com/maps/api/geocode/xml?sensor=false&address=Brooklyn 
      lat       lon 
 40.67818 -73.94416 
> getGeoCode("Brooklyn",verbose=1)
fetching  http://maps.google.com/maps/api/geocode/xml?sensor=false&address=Brooklyn 
lat lon 
 NA  NA 
Warning messages:
1: In min(iLat[iLat > iLoc]) :
  no non-missing arguments to min; returning Inf
2: In min(iLng[iLng > iLoc]) :
  no non-missing arguments to min; returning Inf

Now we can see that is generating the error message but isn't very helpful. Let's get the URL using wget on the command line a few times and see what happens:

wget 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=Brooklyn'
wget 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=Brooklyn'
wget 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=Brooklyn'

(you get the picture).

This for me returns responses that are either 1625 bytes long or 334 bytes long. Hello, that's a bit weird, what's in the 334 byte response:

<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OVER_QUERY_LIMIT</status>
 <error_message>You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_</error_message>
</GeocodeResponse>

getGeoCode doesn't handle this gracefully, and insteads returns NAs and a non-informative warning.

Fix is to get a Google API key or use an alternative georeferencing system or figure out how long to wait between requests on the free API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.