【R】gbfs
2020年12月15日
1. はじめに
bgfsは、バイクシェアのデータセットです。
2. インストール
CRANからインストールできます。
install.packages("gbfs")
3. 使ってみる
データを取得し、ちょろっと見てみます。
library(gbfs) library(tidyverse) pdx_station_info <- get_station_information("https://gbfs.biketownpdx.com/gbfs/gbfs.json") glimpse(pdx_station_info)
> glimpse(pdx_station_info)
Rows: 185
Columns: 19
$ eightd_has_key_dispenser <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS...
$ has_kiosk <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS...
$ eightd_station_services <list> [[], [], [], [], [], [], [], [], [], [], [], [], [],...
$ capacity <int> 20, 12, 18, 18, 6, 10, 7, 6, 12, 18, 14, 16, 18, 18, ...
$ region_code <chr> "PDX", "PDX", "PDX", "PDX", "PDX", "PDX", "PDX", "PDX...
$ dockless_bikes_parking_zone_capacity <int> 20, 12, 18, 18, 6, 10, 7, 6, 12, 18, 14, 16, 18, 18, ...
$ station_type <chr> "lightweight", "lightweight", "lightweight", "lightwe...
$ lat <dbl> 45.51898, 45.53480, 45.55645, 45.52270, 45.50225, 45....
$ external_id <chr> "motivate_PDX_1440914125193298376", "motivate_PDX_144...
$ electric_bike_surcharge_waiver <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS...
$ name <chr> "SW Yamhill at Director Park", "N Wheeler at Broadway...
$ address <chr> "Park Avenue West, Southwest Yamhill Street, Portland...
$ rack_model <chr> "CITY_PILOT_RACK", "CITY_PUBLIC_RACK", "CITY_PILOT_RA...
$ legacy_id <chr> "1440914125193298376", "1440913821215146858", "144091...
$ lon <dbl> -122.6813, -122.6687, -122.6604, -122.6703, -122.6753...
$ station_id <chr> "1440914125193298376", "1440913821215146858", "144091...
$ client_station_id <chr> "motivate_PDX_1440914125193298376", "motivate_PDX_144...
$ rental_uris.android <chr> "https://pdx.lft.to/lastmile_qr_scan", "https://pdx.l...
$ rental_uris.ios <chr> "https://pdx.lft.to/lastmile_qr_scan", "https://pdx.l...
それぞれのステーションでのバイク数はこちら。
pdx_station_status <- get_station_status("https://gbfs.biketownpdx.com/gbfs/gbfs.json") glimpse(pdx_station_status)
> glimpse(pdx_station_status)
Rows: 185
Columns: 19
$ eightd_has_available_keys <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F...
$ last_reported <int> 1599609894, 1599609924, 1599610071, 1599622522, 1599622911, 1599...
$ num_docks_disabled <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
$ num_ebikes_available <int> 7, 2, 0, 8, 0, 2, 4, 1, 8, 5, 3, 13, 4, 2, 0, 1, 6, 8, 4, 1, 8, ...
$ is_renting <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
$ num_docks_available <int> 11, 10, 17, 10, 6, 8, 3, 4, 4, 13, 11, 3, 13, 16, 6, 5, 5, 4, 14...
$ is_installed <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
$ num_bikes_disabled <int> 2, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0...
$ legacy_id <chr> "1440914125193298376", "1440913821215146858", "14409139061499659...
$ station_id <chr> "1440914125193298376", "1440913821215146858", "14409139061499659...
$ is_returning <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
$ station_status <chr> "active", "active", "active", "active", "active", "active", "act...
$ num_bikes_available <int> 7, 2, 0, 8, 0, 2, 4, 1, 8, 5, 3, 13, 4, 2, 0, 1, 6, 8, 4, 1, 8, ...
$ last_updated <dttm> 2020-12-15 14:55:05, 2020-12-15 14:55:05, 2020-12-15 14:55:05, ...
$ year <dbl> 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020...
$ month <dbl> 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, ...
$ day <int> 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, ...
$ hour <int> 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, ...
$ minute <int> 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, ...
で、データをjoin。
pdx_stations <- full_join(pdx_station_info, pdx_station_status, by = "station_id") %>% select(id = station_id, lon, lat, num_bikes_available, num_docks_available) %>% mutate(type = "docked")
ステーションの座標を求めます。
pdx_free_bikes <- get_free_bike_status("https://gbfs.biketownpdx.com/gbfs/gbfs.json", output = "return") %>% select(id = bike_id, lon, lat) %>% mutate(num_bikes_available = 1, num_docks_available = NA, type = "free")
最後にデータを合わせます。
pdx_full <- bind_rows(pdx_stations, pdx_free_bikes)
最後は、leafletでプロットします。
library(leaflet) leaflet(pdx_full) %>% addTiles() %>% addCircleMarkers(lng=~lon,lat=~lat, radius = ~num_bikes_available, color="blue", weight=2) %>% addCircleMarkers(lng=~lon,lat=~lat, radius = ~num_docks_available, color="red", weight=2)
インタラクティブな地図の完成です。
データは、更新されていくようなので、定期的にみてみると面白いかも。
4. さいごに
こういう情報が公開されていると、有効に活用できそうですね。