$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
所要時間のヒートマップを作成する
Search
nonki1974
December 08, 2018
Technology
0
550
所要時間のヒートマップを作成する
nonki1974
December 08, 2018
Tweet
Share
More Decks by nonki1974
See All by nonki1974
GTFS with Tidytransit package
nonki1974
0
310
TokyoR#84_Rexams
nonki1974
0
200
都道府県別焼き鳥屋ランキングの作成
nonki1974
1
890
Introduction to R
nonki1974
0
340
Introduction to dplyr
nonki1974
0
500
Introduction to ggplot2
nonki1974
1
510
Analyzing PSB tracks with R
nonki1974
0
590
introduction to fukuoka.R @ Fukuoka.LT
nonki1974
0
65
gtfsr package @ fukuoka.R #11
nonki1974
0
330
Other Decks in Technology
See All in Technology
履歴テーブル、今回はこう作りました 〜 Delegated Types編 〜 / How We Built Our History Table This Time — With Delegated Types
moznion
16
9.5k
Master Dataグループ紹介資料
sansan33
PRO
1
4k
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
37k
Bill One 開発エンジニア 紹介資料
sansan33
PRO
4
16k
タグ付きユニオン型を便利に使うテクニックとその注意点
uhyo
2
660
一億総業務改善を支える社内AIエージェント基盤の要諦
yukukotani
8
2.8k
HIG学習用スライド
yuukiw00w
0
110
Uncertainty in the LLM era - Science, more than scale
gaelvaroquaux
0
560
原理から解き明かす AIと人間の成長 - Progate BAR
teba_eleven
2
300
知っていると得する!Movable Type 9 の新機能を徹底解説
masakah
0
210
こがヘンだよ!Snowflake?サービス名称へのこだわり
tarotaro0129
0
110
ML PM Talk #1 - ML PMの分類に関する考察
lycorptech_jp
PRO
1
540
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
174
15k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.3k
Git: the NoSQL Database
bkeepers
PRO
432
66k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Done Done
chrislema
186
16k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
1k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
380
A designer walks into a library…
pauljervisheath
210
24k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Raft: Consensus for Rubyists
vanstee
140
7.2k
Transcript
所要時間のヒートマップを 作成する google directions API & googleway package Tomokazu FUJINO
@ R研究集会2018 LT December 28, 2018
はじめに → 不動産の賃料推定などで,主要都市などへの所要時間を考慮に 入れたいことがある → 多くの地点から,特定の地点までの所要時間データの作成を自 動化したい → 今回の例:福岡県の 3
次メッシュにおける代表点から,天神へ の所要時間を可視化する 2
道具 → Google Maps Platform の Directions API → R
から呼び出すパッケージ:googleway package → 土地利用 3 次メッシュ@国土数値情報 → kokudosuuchi , sf package → rmapshaper package 3
Directions API → Google Map Platform 経路探索 API → 月
40000 回呼び出しま で無料で使える → 日本では電車の経路は使 えない(徒歩,バス, 車,自転車のみ) 4
ライブラリ読み込み library(tidyverse) library(sf) library(googleway) 5
利用データの識別子の確認 → 国土数値情報の「土地利用 3 次メッシュ」と「行政区域」の データを API 経由でダウンロードするため,識別子を調べる。 kokudosuuchi::getKSJSummary() %>%
filter(title == "土地利用 3 次メッシュ") ## # A tibble: 1 x 5 ## identifier title field1 field2 ## <chr> <chr> <chr> <chr> ## 1 L03-a 土地利用~ 国土(水・~ 土地利用~ ## # ... with 1 more variable: ## # areaType <chr> kokudosuuchi::getKSJSummary() %>% filter(title=="行政区域") ## # A tibble: 1 x 5 ## identifier title field1 field2 ## <chr> <chr> <chr> <chr> 6
福岡県を覆う3次メッシュを取ってくる → st_transform() 関数で平面直角座標系に変換 kokudosuuchi::getKSJURL("L03-a", meshCode = c(5030, 5031, 4930))
%>% filter(year == 2014, datum == 2) %>% pull(zipFileUrl) %>% map(kokudosuuchi::getKSJData) %>% flatten %>% data.table::rbindlist() %>% st_as_sf() %>% st_transform(crs = 2444) -> kyushu.mesh 7
福岡県の行政界を取ってくる kokudosuuchi::getKSJURL("N03", prefCode = 40) %>% filter(year == 2014) %>%
pull(zipFileUrl) %>% map(kokudosuuchi::getKSJData) %>% flatten %>% data.table::rbindlist() %>% st_as_sf() %>% st_transform(crs = 2444) -> fukuoka.boundary 8
メッシュの切り出しと中心点の計算 福岡県の行政界でメッシュを切り出す st_join(kyushu.mesh, fukuoka.boundary, join = st_intersects) %>% filter(!is.na(N03_007)) ->
fukuoka.mesh メッシュの中心点を計算して,出発点として使う fukuoka.mesh %>% mutate(center = st_centroid(geometry)) %>% st_set_geometry("center") %>% st_transform(crs = 4326) %>% mutate(x = st_coordinates(center)[,2], y = st_coordinates(center)[,1]) -> fukuoka.centroid 9
明らかに不要なメッシュを除去 & APIキーの 設定 fukuoka.centroid %>% filter(海水域 < 500000) %>%
select(x, y) %>% as.data.frame() %>% select(-center) -> fukuoka.origin set_key("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") 10
API呼び出し → google_directions() 関数を使う → mode = "transit"で徒歩+バスの経路検索(日本の場合) fukuoka.origin %>%
pmap(function(x, y){ tryCatch({ google_directions(origin = data.frame(x, y), destination = c(33.591679, 130.398177), mode = "transit", departure_time = as.POSIXct("2018-12-10 07:00:00")) }, error = function(e){ return(NA) }) }) -> tenjin.duration 11
結果のリストを処理する関数を定義 get_duration <- function(dlist){ if("status" %in% names(dlist)){ if(dlist$status == "OK"){
direction_legs(dlist) %>% select(duration, start_location, end_location) %>% rlist::list.flatten() } } } Unzip <- function(...) rbind(data.frame(), ...) 12
データフレームにしてsfにする メッシュと結合するため平面直角座標系にしておく tenjin.duration %>% map(get_duration) %>% do.call(Unzip, .) %>% st_as_sf(coords
= c("start_location.lng", "start_location.lat"), crs = 4326) %>% st_transform(crs = 2444) -> tenjin.duration.df メッシュと結合 st_join(fukuoka.mesh, tenjin.duration.df, join = st_intersects) %>% filter(!is.na(duration.value)) -> tenjin.duration.mesh 13
地図の描画 # 小さい離島を除いておく library(rmapshaper) fukuoka.boundary <- ms_filter_islands( fukuoka.boundary, min_area =
2000000 ) ggplot() + geom_sf(data = fukuoka.boundary, aes(fill = NULL)) + geom_sf(data = tenjin.duration.mesh, aes(fill = cut(duration.value, quantile(duration.value)))) + coord_sf(datum = NA) + theme_void() 14
地図の描画 fill (445,4.66e+03] (4.66e+03,6.66e+03] (6.66e+03,8.36e+03] (8.36e+03,3.73e+04] NA 15
車の場合: mode="driving" fill (281,3e+03] (3e+03,3.67e+03] (3.67e+03,4.69e+03] (4.69e+03,1.42e+04] NA 16