|
4 | 4 | import plotly.graph_objects as go |
5 | 5 | from folium import plugins |
6 | 6 |
|
7 | | -from . import common, routingkit, types |
| 7 | +from . import common, osrm, routingkit, types |
8 | 8 |
|
9 | 9 | # ==================== This file contains route plotting code (mode: 'route') |
10 | 10 |
|
@@ -175,6 +175,13 @@ def arguments(parser): |
175 | 175 | action="store_true", |
176 | 176 | help="indicates whether to add start and end markers", |
177 | 177 | ) |
| 178 | + parser.add_argument( |
| 179 | + "--osrm_host", |
| 180 | + type=str, |
| 181 | + nargs="?", |
| 182 | + default=None, |
| 183 | + help="host and port of the OSRM server (e.g. 'http://localhost:5000')", |
| 184 | + ) |
178 | 185 | parser.add_argument( |
179 | 186 | "--rk_bin", |
180 | 187 | type=str, |
@@ -334,7 +341,6 @@ def create_map( |
334 | 341 | route_animation_color: str, |
335 | 342 | start_end_markers: bool, |
336 | 343 | custom_map_tile: list[str], |
337 | | - rk_distance: bool, |
338 | 344 | ) -> folium.Map: |
339 | 345 | """ |
340 | 346 | Plots the given routes on a folium map. |
@@ -369,8 +375,6 @@ def create_map( |
369 | 375 | omit_end, |
370 | 376 | route_direction, |
371 | 377 | route_animation_color, |
372 | | - 1.0 / 1000.0 if rk_distance else 1.0 / 1000.0, |
373 | | - "km" if rk_distance else "s", |
374 | 378 | ) |
375 | 379 |
|
376 | 380 | # Plot points |
@@ -514,6 +518,7 @@ def plot( |
514 | 518 | weight_points: float, |
515 | 519 | no_points: bool, |
516 | 520 | start_end_markers: bool, |
| 521 | + osrm_host: str, |
517 | 522 | rk_osm: str, |
518 | 523 | rk_bin: str, |
519 | 524 | rk_profile: routingkit.RoutingKitProfile, |
@@ -600,8 +605,10 @@ def plot( |
600 | 605 | route.points[i].distance = length |
601 | 606 | route.length = length |
602 | 607 |
|
603 | | - # Determine route shapes (if routingkit is available) |
604 | | - if rk_osm: |
| 608 | + # Determine route shapes (if osrm or routingkit are available) |
| 609 | + if osrm_host: |
| 610 | + osrm.query_routes(osrm_host, routes) |
| 611 | + elif rk_osm: |
605 | 612 | routingkit.query_routes(rk_bin, rk_osm, routes, rk_profile, rk_distance) |
606 | 613 |
|
607 | 614 | # Dump some stats |
@@ -670,7 +677,6 @@ def plot( |
670 | 677 | route_animation_color, |
671 | 678 | start_end_markers, |
672 | 679 | custom_map_tile, |
673 | | - rk_distance, |
674 | 680 | ) |
675 | 681 |
|
676 | 682 | # Save map |
@@ -737,15 +743,15 @@ def plot_map_route( |
737 | 743 | omit_end: bool, |
738 | 744 | direction: types.RouteDirectionIndicator = types.RouteDirectionIndicator.none, |
739 | 745 | animation_bg_color: str = "FFFFFF", |
740 | | - rk_factor: float = None, |
741 | | - rk_unit: str = None, |
742 | 746 | ): |
743 | 747 | """ |
744 | 748 | Plots a route on the given map. |
745 | 749 | """ |
746 | 750 | rk_text = "" |
747 | | - if route.legs is not None: |
748 | | - rk_text = f"Route cost (routingkit): {sum(route.leg_costs) * rk_factor:.2f} {rk_unit}</br>" |
| 751 | + if route.leg_distances is not None: |
| 752 | + rk_text += f"Route cost (rk/osrm): {sum(route.leg_distances):.2f} km</br>" |
| 753 | + if route.leg_durations is not None: |
| 754 | + rk_text += f"Route duration (rk/osrm): {sum(route.leg_durations):.2f} s</br>" |
749 | 755 | popup_text = folium.Html( |
750 | 756 | "<p>" |
751 | 757 | + f"Route: {route_idx+1} / {route_count}</br>" |
@@ -838,13 +844,23 @@ def statistics( |
838 | 844 | types.Stat("nunassigned", "Unassigned stops", sum([len(g) for g in unassigned])), |
839 | 845 | ] |
840 | 846 |
|
841 | | - if all((r.legs is not None) for r in routes): |
842 | | - costs = [sum(r.leg_costs) for r in routes] |
| 847 | + if all((r.leg_distances is not None) for r in routes): |
| 848 | + costs = [sum(r.leg_distances) for r in routes] |
| 849 | + stats.extend( |
| 850 | + [ |
| 851 | + types.Stat("distances_max", "RK/OSRM distances (max)", max(costs)), |
| 852 | + types.Stat("distances_min", "RK/OSRM distances (min)", min(costs)), |
| 853 | + types.Stat("distances_avg", "RK/OSRM distances (avg)", sum(costs) / float(len(routes))), |
| 854 | + ] |
| 855 | + ) |
| 856 | + |
| 857 | + if all((r.leg_durations is not None) for r in routes): |
| 858 | + durations = [sum(r.leg_durations) for r in routes] |
843 | 859 | stats.extend( |
844 | 860 | [ |
845 | | - types.Stat("costs_max", "RK costs (max)", max(costs)), |
846 | | - types.Stat("costs_min", "RK costs (min)", min(costs)), |
847 | | - types.Stat("costs_avg", "RK costs (avg)", sum(costs) / float(len(routes))), |
| 861 | + types.Stat("durations_max", "RK/OSRM durations (max)", max(durations)), |
| 862 | + types.Stat("durations_min", "RK/OSRM durations (min)", min(durations)), |
| 863 | + types.Stat("durations_avg", "RK/OSRM durations (avg)", sum(durations) / float(len(routes))), |
848 | 864 | ] |
849 | 865 | ) |
850 | 866 |
|
|
0 commit comments