get_distance: Get network distance

Description Usage Arguments Value Examples

View source: R/get_distance.R

Description

Fetch the distance between sets of origins and destinations using Google Distance Matrix API

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
get_distance(
  data,
  origins,
  destinations,
  mode = c("driving", "walking", "cycling", "transit"),
  departing = FALSE,
  tz = Sys.timezone(),
  model = c("best_guess", "pessimistic", "optimistic"),
  google_api_key = Sys.getenv("google_api_key"),
  verbose = FALSE
)

Arguments

data

a data.frame or tibble containing our origins, destinations or both.

origins

a column in our data.frame containing the origin addresses - can be of form 'lat,lng' or address with postcode. Else a string or vector can be passed.

destinations

a column in our data.frame containing the destination addresses - can be of form 'lat,lng' or address with postcode. Else a string or vector can be passed.

mode

driving, cycling, transit or walking.

departing

departure time can be set in format 'YYYY-MM-DD HH:MM:SS', in the local time of the origin country

tz

if departure time is set, timezone defaults to the user's timezone as per 'Sys.timezone'. Where the timezone of the 'site' differs from the user timezone, specify the 'site' timezone here. tz is a character string that must be a time zone that is recognized by the user's OS.

model

'best_guess' is the default model. Google also provides 'optimistic' and 'pessimistic' models. See google distance matrix API documentation.

google_api_key

google cloud console API key. Ensure this is enabled on the distance matrix API and geocoding API.

Value

a data.frame containing the coordinates, location type and approximate address.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## Not run: 
## Example with a dataframe of origins (lat lng) and a single destination
df <- tibble::tribble(
origin_df <- dplyr::mutate(df, origin = paste0(lat,',',lng))

get_distance(origin_df, origin, 'London Paddington')

# lat     lng origin           driving_distance driving_time
# <dbl>   <dbl> <chr>                       <dbl>        <dbl>
# 1  51.5 -0.0918 51.5131,-0.09182             7877         26.9
# 2  51.5 -0.0172 51.5037,-0.01715            13839         39.3
# 3  51.5 -0.123  51.532,-0.12343              5231         20.0
# 4  51.4 -0.337  51.4447,-0.33749            17944         38.4



## Example with a single dataframe containing postcodes and lat,lng coordinates
pcd_df <- tibble::tribble(
 ~ origin,           ~destination,
"51.5131,-0.09182", 'EC2R 8AH',
"51.5037,-0.01715", 'E14 5AB',
" 51.5320,-0.12343", 'SE1 9SG',
"51.4447,-0.33749", 'SW1A 1AA'
)

get_distance(pcd_df, origin, destination)

#   origin              destination driving_distance driving_time
#   <chr>               <chr>                  <dbl>        <dbl>
# 1 "51.5131,-0.09182"  EC2R 8AH                 515         3.4
# 2 "51.5037,-0.01715"  E14 5AB                  867         3.33
# 3 " 51.5320,-0.12343" SE1 9SG                 5747        24.0
# 4 "51.4447,-0.33749"  SW1A 1AA               16895        40.2

## End(Not run)

Chrisjb/RDistanceMatrix documentation built on Jan. 27, 2021, 9:12 p.m.