Show HN:美国路网——一个用于快速进行美国本地路径规划的Python库
Show HN: US Routing – Python library for fast local routing in the US

原始链接: https://github.com/ivanbelenky/us-routing

US Routing是一个Python库,用于在美国境内进行快速、近似的局部路径规划,它利用北美道路数据集。它能够在城市、邮政编码和坐标之间规划路线,并提供最短距离或最快时间等选项。该库提供详细的路线信息,包括距离、持续时间和经过的州。 安装非常简单,可以使用pip或poetry。`get_route`函数计算路线,`download_north_american_roads`函数管理数据获取。该库处理混合类型的地理位置,并包含针对坐标临近性的错误处理。 对于自定义的路径规划方案,`BaseRouter`类允许用户从包含几何和属性数据的shapefile构建图。用户可以定义如何比较和设置边的属性。生成的路由器可以序列化以便以后使用。欢迎为项目贡献代码。

Hacker News 正在讨论一个名为“US Routing”的 Python 库,它可以快速进行美国本地路线规划。该库的优点在于其简洁性,相比 Graphhopper/OSRM 等工具更为简单易用。作者承认使用了 NetworkX 中的 Dijkstra 算法,虽然速度较慢,但实现起来更容易。一些用户建议可以通过切换到 Rustworkx、优化内存使用以及采用更高级的技术(如收缩层次结构)来提高速度。 有人对该库使用的 282MB SQLite 数据库提出了疑问,该数据库基于公共领域的 NTAD 数据集。讨论还涉及道路类型过滤、最短路径近似算法的说明以及 Python 版本要求(3.11),并提出了使用 `uv` 和 `pyenv` 等工具的解决方案。一位用户请求添加“原路返回”导航功能,另一些用户则询问该库是否支持城市内短距离的路线规划功能。作者欢迎贡献代码并期待未来的改进。

原文

US Routing Logo

US Routing is a Python library for fast local routing in the United States. It's useful when approximations are acceptable. It bootstraps from the North American Roads dataset.

You can install US Routing using pip:

or using poetry:

git clone https://github.com/ivanbelenky/us-routing.git
cd us-routing
poetry install

Here's a quick example of how to use US Routing:

from us_routing import get_route

# Route between two cities
r = get_route('New York', 'Los Angeles', edge_distance="DURATION")
print(r.total_distance, r.duration)
# Output in km of course:
# 4434.759999999997 1 day, 20:46:24.499959

# Print route steps
print(r)
# Output: [
#POINT (-73.99811899964011 40.7508730002449) -> POINT (-74.0013209995546 40.74648499998924) (0.5700000000000001 km) 9TH AV, SP_TH_MA, 72 km/h
#POINT (-74.0013209995546 40.74648499998924) -> POINT (-74.0054249996425 40.74097199980971) (0.6799999999999999 km) 9TH AV, SP_TH_MA, 72 km/h
#POINT (-74.0054249996425 40.74097199980971) -> POINT (-74.00819599956175 40.74211600011984) (0.27 km) W 14TH ST, SP_TH_MA, 72 km/h
#POINT (-74.00819599956175 40.74211600011984) -> POINT (-74.0090509998795 40.74090099973697) (0.16 km) 10TH AV, SP_TH_MA, 72 km/h
# ...]

# Route between zip codes
r = get_route('10001', '60007')
print(r.total_distance, r.duration)
# Output:
# 1315.910000000001 13:20:26.827392


# Route between coordinates, will raise ValueError if closest node in the graph is too far from the location
try:
    r = get_route((40.7128, -74.0060), (34.0522, -118.2437), d_threshold=0.00001)
    print(r.total_distance, r.duration)
except ValueError as e:
    print(e)
#  Node 2bd87209-d2fe-4f41-a89f-29104aeb5cf9 is too far from location point=<POINT (40.713 -74.006)> zip_code=None admin=None name=None

r = get_route((-74.0060, 40.7128), (-118.2437, 34.0522), d_threshold=10)

# and for sure you can mix stuff up
r = get_route((40.7128, -74.0060), "seattle", d_threshold=15)
  • Fast routing between US locations (cities, zip codes, or coordinates)
  • Multiple routing options by default (shortest distance, fastest time)
  • Detailed route information (distance, duration, states traversed)

The routing data is based on the North American Roads dataset. The library includes functionality to download and process this data:

from us_routing import download_north_american_roads

download_north_american_roads()

To set up the development environment:

  1. Clone the repository
  2. Install Poetry: pip install poetry
  3. Install dependencies: poetry install

This package provides a BaseRouter class that you can use to build your own custom routing graphs. It exposes a very simple API to create routers from shapefiles containing multiple geometries and optional attributes for those geometries. It looks something like this:

geometries: Sequence[LineString | MultiLineString]
geometries_data: Optional[Sequence[Dict[str, Any]]] = None # should be serializable

# Optional arguments to define how attributes are compared and set to the edges of the graph
edge_attr_equal: Optional[Callable[[Any, Any], bool]] = ...
edge_attr_setter: Optional[Callable[[Any, Any], dict[str, Any]]] = ...

router = BaseRouter.from_geometries(geometries, geometries_data, edge_attr_equal, edge_attr_setter)
router.serialize("path_to_save")

Contributions are welcome! Please feel free to submit a Pull Request.

联系我们 contact @ memedata.com