【问题标题】:Geocode in R with Bing使用 Bing 在 R 中进行地理编码
【发布时间】:2016-05-23 03:45:51
【问题描述】:

我想使用 Bing API 进行批量地理编码。我在 github 上的 https://github.com/gsk3/taRifx.geo/blob/master/R/Contributed.R 上找到了一个包,但是,我似乎无法弄清楚如何在向量上使用它的功能。当我尝试以下一切工作正常:

devtools::install_github("gsk3/taRifx.geo")
options(BingMapsKey='my_APIKEY')
geocode("New York,NY", service="bing", returntype="coordinates")

但如果我用位置向量替换位置,则只返回一个坐标对并返回以下警告:

Warning message: In if (is.na(x) | gsub(" *", "", x) == "") return(c(NA, NA)) : the condition has length > 1 and only the first element will be used

我还尝试了以下替代函数,但同样只返回一对坐标。使用 ggmap 的geocode,我可以在向量上运行此过程,但它的 API 限制为每天 2500 次查询,所以我一直在尝试使用 Bing(雅虎 api 太难获得)。

bGeoCode <- function(str, BingMapsKey){
    require(RCurl)
    require(RJSONIO)
    u <- URLencode(paste0("http://dev.virtualearth.net/REST/v1/Locations?q=", str, "&maxResults=1&key=", BingMapsKey))
    d <- getURL(u)
    j <- fromJSON(d,simplify = FALSE) 
    if (j$resourceSets[[1]]$estimatedTotal > 0) {
      lat <- j$resourceSets[[1]]$resources[[1]]$point$coordinates[[1]]
      lng <- j$resourceSets[[1]]$resources[[1]]$point$coordinates[[2]]
    }
    else {    
      lat <- lng <- NA
    }
    c(lat,lng)
}  

bGeoCode(data$location, "my_APIKEY")

非常感谢任何帮助。

【问题讨论】:

  • lapply(data$location, geocode, service = 'bing', returntype = 'coordinates') 之类的东西循环它。或者,查看ggmap::geocode,它可以采用向量,例如ggmap::geocode(c('dc', 'nyc', 'la'))

标签: r bing geocode


【解决方案1】:

在没有使用相关包的情况下,geocode 函数似乎没有矢量化。一个快速的解决方法是使其矢量化:

geocodeVect <- Vectorize(geocode, vectorize.args="x")
geocodeVect(multiple_locations, ...)

【讨论】:

  • 谢谢你似乎做到了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
  • 2011-03-16
  • 2022-06-21
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多