https://github.com/robinlovelace/rvalhalla
R interface to Valhalla
https://github.com/robinlovelace/rvalhalla
Last synced: 2 months ago
JSON representation
R interface to Valhalla
- Host: GitHub
- URL: https://github.com/robinlovelace/rvalhalla
- Owner: Robinlovelace
- Created: 2023-12-08T23:15:00.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-14T15:15:34.000Z (over 1 year ago)
- Last Synced: 2024-06-11T16:10:07.967Z (11 months ago)
- Language: R
- Size: 389 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
title: Vallhala for R
---
[](https://github.com/Robinlovelace/rvalhalla/actions/workflows/R-CMD-check.yaml)
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```This package is based on tests and the demo in the [setup.qmd](setup.md) file.
## Installation
```{r install, eval=FALSE}
devtools::install_github("robinlovelace/rvalhalla")
```## Usage
``` bash
# download a file to custom_files and start valhalla
mkdir custom_files
wget -O custom_files/andorra-latest.osm.pbf https://download.geofabrik.de/europe/andorra-latest.osm.pbf
docker stop valhalla_gis-ops
docker rm valhalla_gis-ops
docker run -dt --name valhalla_gis-ops -p 8002:8002 -v $PWD/custom_files:/custom_files -e tile_urls=https://download.geofabrik.de/europe/andorra-latest.osm.pbf ghcr.io/gis-ops/docker-valhalla/valhalla:latest# If the container already exists:
docker start valhalla_gis-ops
```After that navigate to http://localhost:8002/ and you'll see the endpoint.
```{r usage}
library(rvalhalla)
# devtools::load_all() # for development
```Let's calculate a single route in Andorra, between two well known places: Andorra la Vella and Pas de la Casa.
```{r}
andorra_la_vella = c(1.5218, 42.5075)
pas_de_la_casa = c(1.7333, 42.5425)
``````{r}
# Calculate the route
route1 = vh_route(andorra_la_vella, pas_de_la_casa, costing = "pedestrian")
route2 = vh_route(andorra_la_vella, pas_de_la_casafrom = andorra_la_vella, to = pas_de_la_casa, costing = "bicycle")
route3 = vh_route(from = andorra_la_vella, to = pas_de_la_casa, costing = "bicycle", costing_options = list(bicycle = list(use_roads = 0.9)))
```We can compare them with `{waldo}`:
```{r}
# waldo::compare(route2, route3)
```Let's plot them all with `ggplot2` (not shown):
```{r eval=FALSE}
library(ggplot2)
ggplot() +
geom_sf(data = route1, color = "red") +
geom_sf(data = route2, color = "blue") +
geom_sf(data = route3, color = "green")
```And with `{tmap}`:
```{r eval=FALSE}
library(tmap)
tmap_mode("view")
m = tm_shape(route1) +
tm_lines("red", lwd = 6) +
tm_shape(route2) +
tm_lines("blue", lwd = 6) +
tm_shape(route3) +
tm_lines("green", lwd = 6)
m
```
```{r eval=FALSE, echo=FALSE}
tmap_save(m, "map_routes.html")
browseURL("map_routes.html")
```# Links
This project builds on and was inspired by the following projects, which are worth checking out:
- [pyvalhalla](https://github.com/gis-ops/pyvalhalla), a Python wrapper for Valhalla
- The excellent [valhalla API documentation](https://valhalla.github.io/valhalla/api/)
- [valhalla-docker](https://github.com/gis-ops/docker-valhalla), well-maintained Docker images for Valhalla
- [valhallar](https://github.com/chris31415926535/valhallr), an alternative R package for Valhalla
- See [benchmark.md](benchmark.md) for a simple benchmark, that can be reproduced as follows:```r
system("quarto render benchmark.qmd")
```