r
  • bing
  • geocode
  • 2016-05-23 18 views 1 likes 
    1

    我想使用必應API批量地理編碼。我在github上發現了一個包https://github.com/gsk3/taRifx.geo/blob/master/R/Contributed.R但是,我似乎無法弄清楚如何在矢量上使用它的函數。當我嘗試下面的一切工作只是罰款: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") 
    

    任何幫助,非常感謝。

    +0

    環路它像'lapply(數據$位置,地理編碼,服務=「兵」,返回類型=「座標」)'。或者,查看可以使用矢量的'ggmap :: geocode',例如'ggmap :: geocode(c('dc','nyc','la'))' – alistaire

    回答

    2

    如果未使用相關軟件包,則看起來geocode函數未被矢量化。速戰速決是使矢量化:

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

    謝謝,似乎這樣做! –

    相關問題