# osrm: Interface Between R and the OpenStreetMap-Based Routing Service OSRM

Timothée Giraud

<figure><img src="image_1.png" /></figure>

## To cite this version:

Timothée Giraud. osrm: Interface Between R and the OpenStreetMap-Based Routing Service OSRM. Journal of Open Source Software, 2022, 7 (78), pp.4574. 10.21105/joss.04574. hal-04142570

HAL Id: hal-04142570

https://hal.science/hal-04142570v1

Submitted on 10 Apr 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_2.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
# osrm: Interface Between R and the OpenStreetMap-Based Routing Service OSRM

Timothée Giraud

1 Centre National de la Recherche Scientifique, France Corresponding author

DOI: 10.21105/joss.04574

Software

- Review

<figure><img src="image_3.png" /></figure>

- Repository

<figure><img src="image_4.png" /></figure>

- Archive

<figure><img src="image_5.png" /></figure>

Editor: Jayaram Hariharan

<figure><img src="image_6.png" /></figure>

<figure><img src="image_7.png" /></figure>

Reviewers:

- @JosiahParry

- @mikemahoney218

- @wcjochem

Submitted: 14 June 2022

Published: 27 October 2022

License

Authors of papers retain copyright and release the work under a Creative Commons Attribution 4.0 International License (CC BY 4.0).

## Summary

osrm is an interface between R and the Open Source Routing Machine (OSRM) API. OSRM (Luxen & Vetter, 2011) is a routing service based on OpenStreetMap (OSM) data. This package enables the computation of routes, trips, isochrones and travel distances matrices (travel times and kilometric distances) based on the OSM road network. It contains five functions that interface with OSRM services:

- osrmTable() uses the *table* service to query time/distance matrices,

- osrmRoute() uses the *route* service to query routes,

- osrmTrip() uses the *trip* service to query trips,

- osrmIsochone() and osrmIsodistance() use multiple osrmTable() calls to create isochrones or isodistance polygons.

An instance of OSRM can be installed on a local or remote server, allowing free and heavy usage of the routing engine.

## Statement of need

Distance based computations and models are at the core of many spatial analysis operations in various scientific fields. The simplest distance metric is the Euclidean distance (or distance as the crow flies) which is easy and inexpensive to compute. The use of this simple metric may be well suited to study some phenomenon, such as species distribution or pollution diffusion. But whenever research aims at studying human activities (transport of persons or goods for example) it is common to use more realistic metrics based on road distance or travel time.

To compute these metrics with R one has to use packages that interface routing engines. Most routing engines are commercial, use tokens to limit the number of requests, or heavily restrict the usage of derived datasets. The use of open source software based on open source data enables a high level of transparency useful to research works that aim at reproducibility.

We argue that osrm offers such a level of transparency by relying on the open source software OSRM, which itself uses the open data source OSM.

osrm is already used in various fields such as transport (Danesi & Tengattini, 2020; Fernandes Barroso et al., 2021; Savaria et al., 2021), education (Odell, 2017), health (Chen et al., 2021; Rice et al., 2021; Snyder & Smucker, 2022; Wisch et al., 2022), applied geography (Kandlbinder et al., 2019; Oberst & Voigtländer, 2021; Wieland, 2018, 2021), environmental science (Hickisch et al., 2019; E. Walker et al., 2021), urban planning (Ballantyne et al., 2022; Dey et al., 2022; Xu, 2019) and linguistics (Jeszenszky et al., 2019).

The use of osrm is also suggested by the package for sustainable transport planning stplanr (Lovelace & Ellison, 2018)
## State of the field

Several packages exist to compute routes, trips or distance matrices. Most of them rely on commercial and non-free software and use non-free data. See for example hereR (Unterfinger, 2021) that uses here services, gmapsdistance (Azuero Melo & Zarruk, 2022), googleway (Cooley, 2022) and mapsapi (Dorman, 2022) that use Google Maps Platform or mapboxapi (K. Walker, 2022) that relies on Mapbox. Using these packages imposes many restrictions on data extraction, analysis and sharing. Other packages use open source routing engines and open data: graphhopper (Kuethe, 2021) uses GraphHopper, opentripplanner (Morgan et al., 2019) uses OpenTripPlanner, valhallr (Belanger, 2021) uses Valhalla. osrmr (Staempfli & Strauss, 2021) uses OSRM, however it exposes only a small subset of OSRM services and does not handle spatial data formats. Among these packages, osrm has the advantage of using OSRM, which is easy to install and run on a local or remote server, to give access to most OSRM services and to handle spatial data formats.

## Features

This is a short overview of the main features of osrm. The dataset used here is shipped with the package and contains a sample of 100 random pharmacies in Berlin (© OpenStreetMap contributors) stored in a geopackage file.

* `osrmTable()` gives access to the *table* OSRM service. In this example we use this function to get the median time needed to access any pharmacy from any other pharmacy.

```r
library(osrm)
```

```r
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyrig
```

```r
## Routing: OSRM - http://project-osrm.org/
```

```r
library.sf)
```

```r
## Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE
```

```r
pharmacy <- st_read(system.file("gpkg/apotheke.gpkg", package = "osrm"),
                     quiet = TRUE)
```

```r
travel_time <- osrmTable(loc = pharmacy)
```

```r
travel_time$durations[1:5,1:5]
```

```r
## 1 2 3 4 5
```

```r
## 1 0.0 21.1 33.4 21.2 12.6
```

```r
## 2 22.1 0.0 42.3 16.1 20.2
```

```r
## 3 33.0 43.0 0.0 30.5 27.4
```

```r
## 4 20.1 15.3 29.7 0.0 12.7
```

```r
## 5 10.2 20.3 26.8 12.3 0.0
```

```r
diag(travel_time$durations) <- NA
```

```r
median(travel_time$durations, na.rm = TRUE)
```

```r
## [1] 21.4
```

The median time needed to access any pharmacy from any other pharmacy is 21.4 minutes.

* `osrmRoute()` is used to compute the shortest route between two points. Here we compute the shortest route between the two first pharmacies.

```r
 route <- osrmRoute(src = pharmacy[1, ], dst = pharmacy[2, ])
```

```r
## Simple feature collection with 1 feature and 4 fields
```

```r
## Geometry type: LINESTRING
```
```r
## Dimension: XY
## Bounding box: xmin: -13170.51 ymin: 5837172 xmax: -3875.771 ymax: 5841047
## Projected CRS: WGS 84 / UTM zone 34N
## src dst duration distance geometry
## 1_2 1 2 21.11667 12.348 LINESTRING (-13170.51 58410...)
```

This route is 12.3 kilometers long and it takes 21.1 minutes to drive.

```r
plot(st_geometryCHO route))
plot(st_geometrypharmacy[1:2,]), pch = 20, add = T, cex = 1.5)
```

<figure><img src="image_8.png" /><figcaption>Figure 1: Shortest route between two points.</figcaption></figure>

* `osrmTrip()` can be used to resolve the travelling salesman problem, it gives the shortest trip between a set of unordered points. In this example we want to obtain the shortest trip between the first five pharmacies.

```r
(trips <- osrmTrip(loc = pharmacy[1:5, ], overview = "full"))
## [[1]]
## [[1]]$trip
## Simple feature collection with 5 features and 4 fields
## Geometry type: LINESTRING
## Dimension: XY
## Bounding box: xmin: -13431.24 ymin: 5837172 xmax: -3875.582 ymax: 5856332
## Projected CRS: WGS 84 / UTM zone 34N
## start end duration distance geometry
## 1 1 2 21.11667 12.3480 LINESTRING (-13170.77 58410...)
## 2 2 4 16.10833 8.4273 LINESTRING (-3875.582 58379...)
## 3 4 3 29.69000 18.1448 LINESTRING (-7444.513 58427...)
## 4 3 5 27.39833 16.4265 LINESTRING (-8027.41 585621...)
## 5 5 1 10.15333 4.2289 LINESTRING (-11716.36 58435...)
```
```r
## 1 1      0      4 MULTIPOLYGON (((13.43743 52...)
## 2 2      4      6 MULTIPOLYGON (((13.42356 52...)
## 3 3      6      8 MULTIPOLYGON (((13.40345 52...)
## 4 4      8     10 MULTIPOLYGON (((13.4077 52...)
## 5 5     10     12 MULTIPOLYGON (((13.42257 52...)

bks <- sort unique(c(iso$isomin, iso$isomax)))
pals <- hcl.colorS(n = length(bks) - 1, palette = "Light Grays", rev = TRUE)
plot(iso["isomax"], breaks = bks, pal = pals,
      main = "Isochrones (in minutes)", reset = FALSE)
points(x = 13.43, y = 52.47, pch = 4, lwd = 2, cex = 1.5)
```

<figure><img src="image_9.png" /><figcaption>Figure 3: Isochrone map.</figcaption></figure>

## References

Azuero Melo, R., & Zarruk, D. (2022). *gmapsdistance: Distance and Travel Time Between Two Points from Google Maps*. https://CRAN.R-project.org/package=gmapsdistance

Ballantyne, P., Singleton, A., Dolega, L., & Credit, K. (2022). A framework for delineating the scale, extent and characteristics of American retail centre agglomerations. *Environment and Planning B: Urban Analytics and City Science*, 49(3), 1112–1128. https://doi.org/10.1177/23998083211040519

Belanger, C. (2021). *valhallr: A Tidy Interface to the 'Valhalla' Routing Engine*. https://CRAN.R-project.org/package=valhallr

Chen, E. M., Ahluwalia, A., Parikh, R., & Nwanyanwu, K. (2021). Ophthalmic emergency department visits: Factors associated with loss to follow-up. *American Journal of Ophthalmology*, 222, 126–136. https://doi.org/10.1016/j.ajo.2020.08.038

Cooley, D. (2022). *googleway: Accesses Google Maps APIs to Retrieve Data and Plot Maps*.
https://CRAN.R-project.org/package=googleway

Danesi, A., & Tengattini, S. (2020). Evaluating accessibility of small communities via public transit. *Archives of Transport, 56*, 59–72. https://doi.org/10.5604/01.3001.0014.5601

Dey, A., Heger, A., & England, D. (2022). Urban fire station location planning using predicted demand and service quality index. *International Journal of Data Science and Analytics*, 1–16. https://doi.org/10.1007/s41060-022-00328-x

Dorman, M. (2022). *mapsapi: 'sf'-Compatible Interface to 'Google Maps' APIs*. https://CRAN.R-project.org/package=mapsapi

Fernandes Barroso, J. M., Albuquerque Oliveira, J. L., & De Oliveira Neto, F. M. (2021). O uso de dados de sistemas de monitoramento automático de tráfego para obter séries temporais dia-a-dia de volumes de tráfego e fluxos origem-destino em redes urbanas. *TRANSPORTES, 29*(2), 2385. https://doi.org/10.14295/transportes.v29i2.2385

Hickisch, R., Hodgetts, T., Johnson, P. J., Sillero-Zubiri, C., Tockner, K., & Macdonald, D. W. (2019). Effects of publication bias on conservation planning. *Conservation Biology, 33*(5), 1151–1163. https://doi.org/10.1111/cobi.13326

Jeszenszky, P., Hikosaka, Y., Imamura, S., & Yano, K. (2019). Japanese lexical variation explained by spatial contact patterns. *ISPRS International Journal of Geo-Information, 8*(9). https://doi.org/10.3390/ijgi8090400

Kandlbinder, K., Miller, N., & Sklarz, M. (2019). Leveling the playing field: Out-of-town buyer premiums in US housing markets over time. *International Journal of Housing Markets and Analysis, 12*. https://doi.org/10.1108/IJHMA-02-2018-0017

Kuethe, S. (2021). *graphhopper: An R Interface to the 'GraphHopper' Directions API*. https://CRAN.R-project.org/package=graphhopper

Lovelace, R., & Ellison, R. (2018). stplanr: A Package for Transport Planning. *The R Journal, 10*(2), 7–23. https://doi.org/10.32614/RJ-2018-053

Luxen, D., & Vetter, C. (2011). Real-time routing with OpenStreetMap data. *Proceedings of the 19th ACM SIGSPATIAL International Conference on Advances in Geographic Information Systems*, 513–516. https://doi.org/10.1145/2093973.2094062

Morgan, M., Young, M., Lovelace, R., & Hama, L. (2019). OpenTripPlanner for R. *Journal of Open Source Software, 4*(44), 1926. https://doi.org/10.21105/joss.01926

Oberst, C., & Voigtländer, M. (2021). Ein hedonischer mietpreisindex für einzelhandelsim-
mobilien in deutschland: Methodik und erste ergebnisse. *IW-Trends - Vierteljahresschrift Zur Empirischen Wirtschaftsforschung, 48*(4), 63–78. https://doi.org/10.2373/1864-810X.21-04-05

Odell, E. (2017). Lonely schools: The relationship between geographic isolation and academic attainment. *Educational Research, 59*(3), 257–272. https://doi.org/10.1080/00131881.2017.1339285

Rice, W. S., Labgold, K., Peterson, Q. T., Higdon, M., & Njoku, O. (2021). Sociodemographic and service use characteristics of abortion fund cases from six states in the U.S. southeast. *International Journal of Environmental Research and Public Health, 18*(7). https://doi.org/10.3390/ijerph18073813

Savaria, M., Apparicio, P., & Carrier, M. (2021). Assessing filtered permeability around the globe: The unknown beloved principle of cycling cities. *Transportation Research Part D: Transport and Environment, 97*, 102964. https://doi.org/10.1016/j.trd.2021.102964

Snyder, M., & Smucker, B. J. (2022). Metamodel optimization of a complex, rural–urban emergency medical services system. *Simulation Modelling Practice and Theory, 118*, 102526. https://doi.org/10.1016/j.simpat.2022.102526
Staempfli, A., & Strauss, C. (2021). osrmr: Wrapper for the 'OSRM' API. https://CRAN.R-project.org/package=osrmr

Unterfinger, M. (2021). hereR: 'sf'-Based Interface to the 'HERE' REST APIs. https://CRAN.R-project.org/package=hereR

Walker, E., Bormpoudakis, D., & Tzanopoulos, J. (2021). Assessing challenges and opportunities for schools' access to nature in England. *Urban Forestry & Urban Greening, **61*, 127097. https://doi.org/10.1016/j.ufug.2021.127097

Walker, K. (2022). mapboxapi: R Interface to 'Mapbox' Web Services. https://CRAN.R-project.org/package=mapboxapi

Wieland, T. (2018). A hurdle model approach of store choice and market area analysis in grocery retailing. *Papers in Applied Geography, **4**(4), 370–389. https://doi.org/10.1080/23754931.2018.1519458

Wieland, T. (2021). Identifying the determinants of store choice in a multi-channel environment: A hurdle model approach. *Papers in Applied Geography, **7**(4), 343–371. https://doi.org/10.1080/23754931.2021.1895875

Wisch, J. K., Roe, C. M., Babulal, G. M., Metcalf, N., Johnson, A. M., Murphy, S., Hicks, J., Doherty, J. M., Morris, J. C., & Ances, B. M. (2022). Naturalistic driving measures of route selection associate with resting state networks in older adults. *Scientific Reports, **12*(1), 1–8. https://doi.org/10.1038/s41598-022-09919-x

Xu, J. (2019). From walking buffers to active places: An activity-based approach to measure human-scale urban form. *Landscape and Urban Planning, **191*, 103452. https://doi.org/10.1016/j.landurbplan.2018.10.008