Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
所要時間のヒートマップを作成する
Search
nonki1974
December 08, 2018
Technology
0
530
所要時間のヒートマップを作成する
nonki1974
December 08, 2018
Tweet
Share
More Decks by nonki1974
See All by nonki1974
GTFS with Tidytransit package
nonki1974
0
300
TokyoR#84_Rexams
nonki1974
0
200
都道府県別焼き鳥屋ランキングの作成
nonki1974
1
870
Introduction to R
nonki1974
0
330
Introduction to dplyr
nonki1974
0
460
Introduction to ggplot2
nonki1974
1
490
Analyzing PSB tracks with R
nonki1974
0
580
introduction to fukuoka.R @ Fukuoka.LT
nonki1974
0
59
gtfsr package @ fukuoka.R #11
nonki1974
0
320
Other Decks in Technology
See All in Technology
恐怖!テストコードなき夜
tsukuboshi
2
110
増え続ける脆弱性に立ち向かう: 事前対策と優先度づけによる 持続可能な脆弱性管理 / Confronting the Rise of Vulnerabilities: Sustainable Management Through Proactive Measures and Prioritization
nttcom
1
230
会社もクラウドも違うけど 通じたコスト削減テクニック/Cost optimization strategies effective regardless of company or cloud provider
aeonpeople
2
430
人に寄り添うAIエージェントとアーキテクチャ #BetAIDay
layerx
PRO
8
1.2k
2025-07-31: GitHub Copilot Agent mode at Vibe Coding Cafe (15min)
chomado
2
300
VLMサービスを用いた請求書データ化検証 / SaaSxML_Session_1
sansan_randd
0
170
株式会社島津製作所_研究開発(集団協業と知的生産)の現場を支える、OSS知識基盤システムの導入
akahane92
1
1.3k
大規模イベントを支える ABEMA の アーキテクチャ 変遷 2025
nagapad
6
600
Claude Codeが働くAI中心の業務システム構築の挑戦―AIエージェント中心の働き方を目指して
os1ma
9
1.3k
クマ×共生 HACKATHON - 熊対策を『特別な行動」から「生活の一部」に -
pharaohkj
0
270
LIFF CLIとngrokを使ったLIFF/LINEミニアプリのお手軽実機確認
diggymo
0
120
Mambaで物体検出 完全に理解した
shirarei24
2
170
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
53
7.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Faster Mobile Websites
deanohume
308
31k
Docker and Python
trallard
45
3.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
The Cult of Friendly URLs
andyhume
79
6.5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
283
13k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.3k
Balancing Empowerment & Direction
lara
1
510
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
For a Future-Friendly Web
brad_frost
179
9.8k
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