Get the Shortest Path Between Two Points
Build and send an OSRM API query to get the travel geometry between two points. This function interfaces the route OSRM service.
osrmRoute( src, dst, loc, overview = "simplified", exclude = NULL, sp, returnclass, osrm.server = getOption("osrm.server"), osrm.profile = getOption("osrm.profile") )
src |
a vector of identifier, longitude and latitude (WGS84), a vector of longitude and latitude (WGS84), a SpatialPointsDataFrame, a SpatialPolygonsDataFrame or an sf object of the origine point. |
dst |
a vector of identifier, longitude and latitude (WGS84), a vector of longitude and latitude (WGS84), a SpatialPointsDataFrame, a SpatialPolygonsDataFrame or an sf object of the destination point. |
loc |
a data.frame of identifier, longitude and latitude (WGS84), a SpatialPointsDataFrame, a SpatialPolygonsDataFrame or an sf object of via points. The first row is the origine, the last row is the destination. |
overview |
"full", "simplified" or FALSE. Use "full" to return the detailed geometry, use "simplified" to return a simplified geometry, use FALSE to return only time and distance. |
exclude |
pass an optional "exclude" request option to the OSRM API. |
sp |
deprecated, if sp==TRUE the function returns a SpatialLinesDataFrame. |
returnclass |
if returnclass="sf" an sf LINESTRING is returned. If returnclass="sp" a SpatialLineDataFrame is returned. If returnclass is not set a data.frame of coordinates is returned. |
osrm.server |
the base URL of the routing server. getOption("osrm.server") by default. |
osrm.profile |
the routing profile to use, e.g. "car", "bike" or "foot" (when using the routing.openstreetmap.de test server). getOption("osrm.profile") by default. |
If returnclass is not set, a data frame is returned. It contains the
longitudes and latitudes of the travel path between the two points.
If returnclass is set to "sp", a SpatialLinesDataFrame is returned.
If returnclass is set to "sf", an sf LINESTRING is returned.
SpatialLinesDataFrame and sf LINESTRING contain 4 fields: identifiers of
origine and destination, travel time in minutes and travel distance in
kilometers.
If overview is FALSE, a named numeric vector is returned. It contains travel
time (in minutes) and travel distance (in kilometers).
## Not run: # Load data data("berlin") library(sf) # Travel path between points route1 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ], returnclass="sf") # Travel path between points excluding motorways route2 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ], returnclass="sf", exclude = "motorway") # Display paths plot(st_geometry(route1)) plot(st_geometry(route2), col = "red", add = TRUE) plot(st_geometry(apotheke.sf[c(1,16),]), col = "red", pch = 20, add = TRUE) # Return only duration and distance route3 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ], overview = FALSE) route3 # Using only coordinates route4 <- osrmRoute(src = c(13.412, 52.502), dst = c(13.454, 52.592), returnclass = "sf") plot(st_geometry(route4)) # Using via points pts <- structure( list(x = c(13.32500, 13.30688, 13.30519, 13.31025, 13.4721, 13.56651, 13.55303, 13.37263, 13.50919, 13.5682), y = c(52.40566, 52.44491, 52.52084, 52.59318, 52.61063, 52.55317, 52.50186, 52.49468, 52.46441, 52.39669)), class = "data.frame", row.names = c(NA, -10L)) route5 <- osrmRoute(loc = pts, returnclass = "sf") plot(st_geometry(route5), col = "red", lwd = 2) points(pts, pch = 20, cex = 2) # Using a different routing server u <- "https://routing.openstreetmap.de/routed-foot/" route5 <- osrmRoute(apotheke.sf[1, ], apotheke.df[16, ], returnclass="sf", osrm.server = u) # Using an open routing service with support for multiple modes # see https://github.com/riatelab/osrm/issues/67 u <- "https://routing.openstreetmap.de/" options(osrm.server = u) route6 <- osrmRoute(apotheke.sf[1, ], apotheke.df[16, ], returnclass="sf", osrm.profile = "bike") route7 <- osrmRoute(apotheke.sf[1, ], apotheke.df[16, ], returnclass="sf", osrm.profile = "car") plot(st_geometry(route5), col = "green") plot(st_geometry(route6), add = TRUE) # note the cycle route has fewer turns plot(st_geometry(route7), col = "red", add = TRUE) # car route, indirect = good! ## End(Not run)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.