【问题标题】:inconsistent result with INVALID_REQUEST in R ggmap geocode()R ggmap geocode() 中与 INVALID_REQUEST 不一致的结果
【发布时间】:2015-08-01 17:27:10
【问题描述】:

我正在尝试对地址列表进行地理编码,但我遇到了一些 INVALID_REQUEST 错误,但我不知道为什么。看看这个:

# First check if I have permission:
geocodeQueryCheck()
2478 geocoding queries remaining.

# Enter data
d <- c("Via del Tritone 123, 00187 Rome, Italy",
       "Via dei Capocci 4/5, 00184 Rome, Italy")

# Ensure it's a character vector
class(d)
[1] "character"

# Try to geocode
library(ggmap)
geocode(d)
   lon      lat
1       NA       NA
2 12.49324 41.89582
Warning message:
geocode failed with status INVALID_REQUEST, location = "Via del Tritone 123, 00187 Rome, Italy" 

# Obtain an error, but if I try directly:
geocode("Via del Tritone 123, 00187 Rome, Italy")
   lon      lat
1 12.48813 41.90352

# It works. What gives?

【问题讨论】:

  • 它对我有用:packageVersion("ggmap") [1] ‘2.4’
  • 我也有 v.2.4。加载直接地理编码后,它会将结果缓存在某处,因此geocode(d) 不会返回任何警告...geocode(d) 是否立即为您工作?
  • 正如我之前提到的,它对我有用。在新会话中尝试。

标签: r geocode ggmap


【解决方案1】:

A similar issue 已被报告为 RgoogleMaps::getGeoCode(),这与 Google 的速率限制有关。由于geocode() 还依赖于 Google Maps API(除非source = "dsk"),因此此限制也可能会导致问题。

您可以通过遍历所有感兴趣的位置(例如使用for*apply)轻松解决这个“顽固”的问题,而不是一次将一个大的地址向量传递给geocode。在循环内部,您可以使用while 来检测是否已成功检索到当前处理位置的坐标,如果没有,只需重复地理编码过程直到成功。

out = lapply(d, function(i) {
  gcd = geocode(i)

  while (all(is.na(gcd))) {
    gcd = geocode(i)
  }

  data.frame(address = i, gcd)
})

例如,在我上一次测试运行期间,检索失败了 3 次,如以下警告所示(这在您的机器上可能看起来不同):

Warning messages:
1: geocode failed with status OVER_QUERY_LIMIT, location = "Via del Tritone 123, 00187 Rome, Italy" 
2: geocode failed with status OVER_QUERY_LIMIT, location = "Via del Tritone 123, 00187 Rome, Italy" 
3: geocode failed with status OVER_QUERY_LIMIT, location = "Via dei Capocci 4/5, 00184 Rome, Italy" 

尽管如此,感谢外部循环结构中包含的while 条件,最终成功检索到所有感兴趣位置的坐标:

> do.call(rbind, out)
                                 address      lon      lat
1 Via del Tritone 123, 00187 Rome, Italy 12.48766 41.90328
2 Via dei Capocci 4/5, 00184 Rome, Italy 12.49321 41.89582

作为额外的享受,这种“固执”的方法可以轻松并行运行(例如,使用 parLapply()foreach()),这可能会在查询大量地址时显着提高速度。

【讨论】:

    猜你喜欢
    • 2012-11-13
    • 1970-01-01
    • 2011-09-18
    • 2015-02-04
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多