I work in project for my university, the project is to develop a map using python and add AIS live data and display the ships on the map. I created the map and I make code for decode the AIS data but still I can't display the live tracking of the ship on the map.So I face issue in visualisation the ship's data on the map.
this is the code
import sys import serial import folium from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtCore import QTimer
class MapWidget(QWidget):
def init(self, parent=None):
super(MapWidget, self).__init__(parent)
# Create a Folium map object
self.msc_map = folium.Map(location=[23.631234255986154, 58.26947439827522], zoom_start=15)
# Create a QWebEngineView object to display the map
self.webview = QWebEngineView()
self.webview.setHtml(self.msc_map.get_root().render())
# Create a layout and add the webview to it
layout = QVBoxLayout(self)
layout.addWidget(self.webview)
# Set up AIS data processing
self.serial_port = serial.Serial("COM3", 38400) # Adjust baud rate as needed
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_data)
self.timer.start(1000) # Adjust the interval as needed
def update_data(self):
if self.serial_port.isOpen():
data = self.serial_port.readline().decode('utf-8')
self.decode_and_update_map(data)
def decode_and_update_map(self, data):
try:
# Assuming the data format is "!AIVDM,1,1,,A,...."
if "!AIVDM" in data:
parts = data.split(',')
if len(parts) >= 8 and parts[5] in ('A', 'B') and parts[7] in ('A', 'B'):
latitude = self.extract_coordinate(parts[6])
longitude = self.extract_coordinate(parts[8])
# Add a play icon marker to the map
folium.Marker(
location=[latitude, longitude],
icon=folium.Icon(color='blue', icon='play')
).add_to(self.msc_map)
# Save the updated map to HTML
self.msc_map.save('AIS_Map.html')
# Reload the web view to reflect the changes
self.webview.setHtml(self.msc_map.get_root().render())
except ValueError as e:
print(f"Error parsing latitude and longitude: {e}")
def extract_coordinate(self, coordinate_part):
degrees = float(coordinate_part[:2])
minutes = float(coordinate_part[2:])
decimal_degrees = degrees + minutes / 60.0
return decimal_degrees
def closeEvent(self, event):
self.timer.stop()
self.serial_port.close()
event.accept()
if name == '__main__':
app = QApplication(sys.argv)
widget = MapWidget()
widget.show()
sys.exit(app.exec_())
Fix the code and let me know the issu