-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
98 lines (76 loc) · 2.81 KB
/
README.Rmd
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
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# openjourney
![](https://lifecycle.r-lib.org/articles/figures/lifecycle-experimental.svg)
The openjourney package allows to query trip durations between cooordinates within Switzerland. It wraps the [Open Journey Planer API](https://opentransportdata.swiss/de/cookbook/open-journey-planner-ojp/) provided by opentransportdata.swiss. It allows to query trip times between multiple locations in a structured manner and use them for data analysis.
## Installation
You can install the the development version from [GitHub](https://github.com/) with:
```{r eval=FALSE}
# install.packages("devtools")
devtools::install_github("tlorusso/openjourney")
```
```{r setup}
library(openjourney)
library(tidyverse)
library(sf)
library(tidygeocoder)
library(xml2)
```
# Usage
## API-Token
To be able to query the API a token is needed. A public key is available in the [documentation](https://opentransportdata.swiss/de/cookbook/open-journey-planner-ojp/#Einleitung) which can be used for a limited number of queries. Private keys with higher rate limits can be created on opentransportdata.swiss.
In our example we'll use the public key.
```{r}
token1 <- paste("Bearer", "57c5dbbbf1fe4d000100001842c323fa9ff44fbba0b9b925f0c052d1", sep = " ")
```
## Query trip durations
Lets get some coordinates for some sample locations.
```{r}
# get coordinates of some locations
coords <- geo(address = c("Zürich Schwamendingerplatz, Switzerland",
"Zürich Stadelhofen, Switzerland",
"Uster Bahnhof, Switzerland",
"Bern Hauptbahnhof, Switzerland",
"Bahnhof Stettbach, Switzerland"),
method ='osm')
```
```{r eval=TRUE, echo=FALSE}
# extract coordinates
coords_list <- coords %>%
select(long,lat) %>%
purrr::transpose()
# set coordinates of the origins
origins <- coords_list[c(1:4)]
# set coordinates of the desination
destinations <- coords_list[c(5)]
# set departure time
dep_time <-paste0(Sys.Date(),"T08:34:40")
# query the trip durations
trips <- get_tripduration(auth=token1,
origin=origins,
origin_id = coords$address[1:4],
destination=destinations,
destination_id=coords$address[5],
time=dep_time,
sys.sleep=5)
```
# Plot trip durations
```{r}
ggplot(trips, aes(duration_min,
paste0(origin_id, "\n - ", destination_id),
size=transfers))+
geom_point()+
theme_minimal()+
scale_fill_viridis_c()+
labs(y="", x="Trip duration (minutes)")+
scale_x_continuous(limits=c(0,100))
```