Shortest Paths and Travel Time from OpenStreetMap with R

The osrm package is an interface between R and the OSRM API. OSRM is a routing service based on OpenStreetMap data.
This package allows computing shortest paths and travel time matrices between points.

A brief overview of osrm package functions:

  • osrmTable Get travel time matrices between points.
  • osrmViaroute Get travel time and travel distance between two points.
  • osrmViarouteGeom Get the travel geometry between two points.

This package relies on the use of a running OSRM service (tested with version 4.9.0 of the OSRM API).
By default this service is the OSRM public API (API usage policy). It is possible to use a different server if you want to make intensive use of the API.

Here is a short example of osrm usage (download data here):

library(osrm) # to get paths between points
library(cartography) # to plot a base map
library(rgdal) # to import/export shapefile

# import a shapefile
letter <- readOGR(dsn="/home/tg/Bureau/", layer = "R")

# get OpenStreetMap tiles on the region
mytiles <- getTiles(spdf = letter, type = "osm")

# # export the plot
# # get size of the figure
# sizes <- getFigDim(spdf = letter, width = 474, mar = c(0,0,0,0))
# png("/home/tg/Bureau/R.png", width = sizes[1], height = sizes[2])
par(mar=c(0,0,0,0))

plot(letter, col = NA)
tilesLayer(mytiles, add=T)

# plot the points
plot(letter, add=T, pch = 20, col = 'blue', cex = 2)

# Create a SpaTialLinesDataFrame of all the roads
df <- data.frame(coordinates(letter))
names(df) <- c("X", "Y")
df$id <- 1:nrow(df)
for (i in 1:(nrow(df)-1)){
  road <- osrmViarouteGeom(src = df[i,3:1], 
                           dst = df[i+1,3:1], 
                           sp =TRUE)
  if (exists("myroads")){
    myroads <- rbind(myroads, road)
  }else{
    myroads <- road
  }
}

# and plot it
plot(myroads, col = "red", lwd = 2, add=TRUE)

mtext("Data: OpenStreetMap & contributors, ODBl; routes: OSRM", 
      side = 1, line =  -1, adj = 0, cex = 0.8, font = 2)
# dev.off()
# # export the road layer
# writeOGR(obj = myroads, dsn="/home/tg/Bureau",
#          layer = "myroads", driver = "ESRI Shapefile")

R

The next version of the package (now on GitHub) will allow SpatialPointsDataFrames as inputs of the functions.


OpenEdition vous propose de citer ce billet de la manière suivante :
Timothée Giraud (12 février 2016). Shortest Paths and Travel Time from OpenStreetMap with R. R Géomatique. Consulté le 9 décembre 2024 à l’adresse https://doi.org/10.58079/tp3q


2 réflexions sur « Shortest Paths and Travel Time from OpenStreetMap with R »

  1. Bonsoir , je travaille sur cette problématique mais je veux changer le pays , travailler sur la tunisie par exemple , si j”exécute en utilisant un autre shapefile il y aura des problémes !!
    qui pourra m’ aider ?

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.