符号化された経路を復号するにはgooglePolylines::decode()が便利
下図のようにGoogle マップで経路検索すると表示される経路を{leaflet}
で描きたい。
Google Maps PlatformのDirection APIを実行して得られる経路はリンク先のアルゴリズムで符号化されている。
符号化された経路内の座標の区切りが理解できておらず復号することができなかったが、googlePolylines::decode()
であっさりできたのでメモしとく。
Direction APIの実行
まずはGoogle Maps PlatformのDirection APIを実行する。以下では皇居ランのコースである平川門〜半蔵門〜桜田門〜二重橋前〜平川門の経路を取得している。
res <- httr::GET( "https://maps.googleapis.com/maps/api/directions/json", query = list( "key" = "[API Key]", "origin" = "35.690362,139.758809", # 平川門 "destination" = "35.690362,139.758809", # 平川門 "waypoints" = stringr::str_c( "via:", "35.684002", ",", "139.744176", "|", # 半蔵門 "via:", "35.6785028", ",", "139.7536378", "|", # 桜田門 "via:", "35.677797", ",", "139.756460" # 二重橋前 ), "mode" = "walking", "units" = "metric" ) ) %>% httr::content()
レスポンスの中身は以下のようになっており、経路情報はroutes$legs
に格納されている。
res %>% dplyr::glimpse() List of 3 $ geocoded_waypoints:List of 5 ..$ :List of 3 .. ..$ geocoder_status: chr "OK" .. ..$ place_id : chr "ChIJWUeeXA6MGGARXRGGM2o02Fk" .. ..$ types :List of 2 ..$ :List of 3 .. ..$ geocoder_status: chr "OK" .. ..$ place_id : chr "ChIJzxK-_nCMGGAReOvi9RN4UcM" .. ..$ types :List of 1 ..$ :List of 3 .. ..$ geocoder_status: chr "OK" .. ..$ place_id : chr "ChIJb4zVw_SLGGARZjRUaE4GF4c" .. ..$ types :List of 1 ..$ :List of 3 .. ..$ geocoder_status: chr "OK" .. ..$ place_id : chr "ChIJkxeHCfSLGGAR7gLWYecxb0M" .. ..$ types :List of 1 ..$ :List of 3 .. ..$ geocoder_status: chr "OK" .. ..$ place_id : chr "ChIJWUeeXA6MGGARXRGGM2o02Fk" .. ..$ types :List of 2 $ routes :List of 1 ..$ :List of 7 .. ..$ bounds :List of 2 .. ..$ copyrights : chr "Map data ©2020" .. ..$ legs :List of 1 .. ..$ overview_polyline:List of 1 .. ..$ summary : chr "都道301号" .. ..$ warnings :List of 1 .. ..$ waypoint_order : list() $ status : chr "OK"
符号化された経路の取得
レスポンスの中身から符号化された経路を取得するには以下のようにする。(よりシンプルに取得する書き方、ご教示ください。。。)
routes <- res$routes %>% purrr::map("legs") %>% purrr::flatten() %>% purrr::map("steps") %>% purrr::flatten() %>% purrr::map("polyline") %>% purrr::map_chr("points")
routes %>% head() [1] "ywyxEgs_tYB?RR" [2] "awyxEsr_tYARI~@CPIjAGj@Q|BAPMvACT" [3] "iyyxEwc_tYBH@DDNTx@DLLd@@FBFPn@@BL^BLX|@FP@BBFFNBHRf@JXDFN`@JVJVHR" [4] "{qyxEqr~sYD?DHFL@@?@EBGDKHGDIF" [5] "qryxEsp~sYDLBL@L@H?H?LAVADARENADCHU\\EDIHAHEHKZQf@CF?@?D?HPL" [6] "styxEgf~sYRHHCFCJEBAHD"
座標情報の取得
符号化された経路情報から座標情報を取得するにはgooglePolylines::decode()
を利用する。
googlePolylines::decode(routes[1]) [[1]] lat lon 1 35.69037 139.7588 2 35.69035 139.7588 3 35.69025 139.7587
結果はlist
で返ってくるためpurrr::map_dfr()
でdata.frame
にする
routes_coords <- routes %>% googlePolylines::decode() %>% purrr::map_dfr(~.)
routes_coords %>% head() %>% knitr::kable()
lat | lon |
---|---|
35.69037 | 139.7588 |
35.69035 | 139.7588 |
35.69025 | 139.7587 |
35.69025 | 139.7587 |
35.69026 | 139.7586 |
35.69031 | 139.7582 |
{leaflet}で可視化
最後に{leaflet}で可視化して確認。
routes_coords %>% leaflet::leaflet() %>% leaflet::addTiles("http://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png", attribution = "<a href='http://maps.gsi.go.jp/development/ichiran.html' target='_blank'>地理院タイル</a>") %>% leaflet::addPolylines(lng = ~lon, lat = ~lat,)
半蔵門で少しはみ出ているのはご愛敬ということで。。。
(備考)経路をLINESTRING型に変換する
以下はLINESTRING型に変換する際のメモ
geom <- routes_coords %>% dplyr::select(lon, lat) %>% as.matrix(byrow = TRUE) %>% sf::st_linestring() %>% sf::st_simplify() %>% sf::st_sfc() %>% sf::st_set_crs(4326) Geometry set for 1 feature geometry type: LINESTRING dimension: XY bbox: xmin: 139.7427 ymin: 35.67732 xmax: 139.7615 ymax: 35.69061 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs LINESTRING (139.7588 35.69037, 139.7588 35.6903...
データ作成年度:平成23年度
となっており、利用するには鮮度に心配がある。
現在開催されている東京公共交通オープンデータチャレンジではバス停情報も公開されているので、上記のようにすれば最新の経路を取得できるかも。Agoop社が提供する1年分の流動人口ポイントデータも組み合わせて可視化してみたい。