【问题标题】:Applying revgeocode to a list of longitude-latitude coordinates将 revgeocode 应用于经纬度坐标列表
【发布时间】:2014-05-19 15:36:22
【问题描述】:

我正在尝试使用 ggmap 库中的 revgeodcode 函数获取(长)经纬度坐标列表的邮政编码。

我的问题和数据与此处相同:Using revgeocode function in a FOR loop. Help required 但接受的答案对我不起作用。

我的数据(.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 并得到预期的答案。

任何帮助将不胜感激。

【问题讨论】:

  • 如果答案不起作用,您为什么接受它?您应该将此作为对答案的评论发布。
  • 我没有接受。操作做到了。
  • 对不起,我以为你也是那里的 OP。

标签: r google-maps loops ggmap


【解决方案1】:

我已经编写了包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

【讨论】:

  • 我几乎复制了这个,但得到以下错误:“match.fun(FUN) 中的错误:缺少参数“FUN”,没有默认值”
  • @AgustínIndaco - 我刚刚复制并粘贴了这个确切的代码,没有收到错误。您是否更改了代码中的其他内容?
【解决方案2】:

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

>data$postal_code

【讨论】:

    【解决方案3】:

    这里有很多问题。

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

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

    【讨论】:

    • 非常感谢您抽出宝贵时间提供帮助。我已经看到它有效并注意到了您的 cmets。
    猜你喜欢
    • 2021-04-15
    • 1970-01-01
    • 2010-09-07
    • 2013-02-14
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多