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

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

Awesome Lists containing this project

README

        

---
output: github_document
title: Vallhala for R
---


[![R-CMD-check](https://github.com/Robinlovelace/rvalhalla/actions/workflows/R-CMD-check.yaml/badge.svg)](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
```

![](https://private-user-images.githubusercontent.com/1825120/293484588-fc06d256-17cb-4e1e-9bbf-5f95e0333245.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MDM5ODA0NTgsIm5iZiI6MTcwMzk4MDE1OCwicGF0aCI6Ii8xODI1MTIwLzI5MzQ4NDU4OC1mYzA2ZDI1Ni0xN2NiLTRlMWUtOWJiZi01Zjk1ZTAzMzMyNDUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDIzMTIzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyMzEyMzBUMjM0OTE4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NTQ3MmYwMTNjOGFhMjU1YjkwZWQ4NzkxYTIxZTNkNDMxMzliYjg5ZDhjYTZiMDAxNWMyNTc2YzQ2ZGQwMzgzMCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.g94rkzPF3-Mu7cNdRPfWlEA5qzu6pp-0iGCM_CmbcUc)

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