Rでコマンド:Flickr APIからデータを取得するコマンド例
写真の共有サイトFlickrからAPIを利用してデータを取得するコマンド例です。写真の位置データを取得し地図上にポイントで示す内容です。
なお、地図の描写には「ggmap」パッケージ、画像の表示には「magick」パッケージを利用しています。
・Rで解析:GoogleMapやStamenMapを利用「ggmap」パッケージ https://www.karada-good.net/analyticsr/r-216 ・Rで解析:画像操作の進化「magick」パッケージ https://www.karada-good.net/analyticsr/r-531
実行コマンドはwindows 7およびOS X 10.11.2のR version 3.2.4で確認しています。
<おすすめのRに関する書籍です>
Rではじめるデータサイエンス 第2版 | Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund, 大橋 真也
Rによる機械学習[第3版] (Programmer's SELECTION)
Flickr APIの取得
Yahoo IDが必要です。もしIDを持っていなくともFlickrアカウントの取得過程で取得を促されます。Flickr APIは非常に簡単に取得ができます。Flickrアカウントを取得後、画像赤枠内を選択し、表示される項目を入力するだけです。
・Flickr API Servics https://www.flickr.com/services/api/
スポンサーリンク
実行コマンド
詳細はコメント、各パッケージのヘルプを確認してください。
#パッケージのインストール
install.packages("ggmap")
install.packages("RCurl")
install.packages("jsonlite")
install.packages("stringi")
#パッケージの読み込み
library("ggmap")
library("RCurl")
library("jsonlite")
library("stringi")
#flickrで取得したAPIKeyを設定
APIKey <- "APIkeyを入力"
#ggmapパッケージを利用して緯度経度を取得
#stringi:stri_encodeコマンドを利用すると日本語でもエラーが出ません。
LonLatData <- geocode(stri_encode("京都", "", "utf-8"), source = "google")
#当初公開コマンド
#LonLatData <- geocode("kyoto", source = "google")
#緯度経度の範囲を設定
LonLatDataAra <- paste(c(LonLatData - .2, LonLatData + .2), collapse = ",")
#API用のURLを作成
#LonLatDataAraの範囲で緯度経度データを持つ情報を取得する
GetData <- paste0("https://www.flickr.com/services/rest/?method=flickr.photos.search&format=json&nojsoncallback=1&api_key=",
APIKey, "&extras=geo&bbox=", LonLatDataAra, "&per_page=500")
#データを取得:RCurlパッケージ:getURLコマンド
raw_data <- getURL(GetData, ssl.verifypeer = FALSE)
#JSONデータをオブジェクト化:jsonliteパッケージ:fromJSONコマンド
data <- fromJSON(raw_data)
#必要なデータを抽出
#データ構造が気になる方はstr(data)で確認してください
MasterData <- data[1]$photos$photo
#MasterDataの構造確認
str(MasterData)
'data.frame': 250 obs. of 19 variables:
$ id : chr "32519776786" "32519150966" "32432952621" "31711590894" ...
$ owner : chr "40761615@N03" "60527064@N00" "51035608895@N01" "51035608895@N01" ...
$ secret : chr "cc9c4ca7bc" "babfe67ab9" "dd43113ae3" "25ce80c1d2" ...
$ server : chr "270" "399" "386" "539" ...
$ farm : int 1 1 1 1 1 1 1 1 1 1 ...
$ title : chr "This morning / January 28, 2017 at09:58AM JST" "Rooftop in detail, Rokudo Chinnou-ji" "2771." "2773." ...
$ ispublic : int 1 1 1 1 1 1 1 1 1 1 ...
$ isfriend : int 0 0 0 0 0 0 0 0 0 0 ...
$ isfamily : int 0 0 0 0 0 0 0 0 0 0 ...
$ latitude : chr "34.977272" "34.997932" "35.038994" "35.032011" ...
$ longitude : chr "135.772263" "135.775383" "135.728916" "135.732008" ...
$ accuracy : chr "16" "16" "16" "16" ...
$ context : int 0 0 0 0 0 0 0 0 0 0 ...
$ place_id : chr "9cw_d0JQV7obLp8N5A" "9cw_d0JQV7obLp8N5A" "9cw_d0JQV7obLp8N5A" "9cw_d0JQV7obLp8N5A" ...
$ woeid : chr "15015372" "15015372" "15015372" "15015372" ...
$ geo_is_family : int 0 0 0 0 0 0 0 0 0 0 ...
$ geo_is_friend : int 0 0 0 0 0 0 0 0 0 0 ...
$ geo_is_contact: int 0 0 0 0 0 0 0 0 0 0 ...
$ geo_is_public : int 1 1 1 1 1 1 1 1 1 1
#Mapデータを取得:ggmapパッケージ;get_googlemapコマンド
GetMap <- get_googlemap(center = c(lon = LonLatData[[1]], lat = LonLatData[[2]]),
zoom = 13)
#プロット:ggmapパッケージ;ggmapコマンド,ggplot2パッケージ:geom_pointコマンド
ggmap(GetMap) +
geom_point(data = MasterData,
aes(x = type.convert(MasterData[, 11]),
y = type.convert(MasterData[, 10])), size = 2, colour = "red")
Warning message:
Removed 152 rows containing missing values (geom_point).
###参考_写真の取得#####
#https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
#install.packages("magick")
library("magick")
#画像の取得
GrtPict <- getURLContent(paste0("https://farm", MasterData[3, 5], ".staticflickr.com/",
MasterData[3, 4], "/", MasterData[3, 1], "_",
MasterData[3, 3], ".jpg"), ssl.verifypeer = FALSE)
#画像の表示
image_browse(image_read(GrtPict))
実行結果
・get_googlemapコマンド
・参考_写真の取得
少しでも、あなたのウェブや実験の解析が楽になりますように!!
スポンサーリンク


