forked from rodazuero/gmapsdistance
-
Notifications
You must be signed in to change notification settings - Fork 3
/
vignette.Rmd.orig
174 lines (144 loc) · 6.63 KB
/
vignette.Rmd.orig
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
title: "gmapsdistance package in use"
author: "Jindra Lacko"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
toc: true
self_contained: no
vignette: >
%\VignetteIndexEntry{Examples of using `{gmapsdistance}` package}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = T,
fig.align="center",
dpi = 150,
out.width = "100%",
fig.width = 8,
fig.height = 4,
fig.path = "./")
```
## Setting up API key
The [Google Distance Matrix API](https://mapsplatform.google.com/) requires registration, and an API key is required in each function call. It is possible to store the key for duration of a R session by using `gmapsdistance::set.api.key()` call.
Note that in order to keep the maintainers API key private it has been stored locally in an environment variable; in the most frequent use cases it can be used in plain text.
``` {r set-up-api}
library(gmapsdistance)
set.api.key(Sys.getenv("GOOGLE_API_KEY"))
```
## Distance between cities
In this example we will compute the driving distance between Washington DC, and New York City. The code returns the `Time`, the `Distance` and the `Status` of the query (`OK` if it was successful). The calculation will be made using the "driving" method, which is the default.
``` {r plain-vanilla}
results = gmapsdistance(origin = "Washington DC",
destination = "New York City NY")
results
```
## Walking distance between GPS coordinates
In this example we will compute the driving distance between the Greek cities of
Marathon and Athens. We show that the function is able to handle LAT-LONG coordinates.
``` {r marathon}
results = gmapsdistance(origin = "38.1621328 24.0029257",
destination = "37.9908372 23.7383394",
mode = "walking")
results
```
## Distance matrix at a specific departure time
This example computes the travel distance and time matrices between two vectors of cities at a specific departure time
``` {r matrix-departure}
results = gmapsdistance(origin = c("Washington DC", "New York NY",
"Seattle WA", "Miami FL"),
destination = c("Los Angeles CA", "Austin TX",
"Chicago IL", "Philadelphia PA"),
mode = "bicycling",
# departure time as seconds from Unix Epoch (1970-01-01)
departure = 1740000000))
results
# Equivalently
results = gmapsdistance(origin = c("Washington DC", "New York NY",
"Seattle WA", "Miami FL"),
destination = c("Los Angeles CA", "Austin TX",
"Chicago IL", "Philadelphia PA"),
mode = "bicycling",
dep_date = "2025-02-19",
dep_time = "22:20:00"))
results
```
## Long format of distance matrix
This example computes the travel distance and time matrices between two vectors of cities and return the results in long format
``` {r matrix-long}
origin = c("Washington DC", "New York NY", "Seattle WA", "Miami FL")
destination = c("Los Angeles CA", "Austin TX", "Chicago IL")
results = gmapsdistance(origin,
destination,
mode = "driving",
shape = "long")
results
```
## Distance matrix between GPS coordinates
This example computes the travel distance and time between two vectors of cities defined as LAT-LONG coordinates.
``` {r matrix-coordinates}
origin = c("40.431478 -80.0505401", "33.7678359 -84.4906438")
destination = c("43.0995629 -79.0437609", "41.7096483 -86.9093986")
results = gmapsdistance(origin,
destination,
mode="bicycling",
shape="long")
results
```
## Combination of distances
This example computes the travel distance and time between two vectors of cities using the 'combinations' option.
``` {r combinations}
# 1. Pairwise
or = c("Washington DC", "New York NY", "Seattle WA", "Miami FL")
des = c("Los Angeles CA", "Austin TX", "Chicago IL", "Philadelphia PA")
results = gmapsdistance(origin = or,
destination = des,
mode = "bicycling",
combinations = "pairwise")
results
# 2. All combinations of origins and destinations in wide format
results = gmapsdistance(origin = or,
destination = des,
mode = "bicycling",
combinations = "all",
shape = "wide")
results
results = gmapsdistance(origin = or,
destination = des,
mode = "bicycling",
combinations = "all",
shape = "long")
results
```
## Using optional parameters - traffic model and avoidance
This example show the use of traffic model and avoidance parameters, while setting the API key directly in function call.
Note that in order to keep the maintainers API key private it has been stored locally in an environment variable; in the most frequent use cases it can be used in plain text.
``` {r optional-params}
APIkey <- Sys.getenv("GOOGLE_API_KEY") # your actual API key would go here...
# Time and distance using a 'pessimistic' traffic model.
results = gmapsdistance(origin = c("Washington DC", "New York NY"),
destination = c("Los Angeles CA", "Austin TX"),
mode = "driving",
dep_date = "2023-11-14",
dep_time = "23:13:20",
traffic_model = "pessimistic",
shape = "long",
key=APIkey)
results
# Time and distance avoiding 'tolls'.
results = gmapsdistance(origin = c("Washington DC", "New York NY"),
destination = c("Los Angeles CA", "Austin TX"),
mode = "driving",
avoid = "tolls",
key=APIkey)
results
```
## Using "foreign" place names
It is perfectly possible to use names in non-ASCII characters; care must be however made that all characters are encoded in UTF-8 though. This may not be the obvious first choice of encoding, especially in Windows environments.
``` {r kyiv}
results = gmapsdistance(origin = "вулиця Хрещатик, Київ",
destination = "Майдан Незалежності, Київ",
mode = "walking")
results
```