4
4
from dataclasses import dataclass
5
5
from enum import auto
6
6
from enum import Enum
7
+ from typing import Any
7
8
8
9
from tqdm import tqdm
9
10
@@ -29,13 +30,46 @@ class CityRoadType(Enum):
29
30
ACCESS_ROAD = auto ()
30
31
31
32
33
+ class RoadPrecisionLevel :
34
+ """City road precision level."""
35
+ def __init__ (self , name : str , priority : int ):
36
+ self .name = name
37
+ self .priority = priority
38
+
39
+ def __str__ (self ) -> str :
40
+ return self .name
41
+
42
+ def __eq__ (self , other : Any ) -> bool :
43
+ if isinstance (other , RoadPrecisionLevel ):
44
+ return self .priority == other .priority
45
+ return False
46
+
47
+ def __lt__ (self , other : 'RoadPrecisionLevel' ) -> bool :
48
+ if isinstance (other , RoadPrecisionLevel ):
49
+ return self .priority < other .priority
50
+ return NotImplemented
51
+
52
+
53
+ class CityRoadPrecision (Enum ):
54
+ """Enum defining different road precision levels."""
55
+ VERY_HIGH = RoadPrecisionLevel (name = "Very-High" , priority = 3 )
56
+ HIGH = RoadPrecisionLevel (name = "High" , priority = 2 )
57
+ MEDIUM = RoadPrecisionLevel (name = "Medium" , priority = 1 )
58
+ LOW = RoadPrecisionLevel (name = "Low" , priority = 0 )
59
+
60
+ @classmethod
61
+ def from_string (cls , precision_name : str ) -> 'CityRoadPrecision' :
62
+ """Convert a string representation to a CityRoadPrecision enum value."""
63
+ for precision in cls :
64
+ if precision .value .name .lower () == precision_name .lower ():
65
+ return precision
66
+ raise ValueError (f"Invalid precision level: { precision_name } " )
67
+
68
+ @property
69
+ def priority (self ) -> int :
70
+ """Get the priority value for the precision level."""
71
+ return self .value .priority
32
72
33
- ROAD_PRECISION_LEVEL = {
34
- "Low" :0 ,
35
- "Medium" :1 ,
36
- "High" :2 ,
37
- "Very High" : 3
38
- }
39
73
40
74
@dataclass (frozen = True )
41
75
class RoadTypeData :
@@ -57,16 +91,19 @@ class RoadTypeData:
57
91
key = lambda road_type : ROAD_TYPE_DATA [road_type ].priority )
58
92
59
93
60
- def get_city_roads_with_priority_better_than (x : int ) -> list [CityRoadType ]:
94
+ def get_city_roads_with_priority_better_than (precision : CityRoadPrecision ) -> list [CityRoadType ]:
61
95
"""Returns a list of CityRoadType with a priority better than the given x."""
62
96
# Filter ROAD_TYPE_DATA to get only those with priority less than x
63
- return [road_type for road_type , data in ROAD_TYPE_DATA .items () if data .priority <= x ]
64
-
97
+ return [
98
+ road_type
99
+ for road_type , data in ROAD_TYPE_DATA .items ()
100
+ if data .priority <= precision .priority
101
+ ]
65
102
66
103
@profile
67
104
def prepare_download_city_roads (query : OverpassQuery ,
68
105
bounds : GpxBounds ,
69
- road_precision : str ) -> list [CityRoadType ]:
106
+ road_precision : CityRoadPrecision ) -> list [CityRoadType ]:
70
107
"""Download roads map from OpenStreetMap.
71
108
72
109
Args:
@@ -79,10 +116,9 @@ def prepare_download_city_roads(query: OverpassQuery,
79
116
"""
80
117
cache_pkl = ROADS_CACHE .get_path (bounds )
81
118
82
- assert road_precision in ROAD_PRECISION_LEVEL . keys ( )
119
+ logger . debug ( f"Road precision: { road_precision . name } " )
83
120
84
- logger .debug (f"Road precision : { road_precision } " )
85
- roads_to_plot : list [CityRoadType ] = get_city_roads_with_priority_better_than (ROAD_PRECISION_LEVEL [road_precision ])
121
+ roads_to_plot : list [CityRoadType ] = get_city_roads_with_priority_better_than (road_precision )
86
122
87
123
if os .path .isfile (cache_pkl ):
88
124
cityroads_cache : dict [CityRoadType , list [ListLonLat ]] = read_pickle (file_path = cache_pkl )
@@ -112,9 +148,9 @@ def prepare_download_city_roads(query: OverpassQuery,
112
148
def process_city_roads (query : OverpassQuery ,
113
149
bounds : GpxBounds ,
114
150
city_roads_downloaded : list [CityRoadType ],
115
- road_precision : str ) -> dict [CityRoadType ,list [ListLonLat ]]:
151
+ road_precision : CityRoadPrecision ) -> dict [CityRoadType ,list [ListLonLat ]]:
116
152
"""Query the overpass API to get the roads of a city."""
117
- roads_to_plot : list [CityRoadType ] = get_city_roads_with_priority_better_than (ROAD_PRECISION_LEVEL [ road_precision ] )
153
+ roads_to_plot : list [CityRoadType ] = get_city_roads_with_priority_better_than (road_precision )
118
154
119
155
if len (city_roads_downloaded ) > 0 :
120
156
# We need to process some downloaded road types.
0 commit comments