-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
152 lines (107 loc) · 4.34 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
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
library(ggplot2)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
#,out.width = "400px"
)
```
# graphhopper-R
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/graphhopper)](https://CRAN.R-project.org/package=graphhopper)
[![github_status_badge](https://img.shields.io/badge/github-0.1.3-blue.svg)](https://github.com/crazycapivara/graphhopper-r/releases/latest)
[![Travis build status](https://travis-ci.org/crazycapivara/graphhopper-r.svg?branch=master)](https://travis-ci.org/crazycapivara/graphhopper-r)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R build status](https://github.com/crazycapivara/graphhopper-r/workflows/R-CMD-check/badge.svg)](https://github.com/crazycapivara/graphhopper-r/actions)
<!-- badges: end -->
An R Interface to the [GraphHopper](https://www.graphhopper.com/) Directions API
The purpose of {graphhopper} is to provide a quick and easy access to the GraphHopper Directions API. Responses can be converted into simple feature (sf) objects in a convenient way. The package is not a complete wrapper of the API. Currently it mainly supports the API also included in [GraphHopper's Open Source Routing Engine](https://github.com/graphhopper/graphhopper). New features will be added continuously.
Dive into the [documentation here](https://crazycapivara.github.io/graphhopper-r/).
## Installation
Install the release version from [CRAN](https://cran.r-project.org/) with:
```r
install.packages("graphhopper")
```
Install the development version from [GitHub](https://github.com/) with:
```r
# install.packages("remotes")
remotes::install_github("crazycapivara/graphhopper-r")
```
## Get started
Run your own GraphHopper instance (with data of Berlin):
```bash
docker run --name gh --rm -p 8989:8989 -d graphhopper/graphhopper:2.0
```
### Setup
```{r example}
library(graphhopper)
API_URL <- "http://localhost:8989"
# API_URL <- "https://graphhopper.com/api/1/"
gh_set_api_url(API_URL)
info <- gh_get_info()
info$version
info$data_date
gh_bbox(info)
```
If you use the [GraphHopper Web Service](https://graphhopper.com/api/1/) instead of a local instance it is recommended that you store your API key in an environment variable called `GH_API_KEY`. Alternatively, you can pass the key as parameter to the `gh_get_*` functions.
### Route
Get a route in Berlin:
```{r route-example}
start_point <- c(52.592204, 13.414307)
end_point <- c(52.539614, 13.364868)
(route <- gh_get_route(list(start_point, end_point)) %>%
gh_as_sf())
ggplot(data = route) +
geom_sf() +
theme(axis.text.x = element_text(angle = 45))
route$time
via_point <- c(52.545461, 13.435249)
route2 <- gh_get_route(list(start_point, via_point, end_point))
gh_time_distance(route2)
ggplot(data = gh_as_sf(route2)) +
geom_sf() +
theme(axis.text.x = element_text(angle = 45))
gh_points(route2) %>%
head()
gh_instructions(route2)[, c("lon", "lat", "gh_id", "gh_end_id", "text", "distance")] %>%
head()
```
### Shortest path tree
```{r spt-example}
start_point <- c(52.53961, 13.36487)
points_sf <- gh_get_spt(start_point, time_limit = 180) %>%
gh_as_sf() %>%
dplyr::mutate(time = (time / 1000 / 60))
ggplot() +
geom_sf(data = points_sf, aes(colour = time), size = 0.5) +
theme(axis.text.x = element_text(angle = 45))
```
Also query previous nodes to plot the network:
```{r spt-example-lines}
(columns <- gh_spt_columns(
prev_longitude = TRUE,
prev_latitude = TRUE,
prev_time = TRUE
))
lines_sf <- gh_get_spt(end_point, time_limit = 240, columns = columns) %>%
dplyr::mutate(mean_time = ((time + prev_time) / 2) / 1000 / 60) %>%
gh_spt_as_linestrings_sf()
ggplot() +
geom_sf(data = lines_sf, aes(color = mean_time), size = 1) +
theme(axis.text.x = element_text(angle = 45))
```
### Isochrone
```{r isochrone-example}
start_point <- c(52.53961, 13.36487)
isochrone_sf <- gh_get_isochrone(start_point, time_limit = 180) %>%
gh_as_sf()
ggplot() +
geom_sf(data = isochrone_sf, fill = "yellow") +
geom_sf(data = points_sf, aes(colour = time), size = 0.5) +
theme(axis.text.x = element_text(angle = 45))
```