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
390
所要時間のヒートマップを作成する
nonki1974
December 08, 2018
Tweet
Share
More Decks by nonki1974
See All by nonki1974
GTFS with Tidytransit package
nonki1974
0
200
TokyoR#84_Rexams
nonki1974
0
170
都道府県別焼き鳥屋ランキングの作成
nonki1974
1
720
Introduction to R
nonki1974
0
300
Introduction to dplyr
nonki1974
0
360
Introduction to ggplot2
nonki1974
1
420
Analyzing PSB tracks with R
nonki1974
0
510
introduction to fukuoka.R @ Fukuoka.LT
nonki1974
0
54
gtfsr package @ fukuoka.R #11
nonki1974
0
270
Other Decks in Technology
See All in Technology
MLOpsの「壁」を乗り越える、LINEヤフーの Data Quality as Code
lycorptech_jp
PRO
5
510
推しは推せるときに推せ! プロダクトにフィードバックしていこう
nakasho
0
310
長期間TiDBを使ってきた話 @ 私たちはなぜNewSQLを使うのかTiDB選定5社が語る選定理由と活用LT / Experiences with TiDB Over Time
chibiegg
2
900
自己改善からチームを動かす! 「セルフエンジニアリングマネージャー」のすゝめ
shoota
6
630
Tellus の衛星データを見てみよう #mf_fukuoka
kongmingstrap
0
190
反実仮想機械学習とは何か
usaito
PRO
11
4.6k
MapLibreとAmazon Location Service
dayjournal
1
150
TechFeed Experts Night#27 〜 フロントエンドフレームワーク最前線 (Svelte)
baseballyama
1
490
開発生産性向上サービスを作るFindyが自分たちで開発生産性を爆上げした組織づくりの歩み / Findy's path to boosting its own development productivity 2024-04-17
ma3tk
3
660
[新卒向け研修資料] テスト文字列に「うんこ」と入れるな(2024年版)
infiniteloop_inc
4
14k
ココがすごいぜ!Playwright Component Test
rakus_fe
0
130
【NW X Security JAWS#3】L3-4:AWS環境のIPv6移行に向けて知っておきたいこと
shotashiratori
0
140
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
20
1.9k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
BBQ
matthewcrist
80
8.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
187
16k
Become a Pro
speakerdeck
PRO
11
4.5k
The Straight Up "How To Draw Better" Workshop
denniskardys
227
130k
Designing for humans not robots
tammielis
248
25k
A Tale of Four Properties
chriscoyier
151
22k
A Philosophy of Restraint
colly
197
16k
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
260
12k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
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