sibi-dst 0.3.40__py3-none-any.whl → 0.3.43__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. sibi_dst/df_helper/__init__.py +2 -0
  2. sibi_dst/df_helper/_artifact_updater_multi_wrapper.py +262 -0
  3. sibi_dst/df_helper/_df_helper.py +5 -2
  4. sibi_dst/df_helper/_parquet_artifact.py +8 -2
  5. sibi_dst/df_helper/_parquet_reader.py +5 -1
  6. sibi_dst/df_helper/backends/sqlalchemy/_db_connection.py +1 -0
  7. sibi_dst/osmnx_helper/__init__.py +2 -2
  8. sibi_dst/osmnx_helper/v1/basemaps/__init__.py +0 -0
  9. sibi_dst/osmnx_helper/{basemaps → v1/basemaps}/router_plotter.py +85 -30
  10. sibi_dst/osmnx_helper/v2/__init__.py +0 -0
  11. sibi_dst/osmnx_helper/v2/base_osm_map.py +153 -0
  12. sibi_dst/osmnx_helper/v2/basemaps/__init__.py +0 -0
  13. sibi_dst/osmnx_helper/v2/basemaps/utils.py +0 -0
  14. sibi_dst/utils/__init__.py +3 -0
  15. sibi_dst/utils/data_utils.py +66 -25
  16. sibi_dst/utils/data_wrapper.py +222 -285
  17. sibi_dst/utils/date_utils.py +118 -113
  18. sibi_dst/utils/df_utils.py +7 -0
  19. sibi_dst/utils/log_utils.py +57 -18
  20. sibi_dst/utils/parquet_saver.py +4 -2
  21. sibi_dst/utils/phone_formatter.py +127 -0
  22. sibi_dst/utils/storage_manager.py +14 -7
  23. sibi_dst-0.3.43.dist-info/METADATA +194 -0
  24. {sibi_dst-0.3.40.dist-info → sibi_dst-0.3.43.dist-info}/RECORD +29 -22
  25. sibi_dst-0.3.40.dist-info/METADATA +0 -62
  26. /sibi_dst/osmnx_helper/{basemaps → v1}/__init__.py +0 -0
  27. /sibi_dst/osmnx_helper/{base_osm_map.py → v1/base_osm_map.py} +0 -0
  28. /sibi_dst/osmnx_helper/{basemaps → v1/basemaps}/calendar_html.py +0 -0
  29. /sibi_dst/osmnx_helper/{utils.py → v1/utils.py} +0 -0
  30. {sibi_dst-0.3.40.dist-info → sibi_dst-0.3.43.dist-info}/WHEEL +0 -0
@@ -3,11 +3,13 @@ from __future__ import annotations
3
3
  from ._df_helper import DfHelper
4
4
  from ._parquet_artifact import ParquetArtifact
5
5
  from ._parquet_reader import ParquetReader
6
+ from ._artifact_updater_multi_wrapper import ArtifactUpdaterMultiWrapper
6
7
  #from .data_cleaner import DataCleaner
7
8
 
8
9
  __all__ = [
9
10
  'DfHelper',
10
11
  'ParquetArtifact',
11
12
  'ParquetReader',
13
+ 'ArtifactUpdaterMultiWrapper',
12
14
  #'DataCleaner'
13
15
  ]
@@ -0,0 +1,262 @@
1
+ import asyncio
2
+ import logging
3
+ import datetime
4
+ import psutil
5
+ import time
6
+ from functools import total_ordering
7
+ from collections import defaultdict
8
+ from contextlib import asynccontextmanager
9
+ import signal
10
+ from sibi_dst.utils import Logger
11
+
12
+ @total_ordering
13
+ class PrioritizedItem:
14
+ def __init__(self, priority, artifact):
15
+ self.priority = priority
16
+ self.artifact = artifact
17
+
18
+ def __lt__(self, other):
19
+ return self.priority < other.priority
20
+
21
+ def __eq__(self, other):
22
+ return self.priority == other.priority
23
+
24
+ class ArtifactUpdaterMultiWrapper:
25
+ def __init__(self, wrapped_classes=None, debug=False, **kwargs):
26
+ self.wrapped_classes = wrapped_classes or {}
27
+ self.debug = debug
28
+ self.logger = kwargs.setdefault('logger',Logger.default_logger(logger_name=self.__class__.__name__))
29
+ self.logger.set_level(logging.DEBUG if debug else logging.INFO)
30
+
31
+ today = datetime.datetime.today()
32
+ self.today_str = today.strftime('%Y-%m-%d')
33
+ self.current_year_starts_on_str = datetime.date(today.year, 1, 1).strftime('%Y-%m-%d')
34
+ self.parquet_start_date = kwargs.get('parquet_start_date', self.current_year_starts_on_str)
35
+ self.parquet_end_date = kwargs.get('parquet_end_date', self.today_str)
36
+
37
+ # track concurrency and locks
38
+ self.locks = {}
39
+ self.worker_heartbeat = defaultdict(float)
40
+
41
+ # graceful shutdown handling
42
+ loop = asyncio.get_event_loop()
43
+ self.register_signal_handlers(loop)
44
+
45
+ # dynamic scaling config
46
+ self.min_workers = kwargs.get('min_workers', 1)
47
+ self.max_workers = kwargs.get('max_workers', 8)
48
+ self.memory_per_worker_gb = kwargs.get('memory_per_worker_gb', 1) # default 2GB per worker
49
+ self.monitor_interval = kwargs.get('monitor_interval', 10) # default monitor interval in seconds
50
+ self.retry_attempts = kwargs.get('retry_attempts', 3)
51
+ self.update_timeout_seconds = kwargs.get('update_timeout_seconds', 600)
52
+ self.lock_acquire_timeout_seconds = kwargs.get('lock_acquire_timeout_seconds', 10)
53
+
54
+ def register_signal_handlers(self, loop):
55
+ for sig in (signal.SIGINT, signal.SIGTERM):
56
+ loop.add_signal_handler(sig, lambda: asyncio.create_task(self.shutdown()))
57
+
58
+ async def shutdown(self):
59
+ self.logger.info("Shutdown signal received. Cleaning up...")
60
+ tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
61
+ [task.cancel() for task in tasks]
62
+ await asyncio.gather(*tasks, return_exceptions=True)
63
+ self.logger.info("Shutdown complete.")
64
+
65
+ def get_lock_for_artifact(self, artifact):
66
+ artifact_key = artifact.__class__.__name__
67
+ if artifact_key not in self.locks:
68
+ self.locks[artifact_key] = asyncio.Lock()
69
+ return self.locks[artifact_key]
70
+
71
+ def get_artifacts(self, data_type):
72
+ if data_type not in self.wrapped_classes:
73
+ raise ValueError(f"Unsupported data type: {data_type}")
74
+
75
+ return [
76
+ artifact_class(
77
+ parquet_start_date=self.parquet_start_date,
78
+ parquet_end_date=self.parquet_end_date,
79
+ logger=self.logger,
80
+ debug=self.debug
81
+ )
82
+ for artifact_class in self.wrapped_classes[data_type]
83
+ ]
84
+
85
+ def estimate_complexity(self, artifact):
86
+ try:
87
+ if hasattr(artifact, 'get_size_estimate'):
88
+ return artifact.get_size_estimate()
89
+ except Exception as e:
90
+ self.logger.warning(f"Failed to estimate complexity for {artifact}: {e}")
91
+ return 1 # default
92
+
93
+ def prioritize_tasks(self, artifacts):
94
+ queue = asyncio.PriorityQueue()
95
+ for artifact in artifacts:
96
+ complexity = self.estimate_complexity(artifact)
97
+ # we invert the complexity to ensure higher complexity -> higher priority
98
+ # if you want high complexity first, store negative complexity in the priority queue
99
+ # or if the smaller number means earlier processing, just keep as is
100
+ queue.put_nowait(PrioritizedItem(complexity, artifact))
101
+ return queue
102
+
103
+ async def resource_monitor(self, queue, workers):
104
+ """Monitor system resources and adjust worker count while queue is not empty."""
105
+ while True:
106
+ # break if queue done
107
+ if queue.empty():
108
+ await asyncio.sleep(0.5)
109
+ if queue.empty():
110
+ break
111
+
112
+ try:
113
+ available_memory = psutil.virtual_memory().available
114
+ worker_memory_bytes = self.memory_per_worker_gb * (1024 ** 3)
115
+ max_workers_by_memory = available_memory // worker_memory_bytes
116
+
117
+ # figure out how many workers we can sustain
118
+ # note: we also cap by self.max_workers
119
+ optimal_workers = min(psutil.cpu_count(), max_workers_by_memory, self.max_workers)
120
+
121
+ # ensure at least self.min_workers is used
122
+ optimal_workers = max(self.min_workers, optimal_workers)
123
+
124
+ current_worker_count = len(workers)
125
+
126
+ if optimal_workers > current_worker_count:
127
+ # we can add more workers if queue is not empty
128
+ diff = optimal_workers - current_worker_count
129
+ for _ in range(diff):
130
+ worker_id = len(workers)
131
+ # create a new worker
132
+ w = asyncio.create_task(self.worker(queue, worker_id))
133
+ workers.append(w)
134
+ self.logger.info(f"Added worker {worker_id}. Total workers: {len(workers)}")
135
+ elif optimal_workers < current_worker_count:
136
+ # remove some workers
137
+ diff = current_worker_count - optimal_workers
138
+ for _ in range(diff):
139
+ w = workers.pop()
140
+ w.cancel()
141
+ self.logger.info(f"Removed a worker. Total workers: {len(workers)}")
142
+
143
+ await asyncio.sleep(self.monitor_interval)
144
+
145
+ except asyncio.CancelledError:
146
+ # monitor is being shut down
147
+ break
148
+ except Exception as e:
149
+ self.logger.error(f"Error in resource_monitor: {e}")
150
+ await asyncio.sleep(self.monitor_interval)
151
+
152
+ @asynccontextmanager
153
+ async def artifact_lock(self, artifact):
154
+ lock = self.get_lock_for_artifact(artifact)
155
+ try:
156
+ await asyncio.wait_for(lock.acquire(), timeout=self.lock_acquire_timeout_seconds)
157
+ yield
158
+ except asyncio.TimeoutError:
159
+ self.logger.error(f"Timeout acquiring lock for artifact: {artifact.__class__.__name__}")
160
+ yield # continue but no actual lock was acquired
161
+ finally:
162
+ if lock.locked():
163
+ lock.release()
164
+
165
+ async def async_update_artifact(self, artifact, **kwargs):
166
+ for attempt in range(self.retry_attempts):
167
+ try:
168
+ async with self.artifact_lock(artifact):
169
+ self.logger.info(
170
+ f"Updating artifact: {artifact.__class__.__name__}, Attempt: {attempt + 1} of {self.retry_attempts}" )
171
+ start_time = time.time()
172
+ await asyncio.wait_for(
173
+ asyncio.to_thread(artifact.update_parquet, **kwargs),
174
+ timeout=self.update_timeout_seconds
175
+ )
176
+ elapsed_time = time.time() - start_time
177
+ self.logger.info(
178
+ f"Successfully updated artifact: {artifact.__class__.__name__} in {elapsed_time:.2f}s." )
179
+ return
180
+
181
+ except asyncio.TimeoutError:
182
+ self.logger.error(f"Timeout updating artifact {artifact.__class__.__name__}, Attempt: {attempt + 1}")
183
+ except Exception as e:
184
+ self.logger.error(
185
+ f"Error updating artifact {artifact.__class__.__name__}, Attempt: {attempt + 1}: {e}" )
186
+
187
+ # exponential backoff
188
+ await asyncio.sleep(2 ** attempt)
189
+
190
+ self.logger.error(f"All retry attempts failed for artifact: {artifact.__class__.__name__}")
191
+
192
+ async def worker(self, queue, worker_id, **kwargs):
193
+ """A worker that dynamically pulls tasks from the queue."""
194
+ while True:
195
+ try:
196
+ prioritized_item = await queue.get()
197
+ if prioritized_item is None:
198
+ break
199
+ artifact = prioritized_item.artifact
200
+ # heartbeat
201
+ self.worker_heartbeat[worker_id] = time.time()
202
+
203
+ await self.async_update_artifact(artifact, **kwargs)
204
+
205
+ except asyncio.CancelledError:
206
+ self.logger.info(f"Worker {worker_id} shutting down gracefully.")
207
+ break
208
+ except Exception as e:
209
+ self.logger.error(f"Error in worker {worker_id}: {e}")
210
+ finally:
211
+ queue.task_done()
212
+
213
+ async def process_tasks(self, queue, initial_workers, **kwargs):
214
+ """Start a set of workers and a resource monitor to dynamically adjust them."""
215
+ # create initial workers
216
+ workers = []
217
+ for worker_id in range(initial_workers):
218
+ w = asyncio.create_task(self.worker(queue, worker_id, **kwargs))
219
+ workers.append(w)
220
+
221
+ # start resource monitor
222
+ monitor_task = asyncio.create_task(self.resource_monitor(queue, workers))
223
+
224
+ # wait until queue is done
225
+ try:
226
+ await queue.join()
227
+ finally:
228
+ # cancel resource monitor
229
+ monitor_task.cancel()
230
+ # all workers done
231
+ for w in workers:
232
+ w.cancel()
233
+ await asyncio.gather(*workers, return_exceptions=True)
234
+
235
+ async def update_data(self, data_type, **kwargs):
236
+ self.logger.info(f"Processing wrapper group: {data_type} with {kwargs}")
237
+ artifacts = self.get_artifacts(data_type)
238
+ queue = self.prioritize_tasks(artifacts)
239
+
240
+ # compute initial worker count (this can be low if memory is low initially)
241
+ initial_workers = self.calculate_initial_workers(len(artifacts))
242
+ self.logger.info(f"Initial worker count: {initial_workers} for {len(artifacts)} artifacts")
243
+
244
+ total_start_time = time.time()
245
+ await self.process_tasks(queue, initial_workers, **kwargs)
246
+ total_time = time.time() - total_start_time
247
+ self.logger.info(f"Total processing time: {total_time:.2f} seconds.")
248
+
249
+ def calculate_initial_workers(self, artifact_count: int) -> int:
250
+ """Compute the initial number of workers before resource_monitor can adjust."""
251
+ self.logger.info("Calculating initial worker count...")
252
+ available_memory = psutil.virtual_memory().available
253
+ self.logger.info(f"Available memory: {available_memory / (1024 ** 3):.2f} GB")
254
+ worker_memory_bytes = self.memory_per_worker_gb * (1024 ** 3)
255
+ self.logger.info(f"Memory per worker: {worker_memory_bytes / (1024 ** 3):.2f} GB")
256
+ max_workers_by_memory = available_memory // worker_memory_bytes
257
+ self.logger.info(f"Max workers by memory: {max_workers_by_memory}")
258
+ # also consider CPU count and artifact_count
259
+ initial = min(psutil.cpu_count(), max_workers_by_memory, artifact_count, self.max_workers)
260
+ self.logger.info(f"Optimal workers: {initial} CPU: {psutil.cpu_count()} Max Workers: {self.max_workers}")
261
+ return max(self.min_workers, initial)
262
+
@@ -112,6 +112,7 @@ class DfHelper:
112
112
  :return: None
113
113
  """
114
114
  self.logger.debug(f"backend used: {self.backend}")
115
+ self.logger.debug(f"kwargs passed to backend plugins: {kwargs}")
115
116
  self._backend_query = self.__get_config(QueryConfig, kwargs)
116
117
  self._backend_params = self.__get_config(ParamsConfig, kwargs)
117
118
  if self.backend == 'django_db':
@@ -124,8 +125,8 @@ class DfHelper:
124
125
  elif self.backend == 'sqlalchemy':
125
126
  self.backend_sqlalchemy = self.__get_config(SqlAlchemyConnectionConfig, kwargs)
126
127
 
127
- @staticmethod
128
- def __get_config(model: [T], kwargs: Dict[str, Any]) -> Union[T]:
128
+
129
+ def __get_config(self, model: [T], kwargs: Dict[str, Any]) -> Union[T]:
129
130
  """
130
131
  Initializes a Pydantic model with the keys it recognizes from the kwargs,
131
132
  and removes those keys from the kwargs dictionary.
@@ -135,7 +136,9 @@ class DfHelper:
135
136
  """
136
137
  # Extract keys that the model can accept
137
138
  recognized_keys = set(model.__annotations__.keys())
139
+ self.logger.debug(f"recognized keys: {recognized_keys}")
138
140
  model_kwargs = {k: kwargs.pop(k) for k in list(kwargs.keys()) if k in recognized_keys}
141
+ self.logger.debug(f"model_kwargs: {model_kwargs}")
139
142
  return model(**model_kwargs)
140
143
 
141
144
  def load_parallel(self, **options):
@@ -1,11 +1,11 @@
1
+ import logging
1
2
  from typing import Optional, Any, Dict
2
3
 
3
4
  import dask.dataframe as dd
4
5
  import fsspec
5
6
 
6
7
  from sibi_dst.df_helper import DfHelper
7
- from sibi_dst.utils import DataWrapper
8
- from sibi_dst.utils import DateUtils
8
+ from sibi_dst.utils import DataWrapper, DateUtils, Logger
9
9
 
10
10
 
11
11
  class ParquetArtifact(DfHelper):
@@ -82,7 +82,12 @@ class ParquetArtifact(DfHelper):
82
82
  **kwargs,
83
83
  }
84
84
  self.df: Optional[dd.DataFrame] = None
85
+ self.debug = self.config.setdefault('debug', False)
86
+ self.logger = self.config.setdefault('logger',Logger.default_logger(logger_name=f'parquet_artifact_{__class__.__name__}'))
87
+ self.logger.set_level(logging.DEBUG if self.debug else logging.INFO)
85
88
  self.data_wrapper_class = data_wrapper_class
89
+ self.class_params = self.config.setdefault('class_params', None)
90
+ self.load_params = self.config.setdefault('load_params', None)
86
91
  self.date_field = self.config.setdefault('date_field', None)
87
92
  if self.date_field is None:
88
93
  raise ValueError('date_field must be set')
@@ -150,6 +155,7 @@ class ParquetArtifact(DfHelper):
150
155
 
151
156
  def _prepare_params(self, kwargs: Dict[str, Any]) -> Dict[str, Any]:
152
157
  """Prepare the parameters for generating the Parquet file."""
158
+ kwargs = {**self.config, **kwargs}
153
159
  return {
154
160
  'class_params': kwargs.pop('class_params', None),
155
161
  'date_field': kwargs.pop('date_field', self.date_field),
@@ -1,10 +1,11 @@
1
+ import logging
1
2
  from typing import Optional
2
3
 
3
4
  import dask.dataframe as dd
4
5
  import fsspec
5
6
 
6
7
  from sibi_dst.df_helper import DfHelper
7
-
8
+ from sibi_dst.utils import Logger
8
9
 
9
10
  class ParquetReader(DfHelper):
10
11
  """
@@ -53,6 +54,9 @@ class ParquetReader(DfHelper):
53
54
  **kwargs,
54
55
  }
55
56
  self.df: Optional[dd.DataFrame] = None
57
+ self.debug = self.config.setdefault('debug', False)
58
+ self.logger = self.config.setdefault('logger', Logger.default_logger(logger_name=self.__class__.__name__))
59
+ self.logger.set_level(logging.DEBUG if self.debug else logging.INFO)
56
60
  self.parquet_storage_path = self.config.setdefault('parquet_storage_path', None)
57
61
  if self.parquet_storage_path is None:
58
62
  raise ValueError('parquet_storage_path must be set')
@@ -63,3 +63,4 @@ class SqlAlchemyConnectionConfig(BaseModel):
63
63
  connection.execute(text("SELECT 1"))
64
64
  except OperationalError as e:
65
65
  raise ValueError(f"Failed to connect to the database: {e}")
66
+
@@ -1,7 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
- from .base_osm_map import BaseOsmMap
4
- from .utils import PBFHandler
3
+ from .v1.base_osm_map import BaseOsmMap
4
+ from .v1.utils import PBFHandler
5
5
 
6
6
  __all__ = [
7
7
  "BaseOsmMap",
File without changes
@@ -1,20 +1,21 @@
1
1
  from __future__ import annotations
2
- from sibi_dst.osmnx_helper.utils import get_distance_between_points, add_arrows
2
+ from sibi_dst.osmnx_helper.v1.utils import get_distance_between_points, add_arrows
3
3
  from collections import defaultdict
4
4
  import folium
5
5
  from folium.plugins import AntPath
6
6
  import networkx as nx
7
7
 
8
8
  from sibi_dst.osmnx_helper import BaseOsmMap
9
- from sibi_dst.osmnx_helper.basemaps.calendar_html import calendar_html
9
+ from sibi_dst.osmnx_helper.v1.basemaps.calendar_html import calendar_html
10
10
 
11
11
  class RoutePlotter(BaseOsmMap):
12
12
  def __init__(self, osmnx_graph, df, **kwargs):
13
13
  self.action_field = kwargs.pop('action_field', '')
14
14
  self.action_groups = kwargs.pop('action_groups', {})
15
15
  self.action_styles = kwargs.pop('action_styles', {})
16
- self.use_ant_path = kwargs.pop('use_ant_path', True)
17
- self.show_calendar = kwargs.pop('show_calendar', True)
16
+ self.use_ant_path = kwargs.pop('use_ant_path', False)
17
+ self.show_calendar = kwargs.pop('show_calendar', False)
18
+ self.show_order_markers = kwargs.pop('show_order_markers', False)
18
19
  self.show_map_title = kwargs.pop('show_map_title', True)
19
20
  self.sort_keys = kwargs.pop('sort_keys', None)
20
21
  self.main_route_layer = folium.FeatureGroup(name="Main Route")
@@ -24,6 +25,8 @@ class RoutePlotter(BaseOsmMap):
24
25
  self.actions = []
25
26
  self.action_group_counts = {action_group: 0 for action_group in self.action_groups}
26
27
  self.marker_count = 1
28
+ # Add a snapping threshold (in meters) to avoid drawing nodes/markers that are too close.
29
+ self.snap_distance = kwargs.pop('snap_distance', 30)
27
30
  kwargs.update({'calc_nearest_nodes': True})
28
31
  kwargs['dt_field'] = 'date_time'
29
32
  super().__init__(osmnx_graph, df, **kwargs)
@@ -36,6 +39,8 @@ class RoutePlotter(BaseOsmMap):
36
39
  self._calculate_routes()
37
40
  self._plot_routes()
38
41
  self._add_markers()
42
+ if self.show_order_markers:
43
+ self._add_order_markers()
39
44
  self.main_route_layer.add_to(self.osm_map)
40
45
  if self.show_calendar:
41
46
  self._add_calendar()
@@ -57,8 +62,8 @@ class RoutePlotter(BaseOsmMap):
57
62
  self.route_polylines.append((polyline, color))
58
63
  for action_group, action_markers in markers.items():
59
64
  self.markers[action_group].extend(action_markers)
60
- self.action_group_counts[action_group] += 1
61
- self.marker_count += 1
65
+ self.action_group_counts[action_group] += len(action_markers)
66
+ self.marker_count += len(action_markers)
62
67
  if self.verbose:
63
68
  print("Route and marker calculation complete.")
64
69
 
@@ -70,7 +75,7 @@ class RoutePlotter(BaseOsmMap):
70
75
 
71
76
  def _calculate_route(self, i):
72
77
  if self.verbose:
73
- print(f"Calculating for item:{i}")
78
+ print(f"Calculating for item: {i}")
74
79
  orig = self.nearest_nodes[i]
75
80
  dest = self.nearest_nodes[i + 1]
76
81
  try:
@@ -81,17 +86,31 @@ class RoutePlotter(BaseOsmMap):
81
86
  lats, lons = zip(*[(self.G.nodes[node]['y'] + offset, self.G.nodes[node]['x']) for node in route])
82
87
  color = 'blue' if i < self.max_distance_index else 'red'
83
88
  polyline = list(zip(lats, lons))
89
+ # Apply node snapping to the polyline to remove points that are too close.
90
+ polyline = self._snap_polyline(polyline)
84
91
  markers = self._calculate_markers(i)
85
92
  return polyline, color, markers
86
93
  except nx.NetworkXNoPath:
87
94
  if self.verbose:
88
- print(f"Item:{i}-No path found for {orig} to {dest}")
95
+ print(f"Item: {i} - No path found for {orig} to {dest}")
89
96
  return None, None, {}
90
97
  except nx.NodeNotFound:
91
98
  if self.verbose:
92
- print(f"Item:{i}-No path found for {orig} to {dest}")
99
+ print(f"Item: {i} - No path found for {orig} to {dest}")
93
100
  return None, None, {}
94
101
 
102
+ def _snap_polyline(self, polyline: list[tuple[float, float]]) -> list[tuple[float, float]]:
103
+ """
104
+ Returns a filtered polyline where consecutive points closer than snap_distance are removed.
105
+ """
106
+ if not polyline:
107
+ return polyline
108
+ snapped_polyline = [polyline[0]]
109
+ for point in polyline[1:]:
110
+ if get_distance_between_points(snapped_polyline[-1], point, 'm') >= self.snap_distance:
111
+ snapped_polyline.append(point)
112
+ return snapped_polyline
113
+
95
114
  def _calculate_markers(self, i):
96
115
  # Calculate markers for action groups
97
116
  markers = defaultdict(list)
@@ -110,24 +129,26 @@ class RoutePlotter(BaseOsmMap):
110
129
  def _plot_routes(self):
111
130
  if self.verbose:
112
131
  print("Plotting routes and markers...")
113
- # self.action_group_counts = {action_group: 0 for action_group in self.feature_groups.keys()}
114
132
  for polyline, color in self.route_polylines:
115
133
  if self.use_ant_path:
116
134
  AntPath(
117
135
  locations=polyline,
118
136
  color=color,
119
- weight=3, # Increase line thickness
120
- opacity=10, # Increase opacity
121
- # pulse_color=color,
122
- delay=1000, # Slower animation to reduce flickering
123
- # dash_array=[20, 30] # Adjust dash pattern if needed
137
+ weight=3, # Increased line thickness
138
+ opacity=10, # Increased opacity
139
+ delay=1000, # Slower animation to reduce flickering
124
140
  ).add_to(self.main_route_layer)
125
141
  else:
126
142
  folium.PolyLine(locations=polyline, color=color).add_to(self.main_route_layer)
127
143
  self.osm_map = add_arrows(self.osm_map, polyline, color, n_arrows=3)
128
- # Plot markers for action groups
144
+ # Plot markers for action groups with snapping to avoid drawing too many nearby markers.
129
145
  for action_group, action_markers in self.markers.items():
146
+ seen_positions = []
130
147
  for location, tooltip, popup_data, action_style in action_markers:
148
+ # Skip marker if a nearby marker (within snap_distance) has already been added.
149
+ if any(get_distance_between_points(location, pos, 'm') < self.snap_distance for pos in seen_positions):
150
+ continue
151
+ seen_positions.append(location)
131
152
  folium.Marker(
132
153
  location=location,
133
154
  popup=folium.Popup(popup_data, max_width=600),
@@ -145,11 +166,14 @@ class RoutePlotter(BaseOsmMap):
145
166
  def _add_markers(self):
146
167
  if self.verbose:
147
168
  print("Adding markers...")
148
- # Add start marker
169
+ # Add a start marker
149
170
  start_popup = folium.Popup(f"Start of route at {self.dt[0]}", max_width=300)
150
- folium.Marker(location=self.gps_points[0], popup=start_popup,
151
- icon=folium.Icon(icon='flag-checkered', prefix='fa')).add_to(self.osm_map)
152
- # Add total distance marker at the end
171
+ folium.Marker(
172
+ location=self.gps_points[0],
173
+ popup=start_popup,
174
+ icon=folium.Icon(icon='flag-checkered', prefix='fa')
175
+ ).add_to(self.osm_map)
176
+ # Add an end marker with total distance info
153
177
  folium.Marker(
154
178
  self.gps_points[-1],
155
179
  popup=f"End of Route at {self.dt[self.max_time_index]}. Total Distance Travelled: {self.total_distance / 1000:.2f} km",
@@ -165,15 +189,15 @@ class RoutePlotter(BaseOsmMap):
165
189
  def _add_map_title(self):
166
190
  if self.map_html_title and self.show_map_title:
167
191
  title_html = f'''
168
- <div style="position: fixed;
169
- top: 10px;
170
- left: 50%;
192
+ <div style="position: fixed;
193
+ top: 10px;
194
+ left: 50%;
171
195
  transform: translate(-50%, 0%);
172
- z-index: 9999;
173
- font-size: 24px;
174
- font-weight: bold;
175
- background-color: white;
176
- padding: 10px;
196
+ z-index: 9999;
197
+ font-size: 24px;
198
+ font-weight: bold;
199
+ background-color: white;
200
+ padding: 10px;
177
201
  border: 2px solid black;
178
202
  border-radius: 5px;">
179
203
  {self.map_html_title}
@@ -181,6 +205,37 @@ class RoutePlotter(BaseOsmMap):
181
205
  '''
182
206
  self.osm_map.get_root().html.add_child(folium.Element(title_html))
183
207
 
208
+ def _add_order_markers(self):
209
+ """Adds numbered markers to indicate the visit order."""
210
+ order_feature_group = folium.FeatureGroup(name="Visit Order")
211
+ for idx, location in enumerate(self.gps_points):
212
+ # Create a DivIcon with the number (starting at 1)
213
+ icon = folium.DivIcon(
214
+ icon_size=(24, 24),
215
+ icon_anchor=(12, 12),
216
+ html=f'''
217
+ <div style="
218
+ font-size: 12pt;
219
+ color: black;
220
+ background-color: white;
221
+ border: 1px solid black;
222
+ border-radius: 50%;
223
+ width: 24px;
224
+ height: 24px;
225
+ text-align: center;
226
+ line-height: 24px;">
227
+ {idx + 1}
228
+ </div>
229
+ '''
230
+ )
231
+ folium.Marker(
232
+ location=location,
233
+ icon=icon,
234
+ tooltip=f"GPS Set No. {idx + 1}: {self.dt[idx]}"
235
+ ).add_to(order_feature_group)
236
+
237
+ order_feature_group.add_to(self.osm_map)
238
+
184
239
  def _get_data(self, index):
185
- # implement in subclass to populate popups
186
- ...
240
+ # Implement in subclass to populate popups
241
+ ...
File without changes