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
|
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
|
|
|
|
|
|
|
|
class MapViewApp(App):
|
|
|
|
|
def __init__(self, **kwargs):
|
2026-03-08 13:32:23 +02:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
self.mapview = None
|
|
|
|
|
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
|
|
|
# додати необхідні змінні
|
|
|
|
|
self.bump_markers = []
|
2026-03-05 14:43:59 +02:00
|
|
|
self.pothole_markers = []
|
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()
|
|
|
|
|
Clock.schedule_interval(self.update, 5)
|
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
|
|
|
|
|
|
|
|
self.mapview.center_on(lat, lon)
|
2024-02-26 14:53:24 +02:00
|
|
|
|
|
|
|
|
def set_pothole_marker(self, point):
|
2026-03-05 14:43:59 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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-05 17:34:55 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
marker = MapMarker(
|
|
|
|
|
lat=lat,
|
|
|
|
|
lon=lon,
|
|
|
|
|
source="images/bump.png"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.mapview.add_marker(marker)
|
|
|
|
|
self.bump_markers.append(marker)
|
|
|
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
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()
|