R语言osrm包,行程路线规划API
出行规划
# Sat Dec 12 11:46:07 2020 -
# 字符编码:UTF-8
# R 版本:R x64 4.0.3 for window 10
# cgh163email@163.com
# 个人笔记不负责任
# —— 拎了个梨🍐
.rs.restartR()
require(osrm)
require(sf)
require(mapview)
# osrm,最短行程路线
rm(list=ls());gc()
# 从OpenStreetMap到OSRM API的最短路径和旅行时间
# osrmTable得到点之间的行程时间矩阵。
# osrmRoute得到两点之间的最短路径。
# osrmTrip得到多个无序点之间的行程几何。
# osrmIsochrone得到等时线的多边形。
# Sat Dec 12 11:49:32 2020 ----osrmTable--------------------------
data("berlin")
# Inputs are data frames
# Travel time matrix
distA <- osrmTable(loc = apotheke.df[1:50, c("id","lon","lat")])
# First 5 rows and columns
distA$durations[1:5,1:5]
plot(distA$sources,type='l')
# Travel time matrix with different sets of origins and destinations
distA2 <- osrmTable(src = apotheke.df[1:10,c("id","lon","lat")],
dst = apotheke.df[11:20,c("id","lon","lat")])
# First 5 rows and columns
distA2$durations[1:5,1:5]
# Inputs are sf points
distA3 <- osrmTable(loc = apotheke.sf[1:10,])
# First 5 rows and columns
distA3$durations[1:5,1:5]
# Travel time matrix with different sets of origins and destinations
distA4 <- osrmTable(src = apotheke.sf[1:10,], dst = apotheke.sf[11:20,])
# First 5 rows and columns
distA4$durations[1:5,1:5]
# Sat Dec 12 13:42:21 2020 ----# osrmRoute得到两点之间的最短路径--------------
rm(list=ls());gc()
data("berlin")
library(sf)
require(mapview)
# 点间行程路径
route1 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ],
returnclass="sf")
# 不包括高速公路的点间行驶路径
route2 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ],
returnclass="sf", exclude = "motorway")
# 预览:
plot(st_geometry(route1),main='行程规划')
plot(st_geometry(route2), col = "blue", add = TRUE,main='不包括高速公路(蓝色)')
plot(st_geometry(apotheke.sf[c(1,16),]), col = "red", pch = 20, add = TRUE)
mapview(route1, color = "magenta")+route2
# Sat Dec 12 14:12:36 2020 --
# 仅返回持续时间和距离
route3 <- osrmRoute(src = apotheke.sf[1, ], dst = apotheke.df[16, ],
overview = FALSE)
route3
# Sat Dec 12 16:14:20 2020 ---仅使用坐标---------------------------
route4 <- osrmRoute(src = c(113.324553,23.106414),
dst = c(113.42471,23.086062),
returnclass = "sf")
mapview(st_geometry(route4))
# Sat Dec 12 16:14:10 2020 --end
# Sat Dec 12 16:38:24 2020 ---使用过孔点---------------------------
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")
mapview(route5)
plot(st_geometry(route5), col = "blue", lwd = 2)
points(pts, pch = 20, cex = 2)
dev.copy(png,'多个途径点的路线规划.jpg');dev.off()
# Sat Dec 12 16:44:40 2020 --end
# 使用其他路由服务器
u <- "https://routing.openstreetmap.de/routed-foot/"
route6 <- osrmRoute(apotheke.sf[1, ], apotheke.df[16, ], returnclass="sf",
osrm.server = u)
mapview(route6)
# 使用支持多种模式的开放式路由服务请参阅
# see https://github.com/rCarto/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!
dev.copy(png,'不同的地图商对路线的规划.jpg');dev.off()
mapview(route5,color='green')+mapview(route6,color='#fecc11')+route7
# Sat Dec 12 17:04:40 2020 --end