if (FALSE) {
library(sf)
apotheke.df <- read.csv(system.file("csv/apotheke.csv", package = "osrm"))
apotheke.sf <- st_read(system.file("gpkg/apotheke.gpkg", package = "osrm"),
quiet = TRUE)
# Travel path between points
route1 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ],
returnclass="sf")
# Display paths
plot(st_geometry(route1))
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!
}
Run the code above in your browser using DataLab