【发布时间】: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'))