2024-02-26 15:16:47 +02:00
|
|
|
import asyncio
|
2024-02-26 14:53:24 +02:00
|
|
|
from kivy.app import App
|
|
|
|
|
from kivy_garden.mapview import MapMarker, MapView
|
|
|
|
|
from kivy.clock import Clock
|
|
|
|
|
from lineMapLayer import LineMapLayer
|
2024-03-13 15:14:48 +02:00
|
|
|
from datasource import Datasource
|
2026-03-25 21:35:21 +02:00
|
|
|
import config
|
2024-02-26 14:53:24 +02:00
|
|
|
|
2026-03-13 20:41:16 +02:00
|
|
|
line_layer_colors = [
|
|
|
|
|
[1, 0, 0, 1],
|
|
|
|
|
[1, 0.5, 0, 1],
|
|
|
|
|
[0, 1, 0, 1],
|
|
|
|
|
[0, 1, 1, 1],
|
|
|
|
|
[0, 0, 1, 1],
|
|
|
|
|
[1, 0, 1, 1],
|
|
|
|
|
]
|
2024-02-26 14:53:24 +02:00
|
|
|
|
2026-03-25 19:05:16 +01:00
|
|
|
|
|
|
|
|
def get_lat_lon(point: dict[str, float] | list[float] | tuple[float, float]) -> tuple[float, float] | None:
|
|
|
|
|
if isinstance(point, dict):
|
|
|
|
|
lat = point.get("lat")
|
|
|
|
|
lon = point.get("lon")
|
|
|
|
|
else:
|
|
|
|
|
lat, lon = point
|
|
|
|
|
|
|
|
|
|
if lat is None or lon is None:
|
|
|
|
|
return None
|
|
|
|
|
return lat, lon
|
|
|
|
|
|
|
|
|
|
|
2024-02-26 14:53:24 +02:00
|
|
|
class MapViewApp(App):
|
|
|
|
|
def __init__(self, **kwargs):
|
2026-03-08 13:32:23 +02:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
2026-03-25 19:05:16 +01:00
|
|
|
self.mapview: MapView | None = None
|
2026-03-08 13:32:23 +02:00
|
|
|
self.datasource = Datasource(user_id=1)
|
2026-03-13 20:41:16 +02:00
|
|
|
self.line_layers = dict()
|
|
|
|
|
self.car_markers = dict()
|
2024-02-26 14:53:24 +02:00
|
|
|
|
2026-03-05 17:34:55 +02:00
|
|
|
# додати необхідні змінні
|
2026-03-25 19:51:10 +01:00
|
|
|
self.bump_markers: list[MapMarker] = []
|
|
|
|
|
self.pothole_markers: list[MapMarker] = []
|
2026-03-05 17:34:55 +02:00
|
|
|
|
2024-02-26 14:53:24 +02:00
|
|
|
def on_start(self):
|
|
|
|
|
"""
|
|
|
|
|
Встановлює необхідні маркери, викликає функцію для оновлення мапи
|
|
|
|
|
"""
|
2026-03-13 19:02:07 +02:00
|
|
|
self.update()
|
2026-03-24 16:57:29 +02:00
|
|
|
Clock.schedule_interval(self.update, 0.1)
|
2024-02-26 14:53:24 +02:00
|
|
|
|
|
|
|
|
def update(self, *args):
|
|
|
|
|
"""
|
|
|
|
|
Викликається регулярно для оновлення мапи
|
|
|
|
|
"""
|
2026-03-08 13:32:23 +02:00
|
|
|
new_points = self.datasource.get_new_points()
|
|
|
|
|
|
|
|
|
|
if not new_points:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for point in new_points:
|
|
|
|
|
|
2026-03-13 20:41:16 +02:00
|
|
|
lat, lon, road_state, user_id = point
|
2026-03-08 13:32:23 +02:00
|
|
|
|
|
|
|
|
# Оновлює лінію маршрута
|
2026-03-13 20:41:16 +02:00
|
|
|
if user_id not in self.line_layers:
|
2026-03-13 20:47:09 +02:00
|
|
|
self.line_layers[user_id] = LineMapLayer(color = line_layer_colors[user_id % len(line_layer_colors)])
|
2026-03-13 20:41:16 +02:00
|
|
|
self.mapview.add_layer(self.line_layers[user_id])
|
|
|
|
|
|
|
|
|
|
self.line_layers[user_id].add_point((lat, lon))
|
2026-03-08 13:32:23 +02:00
|
|
|
|
|
|
|
|
# Оновлює маркер маниши
|
2026-03-13 20:41:16 +02:00
|
|
|
self.update_car_marker(lat, lon, user_id)
|
2026-03-08 13:32:23 +02:00
|
|
|
|
|
|
|
|
# Перевіряємо стан дороги
|
|
|
|
|
self.check_road_quality(point)
|
2024-02-26 14:53:24 +02:00
|
|
|
|
2026-03-07 17:12:23 +02:00
|
|
|
def check_road_quality(self, point):
|
|
|
|
|
"""
|
|
|
|
|
Аналізує дані акселерометра для подальшого визначення
|
|
|
|
|
та відображення ям та лежачих поліцейських
|
|
|
|
|
"""
|
|
|
|
|
if len(point) < 3:
|
|
|
|
|
return
|
|
|
|
|
|
2026-03-13 20:41:16 +02:00
|
|
|
lat, lon, road_state, user_id = point
|
2026-03-07 17:12:23 +02:00
|
|
|
|
|
|
|
|
if road_state == "pothole":
|
|
|
|
|
self.set_pothole_marker((lat, lon))
|
|
|
|
|
elif road_state == "bump":
|
|
|
|
|
self.set_bump_marker((lat, lon))
|
2024-02-26 14:53:24 +02:00
|
|
|
|
2026-03-13 20:41:16 +02:00
|
|
|
def update_car_marker(self, lat, lon, user_id):
|
2024-02-26 14:53:24 +02:00
|
|
|
"""
|
|
|
|
|
Оновлює відображення маркера машини на мапі
|
|
|
|
|
:param point: GPS координати
|
|
|
|
|
"""
|
2026-03-13 20:41:16 +02:00
|
|
|
if user_id not in self.car_markers:
|
|
|
|
|
self.car_markers[user_id] = MapMarker(lat=lat, lon=lon, source='./images/car.png')
|
|
|
|
|
self.mapview.add_marker(self.car_markers[user_id])
|
2026-03-07 16:55:20 +02:00
|
|
|
else:
|
2026-03-13 20:41:16 +02:00
|
|
|
self.car_markers[user_id].lat = lat
|
|
|
|
|
self.car_markers[user_id].lon = lon
|
2026-03-07 16:55:20 +02:00
|
|
|
|
2026-03-25 21:35:21 +02:00
|
|
|
if user_id == config.TRACK_ID:
|
2026-03-25 14:57:38 +02:00
|
|
|
self.mapview.center_on(lat, lon)
|
2024-02-26 14:53:24 +02:00
|
|
|
|
2026-03-26 21:11:02 +02:00
|
|
|
def map_lat_lon_to_marker(self, lat: float, lon: float) -> MapMarker | None:
|
|
|
|
|
flt = filter(lambda marker: lon == marker.lat and lat == marker.lon, self.pothole_markers + self.bump_markers)
|
2026-03-26 22:18:02 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
return next(flt)
|
|
|
|
|
except StopIteration as e:
|
|
|
|
|
return None
|
2026-03-25 19:51:58 +01:00
|
|
|
|
2024-02-26 14:53:24 +02:00
|
|
|
def set_pothole_marker(self, point):
|
2026-03-25 19:52:33 +01:00
|
|
|
lat, lon = get_lat_lon(point)
|
2026-03-05 14:43:59 +02:00
|
|
|
if lat is None or lon is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
marker = MapMarker(
|
|
|
|
|
lat=lat,
|
|
|
|
|
lon=lon,
|
|
|
|
|
source="images/pothole.png"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.mapview.add_marker(marker)
|
|
|
|
|
self.pothole_markers.append(marker)
|
2024-02-26 14:53:24 +02:00
|
|
|
|
|
|
|
|
def set_bump_marker(self, point):
|
2026-03-25 19:05:16 +01:00
|
|
|
lat, lon = get_lat_lon(point)
|
2026-03-05 17:34:55 +02:00
|
|
|
if lat is None or lon is None:
|
|
|
|
|
return
|
2026-03-25 19:05:16 +01:00
|
|
|
|
2026-03-05 17:34:55 +02:00
|
|
|
marker = MapMarker(
|
|
|
|
|
lat=lat,
|
|
|
|
|
lon=lon,
|
2026-03-25 19:05:16 +01:00
|
|
|
source="images/bump.png"
|
2026-03-05 17:34:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.mapview.add_marker(marker)
|
|
|
|
|
self.bump_markers.append(marker)
|
|
|
|
|
|
2026-03-25 19:52:54 +01:00
|
|
|
def delete_pothole_marker(self, point):
|
2026-03-25 19:05:16 +01:00
|
|
|
lat, lon = get_lat_lon(point)
|
|
|
|
|
if lat is None or lon is None:
|
|
|
|
|
return
|
|
|
|
|
|
2026-03-26 21:11:02 +02:00
|
|
|
clicked_marker_data = self.datasource.map_lat_lon_to_ProcessedAgentData(lat, lon)
|
|
|
|
|
|
|
|
|
|
if not clicked_marker_data:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
clicked_marker = self.map_lat_lon_to_marker(clicked_marker_data.latitude, clicked_marker_data.longitude)
|
2026-03-25 19:05:16 +01:00
|
|
|
|
2026-03-26 22:18:02 +02:00
|
|
|
if clicked_marker == None:
|
|
|
|
|
return
|
2026-03-26 21:11:02 +02:00
|
|
|
|
2026-03-26 22:18:02 +02:00
|
|
|
self.mapview.remove_marker(clicked_marker)
|
2026-03-26 21:11:02 +02:00
|
|
|
|
2026-03-26 22:18:02 +02:00
|
|
|
if clicked_marker in self.pothole_markers:
|
|
|
|
|
self.pothole_markers.pop(self.pothole_markers.index(clicked_marker))
|
|
|
|
|
elif clicked_marker in self.bump_markers:
|
|
|
|
|
self.bump_markers.pop(self.bump_markers.index(clicked_marker))
|
2026-03-26 21:11:02 +02:00
|
|
|
|
|
|
|
|
def on_touch_down(self, widget, touch):
|
2026-03-26 11:32:36 +01:00
|
|
|
if touch.button == "right":
|
|
|
|
|
coordinate = self.mapview.get_latlon_at(touch.x, touch.y, self.mapview.zoom)
|
|
|
|
|
self.delete_pothole_marker(coordinate)
|
2026-03-26 21:11:02 +02:00
|
|
|
return True
|
2026-03-26 11:32:36 +01:00
|
|
|
|
2024-02-26 14:53:24 +02:00
|
|
|
|
|
|
|
|
def build(self):
|
|
|
|
|
"""
|
|
|
|
|
Ініціалізує мапу MapView(zoom, lat, lon)
|
|
|
|
|
:return: мапу
|
|
|
|
|
"""
|
2026-03-08 13:32:23 +02:00
|
|
|
self.mapview = MapView(
|
|
|
|
|
zoom=15,
|
|
|
|
|
lat=50.4501,
|
|
|
|
|
lon=30.5234
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-26 21:11:02 +02:00
|
|
|
self.mapview.bind(on_touch_down = self.on_touch_down)
|
|
|
|
|
|
2024-02-26 14:53:24 +02:00
|
|
|
return self.mapview
|
|
|
|
|
|
|
|
|
|
|
2024-02-26 14:54:25 +02:00
|
|
|
if __name__ == "__main__":
|
2024-02-26 15:16:47 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
loop.run_until_complete(MapViewApp().async_run(async_lib="asyncio"))
|
|
|
|
|
loop.close()
|