diff --git a/MapView/datasource.py b/MapView/datasource.py index 74ede8e..28c59c9 100644 --- a/MapView/datasource.py +++ b/MapView/datasource.py @@ -37,6 +37,7 @@ class Datasource: self.user_id = user_id self.connection_status = None self._new_points = [] + self._active_markers = [] asyncio.ensure_future(self.connect_to_server()) def get_new_points(self): @@ -60,6 +61,20 @@ class Datasource: self.connection_status = "Disconnected" Logger.debug("SERVER DISCONNECT") + def map_lat_lon_to_ProcessedAgentData(self, lat: float, lon: float) -> ProcessedAgentData | None: + distances = tuple((abs(lon - marker.latitude) ** 2 + abs(lat - marker.longitude) ** 2) ** 0.5 for marker in self._active_markers) + + if len(distances) == 0: + return None + + min_distance = min(distances) + marker = self._active_markers[distances.index(min_distance)] + + if min_distance < 0.005: + return marker + else: + return None + def handle_received_data(self, data): # Update your UI or perform actions with received data here Logger.debug(f"Received data: {data}") @@ -70,6 +85,9 @@ class Datasource: ], key=lambda v: v.timestamp, ) + + self._active_markers += [i for i in processed_agent_data_list if i.road_state != 'normal'] + new_points = [ ( processed_agent_data.longitude, diff --git a/MapView/main.py b/MapView/main.py index 53d7945..f40f242 100644 --- a/MapView/main.py +++ b/MapView/main.py @@ -41,8 +41,6 @@ class MapViewApp(App): self.bump_markers: list[MapMarker] = [] self.pothole_markers: list[MapMarker] = [] - self.root_window.bind(on_touch_up = self.on_touch_up) - def on_start(self): """ Встановлює необхідні маркери, викликає функцію для оновлення мапи @@ -106,10 +104,8 @@ class MapViewApp(App): if user_id == config.TRACK_ID: self.mapview.center_on(lat, lon) - def map_lat_lon_to_pothole(self, lat: float, lon: float) -> MapMarker | None: - click_tolerance = self.mapview.zoom * 10 - flt = filter(lambda marker: abs(lat - marker.lat) + abs(lon - marker.lon) < click_tolerance, - self.pothole_markers) + 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) return next(flt) def set_pothole_marker(self, point): @@ -145,16 +141,28 @@ class MapViewApp(App): if lat is None or lon is None: return - pothole = self.map_lat_lon_to_pothole(lat, lon) - if pothole: - self.mapview.remove_marker(pothole) - self.pothole_markers.pop(self.pothole_markers.index(pothole)) + clicked_marker_data = self.datasource.map_lat_lon_to_ProcessedAgentData(lat, lon) - def on_touch_up(self, touch): - self.mapview.on_touch_up(touch) + if not clicked_marker_data: + return + + clicked_marker = self.map_lat_lon_to_marker(clicked_marker_data.latitude, clicked_marker_data.longitude) + + self.mapview.remove_marker(clicked_marker) + + pothole_index = self.pothole_markers.index(clicked_marker) + bump_index = self.bump_markers.index(clicked_marker) + + if pothole_marker >= 0: + self.pothole_markers.pop(pothole_index) + elif bump_index >= 0: + self.bump_markers.pop(bump_index) + + def on_touch_down(self, widget, touch): if touch.button == "right": coordinate = self.mapview.get_latlon_at(touch.x, touch.y, self.mapview.zoom) self.delete_pothole_marker(coordinate) + return True def build(self): @@ -168,6 +176,8 @@ class MapViewApp(App): lon=30.5234 ) + self.mapview.bind(on_touch_down = self.on_touch_down) + return self.mapview