2017-08-16 12 views
-1

googlewayパッケージのgoogle_geocode関数を使用してジオコードをバッチする際に問題があります。私はアドレスのデータフレームを入力し、それぞれの緯度と経度の座標を返すようにしたいと思います。住所の数は、Googleの日々のクエリの上限2,500をはるかに超えているため、ソリューションではより多くのクエリを購入できるようにAPIキーを使用する必要があります。googlewayによるバッチジオコーディングR

## Your Google API key 
key<-"<insert key here>" 
### Make Data Frame with two observations 
Dt<-as.data.frame(matrix(c(" 4605 Langdon ST , Fernley , NV , 89408", -119.2026, 
      " 350 Quivera LN , Sparks , NV , 89441", NA), ncol=2)) 
###Change Column Names 
colnames(Dt)<-c("address", "longitude") 

### Make address column character 
Dt$address<-as.character(Dt$address) 

### Make data frame with one observation 
dt<-Dt[1,] 


### geocode one observation with googleway This Works!! 
google_geocode(address = dt[,"address"], 
      key = key) 

### batch geocode 
res <- apply(Dt, 1, function(Dt){ 

google_geocode(address=list(Dt[,"address"]), 
       key = key) 
}) 

## Error in Dt[, "address"] : incorrect number of dimensions 
+0

あなたの質問は完全には明確ではありません。起こったことを反映するために質問を更新してください。 Googleの限界を迂回する方法を見つけようとしている場合は、事前に困難な時間がかかる可能性があります。 –

+0

私はGoogleの制限を回避するつもりはないので、APIキーを使用するソリューションが必要なため、1日に2,500件以上の結果を受信して​​請求することができます。申し訳ありませんが、私はバッチジオコーディングが私の必要性を説明していると思いました。私はデータフレームに格納された数千のアドレスの緯度と経度座標を取得します。 – user3342735

+1

@TFerrell - この質問はRのパッケージ 'googleway'に固有なものなので、あなたがどちらか/両方を熟知していれば '明確'です – SymbolixAU

答えて

2

あなたはdata.frameを構築してきた方法は、次にあなたが各1

をジオコードする *applyメソッドを使用することができますビット複雑と思われるので、私はここでそれを再やってる

dt <- data.frame(address = c("4605 Langdon St, Fernley, NV, 89408", 
          "350 Quivera Ln, Sparks, NV, 89441"), 
       stringsAsFactors = FALSE) 

あなたは、各結果の座標を抽出し、あなたがそれでやりたいことができ

library(googleway) 

key <- 'api_key' 

res <- apply(dt, 1, function(x){ 
    google_geocode(address = x[['address']], 
       key = key) 
}) 

str(res) 
# List of 2 
# $ :List of 2 
# ..$ results:'data.frame': 1 obs. of 5 variables: 
# .. ..$ address_components:List of 1 
# .. .. ..$ :'data.frame': 8 obs. of 3 variables: 
# .. .. .. ..$ long_name : chr [1:8] "4605" "Langdon Street" "Fernley" "Lyon County" ... 
# .. .. .. ..$ short_name: chr [1:8] "4605" "Langdon St" "Fernley" "Lyon County" ... 
# .. .. .. ..$ types  :List of 8 
# .. .. .. .. ..$ : chr "street_number" 
# .. .. .. .. ..$ : chr "route" 
# ... etc 

...

coords <- lapply(res, function(x){ 
    x$results$geometry$location 
}) 

coords <- lapply(seq_along(res), function(x){ 
    coords <- res[[x]]$results$geometry$location 
    address <- dt[x, 'address'] 
    res_df <- data.frame(lat = coords[, 'lat'], 
         lon = coords[, 'lng'], 
         address = address 
         ) 
}) 

df_coords <- do.call(rbind, coords) 
df_coords 
#  lat  lon        address 
# 1 39.59275 -119.2026 4605 Langdon St, Fernley, NV, 89408 
# 2 39.68911 -119.6345 350 Quivera Ln, Sparks, NV, 89441 

mapKey <- symbolix.utils::mapKey() 

google_map(key = mapKey) %>% 
    add_markers(data = df_coords, lat = "lat", lon = "lon", info_window = "address") 

enter image description here


注:

あなたは座標が入力されたアドレスに並んでいることを「確認」になりたいと思った場合、あなたはありません*apply内の検索結果を構築する必要がありますジオコーディング

+0

Googleがジオコードできないアドレスがいくつかある。 – user3342735

+0

@ user3342735これを解決するには、何らかのエラー処理を実装する必要があります。 – SymbolixAU