‘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.
"great-britain.osrm", …) and other data and scripts.
error_codeof the starting-attempt (
0means the start was successful)
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.
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
viaroute(). 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
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