Apply revgeocode to longitude and latitude coordinate list

I am trying to get the zip codes of a (long) longitude coordinate list using the revgeodcode function in the ggmap library.

My question and data are the same as here: Using the revgeocode function in a FOR loop. Help is needed , but the accepted answer does not work for me.

My details (.csv):

ID, Longitude, Latitude 311175, 41.298437, -72.929179 292058, 41.936943, -87.669838 12979, 37.580956, -77.471439 

I follow the same steps:

 data <- read.csv(file.choose()) dset <- as.data.frame(data[,2:3]) location = dset locaddr <- lapply(seq(nrow(location)), function(i){ revgeocode(location[i,], output = c("address"), messaging = FALSE, sensor = FALSE, override_limit = FALSE) }) 

... and get the error message: "Error: is.numeric (location) && length (location) == 2 is not TRUE" In particular, is.numeric (location) is FALSE, which seems strange because I can multiply by 2 and get the expected response.

Any help would be appreciated.

+8
loops r google-maps ggmap
source share
3 answers

There are a lot of things wrong.

Firstly, you have changed the latitude and longitude. All locations in your dataset, as indicated, are in Antarctica.

Secondly, revgeocode(...) expects a numerical vector of length 2 containing longitude and latitude in that order. You are passing a data.frame object (this is the cause of the error) and in accordance with (1) in the wrong order.

Thirdly, revgeocode(...) uses google api maps, which limits 2500 requests per day. Therefore, if you really have a large dataset, good luck with that.

This code works with your sample:

 data <- read.csv(text="ID, Longitude, Latitude 311175, 41.298437, -72.929179 292058, 41.936943, -87.669838 12979, 37.580956, -77.471439") library(ggmap) result <- do.call(rbind, lapply(1:nrow(data), function(i)revgeocode(as.numeric(data[i,3:2])))) data <- cbind(data,result) data # ID Longitude Latitude result # 1 311175 41.29844 -72.92918 16 Church Street South, New Haven, CT 06519, USA # 2 292058 41.93694 -87.66984 1632 West Nelson Street, Chicago, IL 60657, USA # 3 12979 37.58096 -77.47144 2077-2199 Seddon Way, Richmond, VA 23230, USA 

This extracts zipcodes:

 library(stringr) data$zipcode <- substr(str_extract(data$result," [0-9]{5}, .+"),2,6) data[,-4] # ID Longitude Latitude zipcode # 1 311175 41.29844 -72.92918 06519 # 2 292058 41.93694 -87.66984 60657 # 3 12979 37.58096 -77.47144 23230 
+10
source share

I wrote a googleway package to access google map APIs with a valid API key. Therefore, if your data exceeds 2500 units, you can pay for the API key, and then use googleway::google_reverse_geocode()

for example

 data <- read.csv(text="ID, Longitude, Latitude 311175, 41.298437, -72.929179 292058, 41.936943, -87.669838 12979, 37.580956, -77.471439") library(googleway) key <- "your_api_key" res <- apply(data, 1, function(x){ google_reverse_geocode(location = c(x["Latitude"], x["Longitude"]), key = key) }) ## Everything contained in 'res' is all the data returnd from Google Maps API ## for example, the geometry section of the first lat/lon coordiantes res[[1]]$results$geometry bounds.northeast.lat bounds.northeast.lng bounds.southwest.lat bounds.southwest.lng location.lat location.lng 1 -61.04904 180 -90 -180 -75.25097 -0.071389 location_type viewport.northeast.lat viewport.northeast.lng viewport.southwest.lat viewport.southwest.lng 1 APPROXIMATE -61.04904 180 -90 -180 
+2
source share

To extract the zip code, simply write:

 >data$postal_code 
-one
source share

All Articles