Learn R Programming

osrm (version 3.1.1)

osrmTrip: Get the Travel Geometry Between Multiple Unordered Points

Description

Build and send an OSRM API query to get the shortest travel geometry between multiple points. This function interfaces the trip OSRM service.

Usage

osrmTrip(loc, overview = "simplified")

Arguments

loc

a SpatialPointsDataFrame of the waypoints, or a data.frame with points as rows and 3 columns: identifier, longitudes and latitudes (WGS84 decimal degrees).

overview

"full", "simplified". Add geometry either full (detailed) or simplified according to highest zoom level it could be display on.

Value

A list of connected components. Each component contains:

trip

A SpatialLinesDataFrame (loc's CRS if there is one, WGS84 else) containing a line for each step of the trip.

summary

A list with 2 components: duration (in minutes) and distance (in kilometers).

Details

As stated in the OSRM API, if input coordinates can not be joined by a single trip (e.g. the coordinates are on several disconnecte islands) multiple trips for each connected component are returned.

See Also

osrmRoute

Examples

Run this code
# NOT RUN {
# Load data
data("berlin")

# Get a trip with a id lat lon data.frame
trips <- osrmTrip(loc = apotheke.df)

# Display the trip
library(sp)
plot(trips[[1]]$trip, col = "black", lwd = 4)
plot(trips[[1]]$trip, col = c("red", "white"), lwd = 1, add=T)
points(apotheke.df[, 2:3], pch = 21, bg = "red", cex = 1)

# Map
if(require("cartography")){
  osm <- getTiles(x = trips[[1]]$trip, crop = TRUE,
                  type = "cartolight", zoom = 11)
  tilesLayer(x = osm)
  plot(trips[[1]]$trip, col = "black", lwd = 4, add=T)
  plot(trips[[1]]$trip, col = c("red", "white"), lwd = 1, add=T)
  points(apotheke.df[, 2:3], pch = 21, bg = "red", cex = 1)
}

# Get a trip with a SpatialPointsDataFrame
trips <- osrmTrip(loc = apotheke.sp[1:10,])

# Map
if(require("cartography")){
  osm <- getTiles(x = trips[[1]]$trip, crop = TRUE,
                  type = "cartolight", zoom = 11)
  tilesLayer(x = osm)
  plot(trips[[1]]$trip, col = "black", lwd = 4, add=T)
  plot(trips[[1]]$trip, col = c("red", "white"), lwd = 1, add=T)
  plot(apotheke.sp[1:10,], pch = 21, bg = "red", cex = 1, add=T)
}
# }

Run the code above in your browser using DataLab