将revgeocode应用于经纬度坐标列表
我试图通过使用ggmap库中的revgeodcode函数来获取经纬度坐标(长)列表的邮政编码 我的问题和数据与此处相同:但接受的答案对我不适用 我的数据(.csv): 我遵循同样的步骤:将revgeocode应用于经纬度坐标列表,r,google-maps,loops,ggmap,R,Google Maps,Loops,Ggmap,我试图通过使用ggmap库中的revgeodcode函数来获取经纬度坐标(长)列表的邮政编码 我的问题和数据与此处相同:但接受的答案对我不适用 我的数据(.csv): 我遵循同样的步骤: data <- read.csv(file.choose()) dset <- as.data.frame(data[,2:3]) location = dset locaddr <- lapply(seq(nrow(location)), function(i){
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)
})
数据这里有很多错误
首先,将纬度和经度颠倒。数据集中指定的所有位置都位于南极洲
其次,revgeocode(…)
需要一个长度为2的数字向量,其中包含按该顺序排列的经度和纬度。您正在传递一个data.frame
对象(这是错误的原因),根据(1),它的顺序错误
第三,revgeocode(…)
使用谷歌地图api,它限制你每天查询2500次。所以如果你真的有一个大的数据集,祝你好运
此代码适用于您的示例:
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
data要提取邮政编码,只需写下:
>data$postal_code
我已经编写了使用有效API密钥访问google maps API的包。因此,如果您的数据超过2500项,您可以购买API密钥,然后使用googleway::google\u reverse\u geocode()
比如说
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
数据如果答案无效,您为什么接受它?你应该将此作为对答案的评论。我没有接受。手术室有。对不起,我还以为你也是那里的手术室。非常感谢你抽出时间来帮忙。我已经看到了它的工作原理并注意到了您的评论。我几乎复制了这个,但是得到了以下错误:“match.fun(fun)中的错误:缺少参数“fun”,没有默认值“@AgustínIndaco-我只是复制并粘贴了这个精确的代码,没有收到错误。您是否更改了代码中的其他内容?
>data$postal_code
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