Using Routing API to get public transport fares, total travel time and distance?

Hello!

I working on a project which requires me to calculate the total travel time, distance and fares incurred when travelling on Singapore’s public transport from many origins to one destination.

I was exploring the Onemap APIs and chanced upon the routing service API which I think can potentially help me get the data I need.

I am working with R to extract the data from the API. Currently, my plan is to experiment if I can get travel time, distance and fare for one point to my destination. If successful, I aim to generate multiple origins and use a loop to get the information I need. Here is an example of my code:

route_url <- "https://developers.onemap.sg/privateapi/routingsvc/route?"
orchard_rd <- (“1.3039805,103.8320321”)
beauty_word_mrt <- (“1.3416977,103.7759785”)

route_pt <- GET(paste0(route_url, “start=”, orchard_rd, “&end=”, beauty_word_mrt,
“&routeType=pt”, “&token=”, token ,
“&date=2018-09-08&time=21:35:00”,
“&mode=TRANSIT&maxWalkDistance=500”,
“&numItineraries=1” ))

However, I have some questions about the structure of the response and how to extract the data I need:

  1. When I change the parameter “numItineraries=x”, I still get a long list of itineraries (6 to be exact) even if I put x = 1. Is there a way for me to select the most efficient route (in terms of costs and time)?

  2. Also, is the list of itineraries for the response already ordered i.e. itineraries[[1]] is a more efficient route than itineraries[[6]]?

  3. Lastly, I’m wondering if there is a way for me to extract the total distance and time for each route?

I’m not too sure how to interpret response structure but (correct me if I’m wrong) I think the response gives the distances and timing of different segments along each route… Is there any way to piece everything together to give a summary of the total distance and total time?

Would appreciate if anyone would be able to help!

Thanks!

Hi Shaikh

My response to your questions as follows

1. When I change the parameter “numItineraries=x”, I still get a long list of itineraries (6 to be exact) even if I put x = 1. Is there a way for me to select the most efficient route (in terms of costs and time)?

Thanks for the feedback. I would like you to try again. The number of itineraries returned should now be x or less than x.

2. Also, is the list of itineraries for the response already ordered i.e. itineraries[[1]] is a more efficient route than itineraries[[6]]?

Yes.

3. Lastly, I’m wondering if there is a way for me to extract the total distance and time for each route?

Currently there is no such way. We will take your feedback into considerations. For now, just loop through the itineraries to tabulate the total distance and time.

1 Like