
Show HN: US Routing – Python library for fast local routing in the US by ivanbelenky
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:
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
# …]
#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=
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)
” dir=”auto”>
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.679999999999
8 Comments
dmitrygr
Routing library, having nothing to do with Google or Google maps.
simonw
From poking around in the source code I found this 282M SQLite database:
I can't figure out how to read it though. I get this error:
As far as I can tell VSRS is a proprietary Esri thing.
jdelman
It's kinda nice to see a non-AI project on here.
svcphr
Nice. Very light-weight compared to proper local routers like Graphhopper, OSRM, etc., which can be overkill for simple tasks. Although the 'routing' here is nx.shortest_path, which is just Dijkstra, so pretty slow compared to other easy to implement routing algorithms (even just bi-directional Dijkstra or A*… although contraction hierarchies would be huge gain here since edge weights are fixed). Also not sure why readme describes it as an approximation? Dijkstra is guaranteed to return lowest cost path. Maybe approximation because assuming free-flow, or if the NAR dataset is incomplete?
CamperBob2
Edit: thanks very much for the suggestions, especially adding the Python version to the uv command line. I totally missed that, and that totally fixed it. Apologies for the OT tech support derailment.
————–
Question for those familiar with uv. US Routing apparently requires a very specific Python version (3.11 and nothing else), but my system has Python 3.10.9 installed at the moment and I'd rather not upgrade the global version just now. My understanding from reading a lot of uv evangelism on HN and elsewhere is that uv fixes this type of dilemma. But, having just tried to use it to install this package, it's just giving me the same old Python version errors:
Am I misunderstanding the whole uv thing, or just doing something wrong? Or is us-routing somehow incompatible with it?
protocolture
Came here to complain about US Telcos being willing to do anything other than enabling dynamic routing.
Glad to see this is for roads.
VladVladikoff
Does it work for shorter distances? within a city from one business to another address?
culopatin
I’d love to see if I could assist in adding road type filters such as avoid multi lane highways for example