• Stars
    star
    237
  • Rank 169,885 (Top 4 %)
  • Language
    R
  • License
    GNU General Publi...
  • Created about 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Interface between R and the OpenStreetMap-based routing service OSRM

osrm

CRAN downloads R build status codecov Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed. DOI

Interface Between R and the OpenStreetMap-Based Routing Service OSRM

Description

OSRM is a routing service based on OpenStreetMap data. See http://project-osrm.org/ for more information. This package enables the computation of routes, trips, isochrones and travel distances matrices (travel time and kilometric distance).

This package relies on the usage of a running OSRM service (tested with v5.27.0 of OSRM).

You can run your own instance of OSRM following guidelines provided here. The simplest solution is probably the one based on docker containers.

โš ๏ธ You must be careful using the OSRM demo server and read the about page of the service:

One request per second max. No scraping, no heavy usage.

Features

  • 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,
  • osrmNearest() uses the nearest service to query the nearest point on the street network,
  • osrmIsochrone() and osrmIsodistance() use multiple osrmTable() calls to create isochrones or isodistances polygons.

Demo

This is a short overview of the main features of osrm. The dataset used here is shipped with the package, it is 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.
library(osrm)
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright

## Routing: OSRM - http://project-osrm.org/
library(sf)
## Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE
pharmacy <- st_read(system.file("gpkg/apotheke.gpkg", package = "osrm"), 
                    quiet = TRUE)
travel_time <- osrmTable(loc = pharmacy)
travel_time$durations[1:5,1:5]
##      1    2    3    4    5
## 1  0.0 21.1 33.4 21.2 12.6
## 2 22.1  0.0 42.3 16.1 20.2
## 3 33.0 43.0  0.0 30.5 27.4
## 4 20.1 15.3 29.7  0.0 12.7
## 5 10.2 20.3 26.8 12.3  0.0
diag(travel_time$durations) <- NA
median(travel_time$durations, na.rm = TRUE)
## [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.
(route <- osrmRoute(src = pharmacy[1, ], dst = pharmacy[2, ]))
## Simple feature collection with 1 feature and 4 fields
## Geometry type: LINESTRING
## 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 through it.

plot(st_geometry(route))
plot(st_geometry(pharmacy[1:2,]), pch = 20, add = T, cex = 1.5)

  • 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.
(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...
## 
## [[1]]$summary
## [[1]]$summary$duration
## [1] 104.4667
## 
## [[1]]$summary$distance
## [1] 59.5755

The shortest trip between these pharmacies takes 104.5 minutes and is 59.6 kilometers long. The steps of the trip are described in the โ€œtripโ€ sf object (point 1 > point 2 > point 4 > point 3 > point 5 > point 1).

mytrip <- trips[[1]]$trip
# Display the trip
plot(st_geometry(mytrip), col = c("black", "grey"), lwd = 2)
plot(st_geometry(pharmacy[1:5, ]), cex = 1.5, pch = 21, add = TRUE)
text(st_coordinates(pharmacy[1:5,]), labels = row.names(pharmacy[1:5,]), 
     pos = 2)

  • osrmNearest() gives access to the nearest OSRM service. It returns the nearest point on the street network from any point. Here we will get the nearest point on the network from a couple of coordinates.
pt_not_on_street_network <- c(13.40, 52.47)
(pt_on_street_network <- osrmNearest(loc = pt_not_on_street_network))
## Simple feature collection with 1 feature and 2 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 13.39671 ymin: 52.46661 xmax: 13.39671 ymax: 52.46661
## Geodetic CRS:  WGS 84
##      id distance                  geometry
## loc loc      439 POINT (13.39671 52.46661)

The distance from the input point to the nearest point on the street network is of 439 meters

  • osrmIsochrone() computes areas that are reachable within a given time span from a point and returns the reachable regions as polygons. These areas of equal travel time are called isochrones. Here we compute the isochrones from a specific point defined by its longitude and latitude.
(iso <- osrmIsochrone(loc = c(13.43,52.47), breaks = seq(0,12,2)))
## Simple feature collection with 5 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 13.34397 ymin: 52.41642 xmax: 13.50187 ymax: 52.51548
## Geodetic CRS:  WGS 84
##   id isomin isomax                       geometry
## 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)

Installation

  • Development version on GitHub
remotes::install_github("riatelab/osrm")
  • Stable version on CRAN
install.packages("osrm")

Community Guidelines

One can contribute to the package through pull requests and report issues or ask questions here. See the CONTRIBUTING.md file for detailed instructions.

Acknowledgements

Many thanks to the editor (@elbeejay) and reviewers (@JosiahParry, @mikemahoney218 and @wcjochem) of the JOSS article.
This publication has led to a significant improvement in the code base and documentation of the package.

More Repositories

1

cartography

Thematic Cartography for R
R
398
star
2

bertin

A package to draw maps in javascript
JavaScript
304
star
3

mapsf

Thematic cartography with R
R
224
star
4

magrit

Thematic cartography entirely in the browser โ™ฅ
TypeScript
127
star
5

linemap

Create maps made of (ridge) lines
R
116
star
6

maptiles

Download, compose and display map tiles with R
R
97
star
7

tanaka

Tanaka Maps with R
R
78
star
8

geotoolbox

geojson toolbox
JavaScript
59
star
9

geoviz

d3-based javascript library for thematic maps
JavaScript
42
star
10

mapiso

Transformation of regularly spaced grids into contour polygons
R
34
star
11

dicopal.js

Discrete color palettes (hundreds of them!) for JavaScript
TypeScript
32
star
12

SpatialPosition

R package for computing spatial position models
R
31
star
13

potential

An R package to compute the potential model as defined by Stewart (1941).
R
25
star
14

geoverview

Quick geojson overview and summary
JavaScript
25
star
15

bertinR

A wrapper for bertin.js
R
23
star
16

statsbreaks

A javascript package to group the values of a statistical series into classes (discretization)
JavaScript
22
star
17

ReproducibleCartography

ICC 2017 Reproducible Cartography
HTML
18
star
18

mapinsetr

Create map inset
R
17
star
19

fisheye

Transform base maps using log-azimuthal projection
R
15
star
20

maplegend

Create legends for maps and other graphics.
R
12
star
21

flows

Flow selection and analysis
R
11
star
22

go-cart-wasm

Flow-based cartograms (Gastner-Seguy-More 2018 algorithm) for the Web
JavaScript
9
star
23

regioviz

Regioviz - Outil de visualisation et de comparaison rรฉgionale ๐Ÿ“Š ๐Ÿ“ˆ
JavaScript
8
star
24

intro_sf

Faire des cartes avec R - Introduction au package sf
8
star
25

MTA

Multiscalar Territorial Analysis
R
7
star
26

anfdataviz

HTML
6
star
27

gepaf

Encode and decode the Google Encoded Polyline Algorithm Format
R
6
star
28

cartostats

Reproducible Cartography
HTML
4
star
29

geocountries

A tool to get ISO codes and geometries from country names
JavaScript
3
star
30

basemaps

2
star
31

map-templates

R programmes to build map templates for Magrit (with boxes for overseas and outermost territories), with data.
R
2
star
32

MAGRIT-formations

Supports pour les formations en cartographie avec Magrit
HTML
2
star
33

rgvzall

Tool for regional comparison and data visualisation with data for France, Europe and Tunisia
JavaScript
2
star
34

world

country base map
1
star
35

MDM

non
HTML
1
star
36

cartographie-reproductible

Cartographie Reproductible
JavaScript
1
star
37

regiovizlab

Landing page for Regioviz (RIATE version)
HTML
1
star
38

anticartogram

HTML
1
star
39

linearcorrelation

Statistical operators for bi-variate analysis
JavaScript
1
star