LoginSignup
0

More than 5 years have passed since last update.

R/ggmapでStamenMapsのterrainを表示しようとしたらjpgとpngの問題で表示できなかったのでソースから修正した話

Posted at

経緯

Google Mapをソースにすればよいのだが、非公開のシステムを作ろうとするとライセンス上問題がある(買えばいいのだけど)。CC BY 3.0のStamen Mapsがあるのでそれを使おうと思った。
Rのggmapパッケージを使用する。

Stamen Mapsには地図の表示形式が複数あってterrainが一番通常の地図に近くきれいなのだけど、terrain以外の形式は問題なく表示できるのだが、terrainを指定すると、

  • ggmapとしては地図画像をjpgで取得しようと処理する
  • Stamen Mapsからはpngで画像を返してくる
  • エラー

という按配。Google検索したらggmapのgit hubに
stamen returning .png's, not .jpg's
というやり取りがあり、対応済みっぽいのだが、パッケージはまだ更新されていないのか。
仕方ないのでggmapのソースをダウンロードして修正してみる。

手順

  • Rからインストール済みのggmapをアンインストールする。
remove.packages("ggmap")
  • これだけだと現在の関数が残ったままなので、Rを再起動
  • CRANからggmapのソースをダウンロード
  • 展開し、ggmap/R/get_stamenmap.R の下記を修正
    • "terrain"だけでなく"terrain-background"も同様に修正
    旧236   if(maptype %in% c("terrain","terrain-background","watercolor")){
    新236   if(maptype %in% c("watercolor")){
    237     filetype <- "jpg"
    238   } else {
    239     filetype <- "png"
    240   }

中略

    旧427   if(maptype %in% c("terrain","terrain-background","watercolor")){
    新427   if(maptype %in% c("watercolor")){
    428     filetype <- "jpg"
    429   } else {
    430     filetype <- "png"
    431   }

中略

    旧444   if(maptype %in% c("terrain","terrain-background","watercolor")){
    新444   if(maptype %in% c("watercolor")){
    445     tile <- readJPEG(tmp)
    446   } else {
    447     tile <- readPNG(tmp)
    448   }
  • ggmapのフォルダごとtar.gzで圧縮し直す
    • 例 tar zcvf ggmap_2.6.1.edit.tar.gz ggmap/
  • Rで上記修正済みtar.gzを適用
    • Rのメニューバー [Tools]-[Install Packages...]
      • Install from で "Package Arcive File(.zip; .tar.gz)
        • 上記の修正済み.tar.gzファイルを選択
      • "Install"ボタン
  • これで準備が整ったので例えば以下の通り
library(ggmap)

loc<-c(139.69, 35.64, 139.78, 35.7)
(map<-ggmap(get_stamenmap(loc, maptype = 'terrain', zoom = 13)))

Rplot.png

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0