Reverse geocoding loop in R

I am trying to change the geocode of a large data set (about 100 thousand). I used the revgeocode function from the ggmap package. I got the result for 1 entry

 48 Grand View Terrace, San Francisco, CA 94114, USA 48 Grand View Terrace Eureka Valley San Francisco San Francisco County California United States postal_code postal_code_suffix 

but I need to automate the process and use it for the entire data set.

I tried

 r <- lapply(revgeocode(location = (c(z$lon),c(z$lat)), output = "more", messaging = FALSE, sensor = FALSE, override_limit = FALSE, client = "", signature = "")) 

and got errors for unexpected "," at every step.

I also tried writing the following loop

 r <- for(i in 1:10){ revgeocode(location = ("z$lon", "z$lat"),output = "more", messaging = FALSE, sensor = FALSE, override_limit = FALSE,client = "", signature = "")} 

and got similar errors

Please indicate some useful or useful links that will help me write a loop for reverse geocoding. How to check the accuracy of the data?

+8
r reverse-geocoding ggmap
source share
2 answers

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

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

 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 to data.frame (with fill = TRUE , since not all res elements have the same lengths, i.e. some results do not return street_number and postal_code ) and cbind() to the original data

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

Update

Following your comment, if you want to process more than 2500 requests , you can sign up for a premium Google Maps API plan to unlock higher quotas. You can then pass your revgeocode() credentials using the signature and client options.

As mentioned in the documentation :

By purchasing a license for the premium Google Maps API Maps plan, you will receive a welcome email from Google that contains your customer ID and your secret key.

The customer ID is used to access the special features of the Google Maps API Premium Plan. All customer IDs begin with the gme prefix. Pass the client ID as the value of the client parameter. A unique digital signature is created 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") 
+4
source share

I had a problem with a similar API key integration. Basically it is a matter of integrating the API key into the URL that calls R. If this does not help you, you need to change the main code (see it on Github) to allow the argument that calls the key.

0
source share

All Articles