地図を描きたい俺はgoogleVisパッケージかggmapパッケージかどちらを使おうか迷うが、静的な地図を描きたい場合はggmapで良いみたい

掲題の件、そういうことです。動的にインタラクティブに地図いじりたい!って人はgoogleVisパッケージでいいけど、静的な地図ならggmapなな印象だね!・・・と以下に書いてあった。

というわけで、今、静的な地図に興味がある俺はggmapをちょいとお勉強。

日本を中心として地図を描く

基本にして、ここで紹介するget_map, ggmap関数がggmapパッケージのメイン関数となる。
get_map関数はGoogle Mapを筆頭とする4つのデータソースから地図を取得する関数になってて、source引数でデータソースを指定する。
また、それぞれにソースに対して直接アクセスする関数

- get_googlemap
- get_openstreetmap
- get_stamenmap
- get_cloudmademap

もある。あるが、俺はgoogle mapばっかり使うことになるんだろうなって。その他のデータソースは

らっぽい。

ggmap関数がget_mapで取得したデータの描画関数にあたる。
以下は大体日本を中心とした地図の描画方法。

library(ggmap)
map <- get_googlemap(center=c(139,35), scale=1, size=c(1260, 960), zoom=4)
ggmap(map) + 
  geom_point(aes(x=lon, y=lat), data=data.frame(lon=139, lat=35), size=10, colour='black')


geocodeで地名から緯度・経度算出

北海道の緯度・経度。県庁所在地なのかな?

> geocode('Hokkaido')
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hokkaido&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
       lon      lat
1 141.3468 43.06461

revgeocode関数で逆引き(緯度・経度⇒住所)できる。

> revgeocode(c(141.3468,43.06461))
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?latlng=43.06461,141.3468&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
[1] "6 Chome-1 Kita 3 Jonishi, Chuo Ward, Sapporo, Hokkaido, Japan"

これらのAPIに対する利用制限はgeocodeQueryCheckで取得できる。

> geocodeQueryCheck()
2498 geocoding queries remaining. 

get_mapする際の引数(maptype)を変えることで、いろんな種類の地図になる

以下はロードマップの例。他にもいろいろある。

> library(magrittr)
> get_map(maptype='roadmap') %>% ggmap(map, fullpage=TRUE)


ggimage + hadleyで神降臨。

ありがたや。。。

> ggimage(hadley)

2点間の経路を検索してその結果を地図に描画

マニュアルから引っ張って来たそのまんまですが、あぁ、こりゃすげぇや。。。

> from <- 'houson, texas'
> to <- 'waco, texas'
> route_df <- route(from, to, structure = 'route')
Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=houson,+texas&destination=waco,+texas&mode=driving&units=metric&alternatives=false&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
> qmap('college station, texas', zoom = 8) +
+     geom_path(
+         aes(x = lon, y = lat), colour = 'red', size = 1.5,
+         data = route_df, lineend = 'round'
+     )
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=college+station,+texas&zoom=8&size=%20640x640&scale=%202&maptype=terrain&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=college+station,+texas&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms


Open Street Map/Stamenからは取れなかった

Googleじゃないソースも使ってみようかなって思ったら、なんか怒られる。

> get_openstreetmap()
Error: map grabbing failed - see details in ?get_openstreetmap.
In addition: Warning message:
In download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") :
  cannot open: HTTP status was '503 Service Unavailable'
> get_stamenmap()
Error in readPNG(destfile) : file is not in PNG format

cloudmademapはAPIキーが必要だし、googlemapかなぁ。

あとこれ画像張り付けるのめんどいからRPubs + Markdownでまとめるべき事案でしたわ。。。