1- import json
2- from typing import Dict , List , Any
1+ from typing import Any
2+
33from nextmv import Asset
44
5+
56def generate_color (index : int , total : int ) -> str :
67 """
78 Generates a color from a continuous color spectrum based on index and total count.
89 Uses HSL color space to ensure good color separation.
9-
10+
1011 Args:
1112 index: The index of the vehicle (0-based)
1213 total: Total number of vehicles
13-
14+
1415 Returns:
1516 str: Hex color code
1617 """
1718 # Use golden ratio to get well-distributed hues
1819 golden_ratio = 0.618033988749895
1920 hue = (index * golden_ratio ) % 1.0
20-
21+
2122 # Convert HSL to RGB
2223 def hsl_to_rgb (h : float , s : float = 0.7 , l : float = 0.5 ) -> str :
2324 def hue_to_rgb (p : float , q : float , t : float ) -> float :
@@ -35,44 +36,44 @@ def hue_to_rgb(p: float, q: float, t: float) -> float:
3536
3637 q = l * (1 + s ) if l < 0.5 else l + s - l * s
3738 p = 2 * l - q
38-
39+
3940 r = hue_to_rgb (p , q , h + 1 / 3 )
4041 g = hue_to_rgb (p , q , h )
4142 b = hue_to_rgb (p , q , h - 1 / 3 )
42-
43+
4344 return f"#{ int (r * 255 ):02x} { int (g * 255 ):02x} { int (b * 255 ):02x} "
44-
45+
4546 return hsl_to_rgb (hue )
4647
47- def create_visuals (solution : Dict [str , Any ]) -> Asset :
48+ def create_visuals (solution : dict [str , Any ]) -> Asset :
4849 """
4950 Creates a GeoJSON visualization from the routes in the solution.
5051 Includes both route lines and step points.
51-
52+
5253 Args:
5354 solution: The solution dictionary containing route information
54-
55+
5556 Returns:
5657 Asset: A Nextmv Asset containing the GeoJSON visualization
5758 """
5859 features = []
59-
60+
6061 # Extract routes directly from solution
6162 routes = solution .get ("routes" , [])
6263 total_routes = len (routes )
63-
64+
6465 for i , route in enumerate (routes ):
6566 vehicle_id = route .get ("vehicle" )
6667 route_color = generate_color (i , total_routes )
67-
68+
6869 # Get coordinates from steps
6970 coordinates = []
7071 for step in route .get ("steps" , []):
7172 if "location" in step :
7273 # Convert [lng, lat] to [lat, lng] for Leaflet
7374 lng , lat = step ["location" ]
7475 coordinates .append ([lng , lat ])
75-
76+
7677 # Create point feature for each step
7778 point_feature = {
7879 "type" : "Feature" ,
@@ -98,7 +99,7 @@ def create_visuals(solution: Dict[str, Any]) -> Asset:
9899 }
99100 }
100101 features .append (point_feature )
101-
102+
102103 # Create line feature for the route
103104 line_feature = {
104105 "type" : "Feature" ,
@@ -121,13 +122,13 @@ def create_visuals(solution: Dict[str, Any]) -> Asset:
121122 }
122123 }
123124 features .append (line_feature )
124-
125+
125126 # Create GeoJSON FeatureCollection
126127 geojson = {
127128 "type" : "FeatureCollection" ,
128129 "features" : features
129130 }
130-
131+
131132 # Create and return Nextmv Asset
132133 return Asset (
133134 name = "Route Visualization" ,
@@ -138,4 +139,4 @@ def create_visuals(solution: Dict[str, Any]) -> Asset:
138139 "label" : "Route Visualization" ,
139140 "type" : "custom-tab"
140141 }
141- )
142+ )
0 commit comments