路由函数
时序库包含路由函数,这些函数会列出从一个节点到另一个节点路径所经过的边。
为了向您展示如何使用时空库中的路由功能,本主题中的代码示例使用了从 下载的亚特兰大市 https://www.openstreetmap.org OSM 地图。
要计算路线:
创建一个路由器并从 OSM 地图中读取数据:
>>> router = stc.router() >>> router.read_map('atlanta.osm') Creating Road Network... Road Network Created... Creating Network Searcher... Network Searcher Created...设置起始点和终点:
>>> start = stc.point(33.763261, -84.394897) >>> end = stc.point(33.753900, -84.385121)找出时间成本最低(即耗时最短)的最佳路线:
>>> best_time_route = router.compute_route(start, end, method='time') # Check time cost, in the unit of hours >>> best_time_route.cost 0,044188548756240675 # Check route path (only showing the first three points), which is a list of points in 3-tuple (osm_point_id, lat, lon) >>> best_time_route.path[:3] [(2036943312, 33.7631862, -84.3939405), (3523447568, 33.7632666, -84.3939315), (2036943524, 33.7633273, -84.3939155)]找出距离成本最小的最佳路径(即从距离角度来看最快的路径):
>>> best_distance_route = router.compute_route(start, end, method='distance') # Check distance cost, in the unit of meters >>> best_distance_route.cost 2042,4082601271236 # Check route path (only showing the first three points), which is a list of points in 3-tuple (osm_point_id, lat, lon) >>> best_distance_route.path[:3] [(2036943312, 33.7631862, -84.3939405), (3523447568, 33.7632666, -84.3939315), (2036943524, 33.7633273, -84.3939155)]