/rvalhalla

R interface to Valhalla

Primary LanguageR

Vallhala for R

R-CMD-check

This package is based on tests and the demo in the setup.qmd file.

Installation

devtools::install_github("robinlovelace/rvalhalla")

Usage

# 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.

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.

andorra_la_vella = c(1.5218, 42.5075)
pas_de_la_casa = c(1.7333, 42.5425)
# 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}:

# waldo::compare(route2, route3)

Let’s plot them all with ggplot2 (not shown):

library(ggplot2)
ggplot() +
  geom_sf(data = route1, color = "red") +
  geom_sf(data = route2, color = "blue") +
  geom_sf(data = route3, color = "green")

And with {tmap}:

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

Links

This project builds on and was inspired by the following projects, which are worth checking out:

system("quarto render benchmark.qmd")