library(ggplot2) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-" #,out.width = "400px" )
An R Interface to the GraphHopper 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. New features will be added continuously.
Dive into the documentation here.
Install the release version from CRAN with:
install.packages("graphhopper")
Install the development version from GitHub with:
# install.packages("remotes") remotes::install_github("crazycapivara/graphhopper-r")
Run your own GraphHopper instance (with data of Berlin):
docker run --name gh --rm -p 8989:8989 -d graphhopper/graphhopper:2.0
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 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.
Get a route in Berlin:
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()
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:
(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))
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.