knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(message = FALSE) # knitr::opts_chunk$set(results = 'hide')
'osrmr' is a little R package designed to easily access and use the OSRM (open source routing machine - http://project-osrm.org/) directly from R.
Many problems with geocoding tasks can be solved in R. Yet, there is no direct routing engine implemented. For such tasks, OSRM is a widely used tool to access geospacial data and solve routing problems. The 'osrmr' package gives the possibility to access a basic functionality of OSRM.
You can access the OSRM-routing-engine with the onlinehost of OSRM (API 5) or by running a localhost of OSRM (API 4 or 5) on your device. As far as I know, there is no simple way to access the onlinehost of the OSRM API 4.
In order to use the localhost you need a local build of the OSRM-routing-engine (https://github.com/Project-OSRM/osrm-backend/wiki/Building-OSRM). Keep the local build in
a directory of your choice. This directory should contain a map of the area you want to
work with (i.e.
...) and other data and scripts.
error_codeof the starting-attempt (
0means the start was successful)
library(osrmr) # Set the path of your localhost as environment variable Sys.setenv("OSRM_PATH"="C:/OSRM_API5") # start localhost of OSRM run_server("switzerland-latest.osrm") quit_server() # quit the running server
With the 'osrmr' package you can use the following basic functionalities of OSRM:
The coordinate-standard for all functionalities of 'osrmr' is WGS84.
Keep in mind that different API's and localhost/onlinehost may return different results. There are pro's and con's for using one specific configuration. When choosing your configuration keep the following points in mind:
For given coordinates, you can calculate the nearest position which can be accessed by car by
using the function
nearest(). You can specify the coordinates, the API version and whether
to use the localhost or not.
library(osrmr) nearest(lat = 47, lng = 8, api_version = 5, localhost = FALSE)
run_server("switzerland-latest.osrm") nearest(lat = 47, lng = 8, api_version = 5, localhost = TRUE) quit_server() # lat lng # 1 47.00008 8.003016
Results may differ depending on the api version and the build of the localhost (or onlinehost).
For a given start- and end-destination, you can access route informations using
OSRM chooses the nearest point which can be accessed by car for the start- and end-destination.
You can choose whether to return only the traveltime (in seconds, as numeric) or
more details of the route (as list), by setting the parameter
viaroute(lat1 = 47.1, lng1 = 8.1, lat2 = 46.9, lng2 = 8.3, instructions = FALSE, api_version = 5, localhost = FALSE) viaroute(lat1 = 47.1, lng1 = 8.1, lat2 = 46.9, lng2 = 8.3, instructions = TRUE, api_version = 5, localhost = FALSE)
decode_geom() to decode polylines. Polylines can be generated with OSRM (see example
below). Depending on the OSRM API version, the precision for [lat, lng] encoding is different.
To decode a polyline the right way you need to know the precision of the polyline. The parameter
decode_geom() accepts the values 5 (
1e-5) or 6 (
1e-6) to decode a polyline.
polyline_5 <- rjson::fromJSON(file = "http://router.project-osrm.org/route/v1/driving/8.1,47.1;8.3,46.9?steps=false&geometries=polyline")$routes[]$geometry polyline_6 <- rjson::fromJSON(file = "http://router.project-osrm.org/route/v1/driving/8.1,47.1;8.3,46.9?steps=false&geometries=polyline6")$routes[]$geometry decoded_5 <- decode_geom(polyline_5, precision = 5) decoded_6 <- decode_geom(polyline_6, precision = 6) options(digits = 10) decoded_5[1:5,] decoded_6[1:5,] nrow(decoded_5) nrow(decoded_6)
The differences of precisions 5 and 6 are smaller than a tolerance of
assertthat::assert_that(all.equal(decoded_5, decoded_6, tolerance = 1e-6))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.