stats_noob
stats_noob

Reputation: 5907

"Bus Distance" in R?

In a specific city, is there a way to find out the amount of kilometers needed to travel between two points (e.g. 2 pairs of lat/long) by bus?

I found the following post that shows how to find out walking and driving distance:

#install

remotes::install_github("riatelab/osrm")

# https://www.rdocumentation.org/packages/osrm/versions/3.5.1/topics/osrmRoute 

route1 <-  osrmRoute(src = c(13.412, 52.502),
                     dst = c(13.454, 52.592), osrm.profile = "foot")

route2 <-  osrmRoute(src = c(13.412, 52.502),
                     dst = c(13.454, 52.592), osrm.profile = "car")

route3 <-  osrmRoute(src = c(13.412, 52.502),
                     dst = c(13.454, 52.592), osrm.profile = "bike")

#
route1$distance

But it does not seem like there is a "bus option" here. Is this possible in R?

Thank you!

Upvotes: 1

Views: 86

Answers (1)

caldwellst
caldwellst

Reputation: 5956

You can't use the osrm package because project OSRM does not support bus directions. However the googleway package offers access to the Google Maps API. You can use this to calculate distance using the Distance Matrix API. You cannot specify bus specifically, but can calculate distance using transit, which includes bus and other modes of public transit.

library(googleway)

key <- "your_api_key"

google_distance(
  origin = c(52.502, 13.412),
  destination = c(52.592, 13.454),
  mode = "transit",
  key = key
)
#> $destination_addresses
#> [1] "Alt-Blankenburg 26, 13129 Berlin, Germany"
#> 
#> $origin_addresses
#> [1] "Prinzessinnenstraße 20, 10969 Berlin, Germany"
#> 
#> $rows
#>                            elements
#> 1 13.3 km, 13286, 42 mins, 2507, OK
#> 
#> $status
#> [1] "OK"

Upvotes: 4

Related Questions