Skip to content

Instantly share code, notes, and snippets.

@uribo
Created July 17, 2020 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uribo/9a73f7eba08dabf01e8a537fbaab8f70 to your computer and use it in GitHub Desktop.
Save uribo/9a73f7eba08dabf01e8a537fbaab8f70 to your computer and use it in GitHub Desktop.
Transport census flowmap
###################################
# 大都市交通センサス
# 第12回 首都圏 https://www.mlit.go.jp/sogoseisaku/transport/sosei_transport_tk_000007.html
# 線別駅間移動人員,
# 鉄道位置データ: 国土数値情報 鉄道時系列 https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N05-v1_3.html
# 行政区画: 地球地図日本 https://www.gsi.go.jp/kankyochiri/globalmap.html
# 加工・編集者: 瓜生真也 (Shinya Uryu)
#
# mapboxよりアクセストークンを取得してください
###################################
library(flowmapblue)
library(ssrn)
library(sf)
library(dplyr)
d <-
kuniumi::read_ksj_n05("N05-18_GML/N05-18_Station2.geojson") %>%
# 現存する駅のみ、新幹線を除く
filter(timePeriod_End == "9999",
!timePeriod_Begin %in% c("2016", "2017", "2018"),
serviceProviderType != "1") %>%
select(railwayLineName, operationCompany, stationName, geometry) %>%
mutate(operationCompany = stringr::str_remove_all(operationCompany, "(旧国鉄)")) %>%
filter(operationCompany != "東日本旅客鉄道" | stationName != "新治") %>%
filter(operationCompany != "秩父鉄道" | !stationName %in% c("和銅黒谷", "大野原", "秩父", "御花畑",
"影森", "浦山口", "武州中川", "武州日野",
"白久", "三峰口")) %>%
filter(operationCompany != "箱根登山鉄道" | !stationName %in% c("中強羅", "公園下", "公園上", "上強羅", "早雲山")) %>%
filter(!operationCompany %in% c("御岳登山鉄道", "高尾登山電鉄", "舞浜リゾートライン", "筑波観光鉄道", "真岡鐵道",
"富士急行", "大山観光電鉄", "伊豆箱根鉄道")) %>%
filter(operationCompany != "東京都" | !stationName %in% c("上野動物園西園", "上野動物園東園")) %>%
mutate(operationCompany = if_else(operationCompany == "横浜高速鉄道" & stationName %in% c("こどもの国", "恩田", "長津田"),
"東京急行電鉄",
operationCompany),
stationName = if_else(operationCompany == "相模鉄道" & stationName == "三ツ鏡",
"三ツ境",
stationName))
# 運営会社で駅を一つに
d <-
d %>%
group_by(operationCompany, stationName) %>%
slice_head(n = 1) %>%
ungroup() %>%
select(-railwayLineName)
sf_kanto <-
sf::st_read("gm-japan/gm-jpn-all_u_2_2/polbnda_jpn.shp") %>%
sf::st_transform(crs = 4326) %>%
dplyr::filter(nam %in% c("Tochigi Ken", "Ibaraki Ken",
"Saitama Ken", "Kanagawa Ken", "Gunma Ken",
"Tokyo To", "Chiba Ken", "Yamanashi Ken")) %>%
dplyr::filter(adm_code %in% c("08203", "08204", "08205",
"08207", "08208", "08210",
"08211", "08217", "08219", "08220", "08224",
"08227", "08228", "08229", "08230",
"08235",
"08442", "08443", "08447", "08521", "08542", "08546",
"08564",
"09203", "09204", "09208", "09364",
"10207", "10521", "10522",
"11100", "11201", "11202", "11203", "11206",
# 除外しても良い?駅があるけどデータがない
"11207",
"11208", "11209", "11210", "11211", "11212", "11214",
"11215", "11216", "11217", "11218", "11219", "11221",
"11222", "11223", "11224", "11225", "11227", "11228",
"11229", "11230", "11231", "11232", "11233", "11234",
"11235", "11237", "11238", "11239", "11240", "11241",
"11242", "11243", "11245", "11301",
"11324", "11326", "11327", "11341", "11342", "11343", "11346",
"11347", "11348", "11349", "11361", "11369", "11381",
"11385", "11408", "11442", "11445", "11464", "11465",
"12100", "12203", "12204", "12206", "12207", "12208",
"12210", "12211", "12212", "12213", "12216", "12217", "12219",
"12220", "12221", "12222", "12224", "12225", "12227",
"12228", "12229", "12230", "12231", "12232", "12233",
"12236", "12237", "12322", "12329", "12342", "12402",
"12421", "12423", "12426", "12441",
paste0(seq.int(13101, 13360)),
"14100", "14130", "14150", "14201", "14203", "14204",
"14205", "14206", "14207", "14208", "14210", "14211",
"14212", "14213", "14214", "14215", "14216", "14217",
"14218", "14301", "14321", "14341", "14342", "14361",
"14362", "14363", "14366", "14382", "14382", "14383",
"14384", "14401",
"19206", "19212"))
sf_target_st_kanto <-
d %>%
st_crop(sf_kanto)
# 鉄道駅コード
df_station_code <-
readxl::read_xlsx("001179689.xlsx",
skip = 1,
col_names = c("st_code",
"oc_name",
"rw_name",
"st_name",
"oc_code",
"rw_code")) %>%
mutate(st_name = stringi::stri_trans_general(st_name, "nfkc")) %>%
mutate(st_name = recode(st_name,
`四ッ谷` = "四ツ谷",
`巌根` = "巖根",
`三ッ沢上町` = "三ツ沢上町",
`三ッ沢下町` = "三ツ沢下町",
`日本大通り` = "日本大通",
`南阿佐ケ谷` = "南阿佐ヶ谷",
`西ケ原` = "西ヶ原",
`祇園` = "祗園"),
st_name = if_else(oc_name == "東京地下鉄" & st_name == "市ケ谷",
"市ヶ谷",
st_name),
st_name = if_else(oc_name == "東京地下鉄" & st_name == "霞ケ関",
"霞ヶ関",
st_name),
oc_name = recode(oc_name,
`横浜市交通局` = "横浜市",
`東京都交通局` = "東京都"),
rw_name = stringr::str_remove(rw_name, "(.+)")) %>%
filter(oc_name != "東日本旅客鉄道" | rw_name != "総武本線" | st_name != "横芝") %>%
filter(oc_name != "東日本旅客鉄道" | rw_name != "内房線" | !st_name %in% c("青堀", "大貫", "佐貫町", "上総湊", "竹岡", "浜金谷"))
# 線別駅間移動人員
df_passenger <-
readxl::read_xlsx("001179095.xlsx",
col_types = c("text", "text", "numeric", "text", "numeric", "numeric"),
skip = 1,
col_names = c("rw_code",
"departure_st_code", "departure_status",
"arrive_st_code", "arrive_status",
"volume")) %>%
mutate(rw_code = stringr::str_pad(rw_code, width = 3, pad = "0"),
across(ends_with("st_code"), ~stringr::str_pad(.x, width = 5, pad = "0")))
df_passenger_all <-
df_passenger %>%
make_passenger_od(df_station_code,
depart = departure_st_code,
arrive = arrive_st_code,
location = st_code,
value = volume,
.all = FALSE) %>%
dplyr::left_join(df_station_code %>%
select(arrive_st_code = st_code, next_st_name = st_name),
by = "arrive_st_code")
sf_target_st_kanto <-
sf_target_st_kanto %>%
inner_join(df_station_code,
by = c("operationCompany" = "oc_name",
"stationName" = "st_name"))
df_target_st <-
sf_target_st_kanto %>%
mutate(lon = st_coordinates(geometry)[, 1],
lat = st_coordinates(geometry)[, 2]) %>%
st_drop_geometry() %>%
select(id = st_code, name = stationName, lat, lon)
df_passenger_od <-
df_passenger_all %>%
select(origin = departure_st_code,
dest = arrive_st_code,
count = volume)
mapboxAccessToken <- rstudioapi::askForPassword()
flowmapblue(df_target_st,
df_passenger_od,
mapboxAccessToken,
clustering = TRUE,
darkMode = FALSE,
animation = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment