michaeldorman / mapsapi

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

Google Directions - leg departure/arrival times #4

Closed ioanpat55 closed 5 years ago

ioanpat55 commented 5 years ago

Hi

thanks for your very helpful R package. I wanted to raise an issue that I haven't managed to resolve so far. When i run my mp_directions code and then produce the output with all journey legs/segments, i can see data like the duration, distance and travel mode of each leg but can't see the departure/arrival time for each leg (data for each row/segment). I know it exists according to Google's online guidance but possibly not included in the package's data extracted? This is something I need for public transport (e.g. my departure is set for 8am but next train leaves at 830am), could you please advise whether mapsapi captures this in its outputs and how to produce it? Many thanks.

michaeldorman commented 5 years ago

I've added departure_time and arrival_time fields for result of the mp_get_routes and mp_get_segments functions. Please see example below, will be happy to hear if you encounter any issues. Thanks!

> # Transit example
> doc = mp_directions(
+   origin = c(34.81127, 31.89277),
+   destination = c(34.781107, 32.085003),
+   mode = "transit",
+   alternatives = TRUE,
+   key = key
+ )
> r = mp_get_routes(doc)
> r
Simple feature collection with 4 features and 11 fields
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: 34.6987 ymin: 31.80558 xmax: 34.82119 ymax: 32.08733
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
    alternative_id leg_id summary distance_m distance_text duration_s  duration_text
1-1              1      1              57010       57.0 km       7040 1 hour 57 mins
2-1              2      1              26258       26.3 km       4826 1 hour 20 mins
3-1              3      1              26260       26.3 km       4858 1 hour 21 mins
4-1              4      1              57458       57.5 km       7480 2 hours 5 mins
    duration_in_traffic_s duration_in_traffic_text      departure_time
1-1                    NA                       NA 2019-02-16 17:27:20
2-1                    NA                       NA 2019-02-16 18:34:14
3-1                    NA                       NA 2019-02-16 18:34:14
4-1                    NA                       NA 2019-02-16 17:27:20
           arrival_time                       geometry
1-1 2019-02-16 19:24:40 LINESTRING (34.81144 31.892...
2-1 2019-02-16 19:54:40 LINESTRING (34.81144 31.892...
3-1 2019-02-16 19:55:12 LINESTRING (34.81144 31.892...
4-1 2019-02-16 19:32:00 LINESTRING (34.81144 31.892...
ioanpat55 commented 5 years ago

Hi, thanks for your very quick response. Just to let you know that unfortunately i still don't get departure and arrival times for each leg in my routes and segments outputs. I tried your sample code too with different coordinates, my version of the code but no luck, I get the previous output without those 2 columns. I also use my private Google key for the key= parameter but i don't think this should matter. Grateful for any other help or advice. Thanks!

michaeldorman commented 5 years ago

Just to make sure - you need to install the development version of mapsapi for the changes to be in effect.

devtools::install_github("michaeldorman/mapsapi")

If this doesn't solve the issue, could you please post a reproducible example? (without the API key) Thanks!

ioanpat55 commented 5 years ago

Hi, sorry for the very slow response. I have installed the developer version and can see the extra data now! However, a note to make, i can see the departure and arrival times for the transport segments only, for walking segments there are no data. I can infer the times from the duration columns which are still there but is there an easy way to extract departure/arrival times for all segments? Many thanks.

michaeldorman commented 5 years ago

Hi, This behavior is due to the design of the Google Maps API - the arrival_time and departure_time fields are only given for steps with travel_mode = TRANSIT. So, the arrival_time and departure_time fields are not available for the walking segments. Please see attached screenshot from the docs and sample API response. It makes sense to infer those time based on duration, like you suggested. Best regards, Michael

Screenshot from 2019-03-12 10-07-17

Screenshot from 2019-03-12 10-07-13