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 is not TRUE” 具体来说,is.numeric(location) 为 FALSE,这看起来很奇怪,因为我可以乘以 2并得到预期的答案。

任何帮助,将不胜感激。

4

3 回答 3

12

这里有很多问题。

首先,您将纬度和经度颠倒过来。指定的数据集中的所有位置都在南极洲。

其次,revgeocode(...)需要一个长度为 2 的数字向量,其中包含按该顺序排列的经度和纬度。您正在传递一个data.frame对象(这是错误的原因),并且根据(1)它的顺序错误。

第三,revgeocode(...)使用 google maps 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
于 2014-04-07T17:48:23.583 回答
2

我已经编写了包googleway以使用有效的 API 密钥访问谷歌地图 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
于 2016-06-25T02:08:17.747 回答
-2

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

>data$postal_code
于 2015-05-01T13:34:52.293 回答