michaeldorman / mapsapi

'sf'-Compatible Interface to Google Maps APIs
Other
50 stars 14 forks source link

Departure time conversion for mp_directions #13

Closed zlihong88 closed 3 years ago

zlihong88 commented 3 years ago

Hello, many thanks for your fantastic package. I came across a question about the departure time setting for getting transit routes via mp_directions(). The manual of version 0.4.9 puts the requirement as "The desired time of departure, as POSIXct ". Should I change the local time zone (AEST for my case) into GMT? I have tried different ways to set the departure time and also made it as a future time, but the result is either empty or one object with all NAs. For example, for a pair of origin and destination, the local time is 2024-05-04 07:00:00 AEST, and the corresponding UTC time is 2024-05-03 21:00:00 GMT. I also tried 2024-05-04 07:00:00 GMT in the function, but none of them gave rational outcomes. I only got some realistic routes when I dropped the departure-time argument, but they were based on the real time when I generated them. I want to make the transit routes considering the departure-time for my subsequent analysis. Could you explain more on how to set it? Thank you so much!!!

michaeldorman commented 3 years ago

Thanks for letting me know! I will look into it and get back to you

michaeldorman commented 3 years ago

I think the reason is that the year 2024 is too far into the future, so public transport data are not available. Google Maps API returns a "zero results" response in such case.

For example, the following:

library(mapsapi)

doc = mp_directions(
  origin = "Beer-Sheva",
  destination = "Rehovot",
  mode = "transit",
  departure_time = as.POSIXct("2024-07-17 13:00:00", tz = "Asia/Jeruzalem"),
  key = "..."
)
doc

Returns a "zero results" response:

{xml_document}
<DirectionsResponse>
[1] <status>ZERO_RESULTS</status>
[2] <available_travel_mode>DRIVING</available_travel_mode>
[3] <available_travel_mode>WALKING</available_travel_mode>
[4] <available_travel_mode>BICYCLING</available_travel_mode>
[5] <geocoded_waypoint>\n  <geocoder_status>OK</geocoder_status>\n  <type>locality</ty ...
[6] <geocoded_waypoint>\n  <geocoder_status>OK</geocoder_status>\n  <type>locality</ty ...

A more proximate departure time, or using mode="driving", do work. In any case you are assuming that the present-day public transport time schedules will be in effect in 2024, so perhaps you can just use a more proximate date + same hour and day of week.

michaeldorman commented 3 years ago

P.S. Departure time can be specified in either local time or UTC, the results should be identical.

zlihong88 commented 3 years ago

Thank you so much for the reply. By the way, may I ask that why the departure time column in the route outcome is empty when I set departure time for driving mode in the input data and do get some results in the duration_in_traffic column? Is there anything wrong with my input data?

michaeldorman commented 3 years ago

The departure_time field is only returned when mode="transit": https://developers.google.com/maps/documentation/directions/get-directions#Legs

Screenshot from 2021-07-13 17-19-41

zlihong88 commented 3 years ago

Thank you very much!