Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

r - Applying revgeocode to a list of longitude-latitude coordinates

I'm trying to get the Zip codes of a (long) list of Longitude Latitude coordinates by using the revgeodcode function in the ggmap library.

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

My data (.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" Specifically, is.numeric(location) is FALSE, which seems strange because I can multiply by 2 and get the expected answer.

Any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are lots of things wrong here.

First, you have latitude and longitude reversed. All the locations in your dataset, as specified, are in Antarctica.

Second, revgeocode(...) expects a numeric vector of length 2 containing the longitude and latitude in that order. You are passing a data.frame object (this is the reason for the error), and as per (1) it's in the wrong order.

Third, revgeocode(...) uses the google maps api, which limits you to 2500 queries a day. So if you really do 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 the 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...