Code Monkey home page Code Monkey logo

osrm's Introduction

osrm R package

Version R build status codecov

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 allows to compute 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.26.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.

To consider when using OSRM:
> “Most of the previously active core devs have either moved on to new roles, or are simply busy on different projects (…)”

Features

  • osrmTable Get travel time matrices between points.

  • osrmRoute Get the shortest path between two points.

  • osrmTrip Get the travel geometry between multiple unordered points.

  • osrmIsochrone Get polygons of isochrones.

Demo

osrmTable()

library(osrm)
library(sf)
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "osrm"), 
                       quiet = TRUE)
# Travel time matrix
distA <- osrmTable(loc = apotheke.sf[1:5,])
distA$durations
1 2 3 4 5
1 0.0 21.4 33.6 20.6 12.0
2 22.8 0.0 41.8 16.1 20.2
3 33.2 42.4 0.0 30.2 27.4
4 19.4 15.3 29.4 0.0 12.9
5 9.5 20.2 26.8 12.3 0.0

osrmRoute()

library(maptiles)
library(mapsf)
# Route
route <- osrmRoute(src = apotheke.sf[74,], dst = apotheke.sf[55,],
                   overview = "full", returnclass = "sf")
# Get map tiles
osm <- get_tiles(x = route, crop = TRUE, zoom = 13)
# temporary fix for terra <= 1.3-22, replace NA values by 255
osm[is.na(osm)] <- 255
# Map
theme <- mf_theme(mar = c(0,0,1.2,0), inner = FALSE, line = 1.2, cex = .9, 
                  pos = "center", tab = FALSE)
mf_export(osm,filename = "img/route.png", width = ncol(osm), theme = theme)
mf_raster(osm, add = TRUE)
mf_map(route, lwd = 4, add = TRUE, col = "blue")
mf_map(route, lwd = 1, col = "white", add = TRUE)
mf_map(apotheke.sf[c(74,55),], pch = 20, col = "red", add = TRUE)
mf_title("osrmRoute()")
mf_credits(get_credit("OpenStreetMap"), pos = "bottomright", cex = .8, 
           bg = "#ffffff80")
dev.off()

osrmTrip()

# Trip 
trips <- osrmTrip(loc = apotheke.sf[10:20,], returnclass="sf")
trip <- trips[[1]]$trip
# Get map tiles
osm2 <- get_tiles(x = trip, crop = TRUE, zoom = 11)
# temporary fix for terra <= 1.3-22, replace NA values by 255
osm2[is.na(osm2)] <- 255
# Map
mf_export(osm2,filename = "img/trip.png", width = ncol(osm), theme = theme)
mf_raster(osm2, add = TRUE)
mf_map(trip, col = "black", lwd = 4, add = TRUE )
mf_map(trip, col = c("red", "white"), lwd = 1, add = TRUE)
mf_map(apotheke.sf[10:20,], pch = 21, col = "red", cex = 1.5, add = TRUE)
mf_title("osrmTrip()")
mf_credits(get_credit("OpenStreetMap"), pos = "bottomright", cex = .8, 
           bg = "#ffffff80")
dev.off()

osrmIsochrone()

bks <- seq(from = 0, to = 14, by = 2)
iso <- osrmIsochrone(loc = apotheke.sf[87,], returnclass="sf",
                     breaks = bks, res = 70)
# Get map tiles
osm3 <- get_tiles(x = iso, crop = TRUE, zoom = 11)
# temporary fix for terra <= 1.3-22, replace NA values by 255
osm3[is.na(osm3)] <- 255
# Map
cols <- hcl.colors(n = 7, palette = "Emrld", alpha = 0.75, rev = F)
mf_export(osm3,filename = "img/iso.png", width = ncol(osm), theme = theme)
mf_raster(osm3, add = TRUE)
mf_map(x = iso, var = "center", type = "choro", 
       breaks = bks, border = NA, pal = cols,
       leg_pos = "topleft", leg_frame = T,
       leg_title = "Isochrones\n(min)",
       leg_title_cex = 1, leg_val_cex = .8,
       add = TRUE)
mf_map(apotheke.sf[87,], pch = 21, col = "red", 
       cex = 1.5, add=TRUE)
mf_title("osrmIsochrone()")
mf_credits(get_credit("OpenStreetMap"), pos = "bottomright", cex = .8, 
           bg = "#ffffff80")
dev.off()

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.

osrm's People

Contributors

rcarto avatar pasipasi123 avatar mbask avatar robinlovelace avatar rcura avatar metanoid avatar kirel avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.