Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/crazycapivara/graphhopper-r

An R Interface to the graphhopper API
https://github.com/crazycapivara/graphhopper-r

api-client graphhopper r routing-engine rspatial rstats travel-times

Last synced: 30 days ago
JSON representation

An R Interface to the graphhopper API

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r setup, include = FALSE}
library(ggplot2)

knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
#,out.width = "400px"
)
```
# graphhopper-R

[![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)

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))
```