将逆地理编码应用于经度-纬度坐标列表

9
我正在尝试使用ggmap库中的revgeodcode函数获取一长串经纬度坐标的邮政编码。
我的问题和数据与此处相同:在FOR循环中使用revgeocode函数。需要帮助,但是被接受的答案对我不起作用。
我的数据(.csv):
ID,      Longitude,      Latitude
311175,  41.298437,      -72.929179
292058,  41.936943,      -87.669838
12979,   37.580956,      -77.471439

我跟随相同的步骤:

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)
               })

...并出现错误信息:"错误:is.numeric(location)&&length(location)== 2不为TRUE" 具体来说,is.numeric(location)为FALSE,这似乎很奇怪,因为我可以乘以2并获得预期的答案。

希望能得到任何帮助。


如果答案不起作用,为什么要接受它?你应该将其作为评论发布到答案中。 - jlhoward
我没有接受它,是操作员接受了。 - Rutger
抱歉,我以为你也是原帖作者。 - jlhoward
3个回答

12

这里有很多问题。

首先,你把纬度和经度弄反了。你数据集中的所有位置都是在南极洲。

其次,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

这将提取邮政编码:

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

非常感谢您抽出时间来帮助我。我已经看到它可行,并注意到了您的评论。 - Rutger

2
我编写了 googleway 这个软件包来使用有效的 API 密钥访问 Google 地图 API。因此,如果您的数据超过 2,500 个项目,您可以购买一个 API 密钥,然后使用 googleway::google_reverse_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ín Indaco
@AgustínIndaco - 我刚刚复制并粘贴了这段完全相同的代码,没有收到任何错误。你在代码中改变了其他什么吗? - SymbolixAU

-2

要提取邮政编码,只需写下:

>data$postal_code

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接