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
500
所要時間のヒートマップを作成する
nonki1974
December 08, 2018
Tweet
Share
More Decks by nonki1974
See All by nonki1974
GTFS with Tidytransit package
nonki1974
0
290
TokyoR#84_Rexams
nonki1974
0
200
都道府県別焼き鳥屋ランキングの作成
nonki1974
1
850
Introduction to R
nonki1974
0
320
Introduction to dplyr
nonki1974
0
440
Introduction to ggplot2
nonki1974
1
480
Analyzing PSB tracks with R
nonki1974
0
570
introduction to fukuoka.R @ Fukuoka.LT
nonki1974
0
59
gtfsr package @ fukuoka.R #11
nonki1974
0
310
Other Decks in Technology
See All in Technology
低レイヤを知りたいPHPerのためのCコンパイラ作成入門 / Building a C Compiler for PHPers Who Want to Dive into Low-Level Programming
tomzoh
0
220
LangfuseでAIエージェントの 可観測性を高めよう!/Enhancing AI Agent Observability with Langfuse!
jnymyk
1
210
AIと開発者の共創: エージェント時代におけるAIフレンドリーなDevOpsの実践
bicstone
1
300
Devinで模索する AIファースト開発〜ゼロベースから始めるDevOpsの進化〜
potix2
PRO
7
3.3k
JPOUG Tech Talk #12 UNDO Tablespace Reintroduction
nori_shinoda
1
140
Webアプリを Lambdaで動かすまでに考えること / How to implement monolithic Lambda Web Application
_kensh
7
1.3k
Road to Go Gem #rubykaigi
sue445
0
350
AWSのマルチアカウント管理 ベストプラクティス最新版 2025 / Multi-Account management on AWS best practice 2025
ohmura
4
280
開発視点でAWS Signerを考えてみよう!! ~コード署名のその先へ~
masakiokuda
3
160
試験は暗記より理解 〜効果的な試験勉強とその後への活かし方〜
fukazawashun
0
370
Dynamic Reteaming And Self Organization
miholovesq
3
360
Classmethod AI Talks(CATs) #21 司会進行スライド(2025.04.17) / classmethod-ai-talks-aka-cats_moderator-slides_vol21_2025-04-17
shinyaa31
0
570
Featured
See All Featured
Fireside Chat
paigeccino
37
3.4k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.3k
For a Future-Friendly Web
brad_frost
176
9.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.5k
Statistics for Hackers
jakevdp
798
220k
The World Runs on Bad Software
bkeepers
PRO
67
11k
The Invisible Side of Design
smashingmag
299
50k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2k
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
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