Increase the API Limit in Ggmap's Geocode Function (In R)

Increase the api limit in ggmap's geocode function (in R)

I didn't find a way to use the existing geocode function (from the ggmap library) to answer this question, so I just created a new function to just do this myself using the existing getURL function (from the RCurl library) and the fromJSON function (from the RJSONIO library).

Write the new function:

library(RJSONIO)
library(RCurl)

getGeoData <- function(location){
location <- gsub(' ','+',location)
geo_data <- getURL(paste("https://maps.googleapis.com/maps/api/geocode/json?address=",location,"&key=**[YOUR GOOGLE API KEY HERE]**", sep=""))
raw_data_2 <- fromJSON(geo_data)
return(raw_data_2)
}

Test:
getGeoData("San Francisco")

This gives you a list with the same data that's almost (but not quite) in the same exact format as the list produced by geocode("San Francisco").

How to geocode using Google API Key in R ggmap as of 2019

Have you tried this link here?

Note: it is written by SmartyStreets, a geocoding API, so it is biased. But it does compare and contrast some alternatives with their tradeoffs:

•Google

•TAMU

•Loqate

•Geocodio

•SmartyStreets

Also worth noting (link)

Geocode failed with status OVER_QUERY_LIMIT R studio

There can be two reasons for this :

1) You may have exceeded a 24-hour limit if you are using free version of google API. You can either go for premium service or check the limit policy for service which you are using here

2) You are crossing limit of requests per second by hitting geocode multiple times in one second . So you can change your code and add the wait time before next request to avoid this error.

Geocoding in R with Google Maps

Have you thought about using the json call instead? Looking at your code, you could achieve the same doing this (you'll need to install packages RCurl and RJSONIO from omegahat.com).

Copy and paste this into R:

library(RCurl)
library(RJSONIO)

construct.geocode.url <- function(address, return.call = "json", sensor = "false") {
root <- "http://maps.google.com/maps/api/geocode/"
u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")
return(URLencode(u))
}

gGeoCode <- function(address,verbose=FALSE) {
if(verbose) cat(address,"\n")
u <- construct.geocode.url(address)
doc <- getURL(u)
x <- fromJSON(doc,simplify = FALSE)
if(x$status=="OK") {
lat <- x$results[[1]]$geometry$location$lat
lng <- x$results[[1]]$geometry$location$lng
return(c(lat, lng))
} else {
return(c(NA,NA))
}
}

Here's how you use the above functions:

x <- gGeoCode("Philadelphia, PA")

This is the result you get. I think in the original code, lat and lng are mixed up? But hopefully this is what you want:

> x
[1] 39.95233 -75.16379

Hope that helps a little mate,

Tony Breyal

ggmap geocode function returning NA's

Removing the spaces before the commas might help:

dataset <- read.table(text ="fullAddress
'109 Bennett Dr, Caribou, ME 4736'
'1201 S Main St, Ann Arbor, MI 48104'
'2679 Ann Arbor Saline Rd, Ann Arbor, MI 48103'",
stringsAsFactors = F, header = T, quote = "'", sep = ",")

If I call mutate_geocode() on the first row I get the desired results:

                       fullAddress      lon      lat
1 109 Bennett Dr, Caribou, ME 4736 -68.0041 46.86837

If you keep getting geocode failed with status OVER_QUERY_LIMIT errors, then check this other question: Getting OVER QUERY LIMIT after one request with geocode

Loop for Reverse Geocoding in R

Based on this answer, you could create new variables in your data.frame

We use mapply() to process your coordinates and return the results in a list res.

res <- mapply(FUN = function(lon, lat) { 
revgeocode(c(lon, lat), output = "more")
},
df$lon, df$lat
)

Then, we use rbindlist() from data.table to convert the list into a data.frame (with fill = TRUE since not all elements of res have the same lenghts i.e. some results do not return a street_number and a postal_code) and cbind() it to the original data

cbind(df, data.table::rbindlist(res, fill = TRUE))

Update

Following up on your comment, if you want to process more than 2500 queries, you could subscribe to Google Maps APIs Premium Plan to unlock higher quotas. Then you can pass on your credentials to revgeocode() using the signature and client parameter.

As per mentionned in the documentation:

Upon purchasing your Google Maps APIs Premium Plan license, you will
receive a welcome email from Google that contains your client ID and
your private cryptographic key.

Your client ID is used to access the special features of Google Maps
APIs Premium Plan. All client IDs begin with a gme- prefix. Pass your
client ID as the value of the client parameter. A unique digital
signature is generated using your private cryptographic key. Pass this
signature as the value of the signature parameter.

You can see how it works under the hood by examining the revgeocode() source and see how the URL is constructed:

sensor4url <- paste("&sensor=", sensor, sep = "")
client4url <- paste("&client=", client, sep = "")
signature4url <- paste("&signature=", signature, sep = "")
url_string <- paste("http://maps.googleapis.com/maps/api/geocode/json?latlng=",
loc4url, sensor4url, sep = "")
if (userType == "business") {
url_string <- paste(url_string, client4url, signature4url,
sep = "")
}

Data

df <- structure(list(lat = c(32.31, 32.19, 34.75, 35.09, 35.35, 34.74 ), lon = 
c(119.827, 119.637, 119.381, 119.364, 119.534, 119.421 )), .Names =
c("lat", "lon"), row.names = c(21L, 32L, 37L, 48L, 50L, 89L), class = "data.frame")


Related Topics



Leave a reply



Submit