Convert/export googleway output to data frame

南楼画角 提交于 2019-11-28 01:36:24

Google's Distance API

is a service that provides travel distance and time for a matrix of origins and destinations.

That is, you will get a distance for all the possible origin & destination combinations.

Given your description

My desired output is distance and time of 1o pairs of lat/long (e.g., the 1st element of origins and 1st element of destinations, 2nd element of origins and 2nd element of destinations, etc.)

You actually want just one value per origin/destination pair.

Also, the API can only accept one request at a time, so if you want to iterate over all O/D pairs, and all transport modes, you need to use a loop

Example

library(googleway)

set_key("your_api_key")

## iterate over each row of origins/destinaions
lst <- lapply(1:nrow(origins), function(x) {

    google_distance(origins = c(origins[x, "V1"], origins[x,"V2"]), 
                    destinations = c(destinations[x, "V1"], destinations[x, "V2"]),
                    mode = "driving",  ## you can only do one mode at a time
    )

})

## in the above iteration, we used 'lapply', so our results are stored in a list
## you have to access the specific elements/results from that list
lst_elements <- lapply(lst, function(x){
    stats::setNames(
        cbind(
          distance_elements(x)[[1]][['duration']],
          distance_elements(x)[[1]][['distance']]
        )
        , c("duration_text", "duration_value", "distance_text", "distance_value")
    )
})

## then you can start to create your data.frames (or data.table in this case)
dt_durations <- data.table::rbindlist(lst_elements)

#      duration_text duration_value distance_text distance_value
#  1:        17 mins            993       17.6 km          17589
#  2: 1 hour 47 mins           6429        158 km         158198
#  3:        33 mins           2009       38.6 km          38630
#  4:         8 mins            504        2.5 km           2466
#  5:         4 mins            225        1.5 km           1486
#  6:          1 min              1           2 m              2
#  7:        22 mins           1312       19.5 km          19495
#  8:        27 mins           1630       27.1 km          27094
#  9:        47 mins           2845       61.0 km          61024
# 10:         6 mins            364        7.0 km           7001

You'll have to do a similar 'loop' for iterating over the different modes


Going Further

If you want, you can also use the directions API to get driving routes between them

lst <- lapply(1:nrow(origins), function(x) {

    google_directions(origin = c(origins[x, "V1"], origins[x,"V2"]), 
                    destination = c(destinations[x, "V1"], destinations[x, "V2"]),
                    mode = "driving",  ## you can only do one mode at a time
    )

})

lst_elements <- lapply(lst, function(x){
    data.frame(
        polyline = direction_polyline(x)
    )
})

dt_routes <- data.table::rbindlist(lst_elements)


df_distances <- cbind(origins, destinations)
df_distances <- stats::setNames(df_distances, c("origin_lat", "origin_lon", "destination_lat", "destination_lon"))
df_distances <- cbind(df_distances, dt_routes, dt_durations)
df_distances$colour <- "blue" ## for colouring some markers
df_distances$info <- paste0("<b>Duration:</b>", df_distances$distance_value, 
                            "<br><b>Distance:</b>", df_distances$duration_value)

set_key("your_api_key", api = "map")

google_map(data = df_distances) %>%
    add_markers(lat = "origin_lat", lon = "origin_lon") %>%
    add_markers(lat = "destination_lat", lon = "destination_lon", colour = "colour") %>%
    add_polylines(polyline = "polyline", info_window = "info")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!