IBM Cloud Docs
寻径函数

寻径函数

时空库提供有寻径函数,用于列出产生节点间路径的边缘。

为了说明如何使用时空库中的路由功能,本主题中的代码样本使用从 OpenStreetMap 下载的亚特兰大城市的 OSM 地图。

要计算路径,请完成下列步骤:

  1. 创建寻径程序,并从 OSM 地图读取:
    >>> router = stc.router()
    >>> router.read_map('atlanta.osm')
    Creating Road Network...
    Road Network Created...
    Creating Network Searcher...
    Network Searcher Created...
    
  2. 设置开始点和结束点:
    >>> start = stc.point(33.763261, -84.394897)
    >>> end = stc.point(33.753900, -84.385121)
    
  3. 寻找时间成本最低的最佳路径(在时间方面最快的路径):
    >>> 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)]
    
  4. 寻找距离成本最低的最佳路径(在距离方面最快的路径):
    >>> 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)]