From 3d4af01c1856e221b4eccfa70cbb3a5422b5f696 Mon Sep 17 00:00:00 2001
From: bcalmel <blaise.calmel@inrae.fr>
Date: Thu, 27 Jun 2024 18:33:32 +0200
Subject: [PATCH] Connect actionKML to export ADCP track for measurement with
 GPS

---
 UI/FigDischargeDistribution.py |  2 +-
 UI/main.py                     | 56 ++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/UI/FigDischargeDistribution.py b/UI/FigDischargeDistribution.py
index 9616d3a..8c78da9 100644
--- a/UI/FigDischargeDistribution.py
+++ b/UI/FigDischargeDistribution.py
@@ -173,7 +173,7 @@ class FigDischargeDistribution(object):
             colors = ['darkorange'] * len(nanmean)
 
         # Add boxplot to legend
-        legends.append(mpatches.Patch(facecolor=colors[0], edgecolor="k", linewidth=1))##36ed00
+        legends.append(mpatches.Patch(facecolor=colors[0], edgecolor="k", linewidth=1))
 
         # Plot boxplot/violinplot
         if len(data) > 0:
diff --git a/UI/main.py b/UI/main.py
index b20e317..fe60bd1 100644
--- a/UI/main.py
+++ b/UI/main.py
@@ -24,6 +24,7 @@ from PyQt5 import QtWidgets, QtGui, QtCore
 from PyQt5.QtCore import Qt, QTranslator
 from PyQt5.QtWidgets import QFileDialog, QApplication, QMainWindow
 import pickle
+import simplekml
 
 import pandas as pd
 from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
@@ -509,6 +510,8 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
         self.actionZoomGraph.triggered.connect(self.zoom)
         self.actionPanGraph.triggered.connect(self.pan)
 
+        self.actionKML.triggered.connect(self.export_kml)
+
     def disable_button(self):
         """ Disable action at start of the application.
         """
@@ -522,6 +525,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
         self.actionExport_baratinage.setEnabled(False)
         self.actionSave_pickle.setEnabled(False)
         self.actionTable.setEnabled(False)
+        self.actionKML.setEnabled(False)
 
         self.actionHomeGraph.setEnabled(False)
         self.actionZoomGraph.setEnabled(False)
@@ -3180,6 +3184,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
                 # self.actionExport_bareme.setEnabled(True)
                 # self.actionExport_baratinage.setEnabled(True)
                 self.actionSave_pickle.setEnabled(True)
+                self.actionKML.setEnabled(True)
         else:
             self.freeze = True
             self.menuFile.setEnabled(False)
@@ -3198,6 +3203,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
             self.actionExport_bareme.setEnabled(False)
             self.actionExport_baratinage.setEnabled(False)
             self.actionSave_pickle.setEnabled(False)
+            self.actionKML.setEnabled(False)
 
             self.actionHomeGraph.setEnabled(False)
             self.actionZoomGraph.setEnabled(False)
@@ -3401,6 +3407,56 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
             self.setWindowTitle(self.QRame_version + ' : ' + select_save.full_Name)
             QApplication.restoreOverrideCursor()
 
+    def export_kml(self):
+        """ Export ADCP track for measurement with GPS as kml
+        """
+        # Open dialog
+        select_save = SaveDialog(save_type='kml', parent=self)
+        if len(select_save.full_Name) > 0:
+            any_line = False
+            kml = simplekml.Kml(open=1)
+            for name, meas in self.worker_extract.measurements_dict.items():
+                gps = True
+                for transect_idx in meas.checked_transect_idx:
+                    if meas.transects[transect_idx].boat_vel.gga_vel is None or np.all(
+                            meas.transects[transect_idx].gps.diff_qual_ens[
+                                ~np.isnan(meas.transects[transect_idx].gps.diff_qual_ens)
+                            ] < 2):
+                        gps = False
+                        break
+                if gps:
+                    any_line = True
+                    meas_folder = kml.newfolder(name=name)
+                    for transect_idx in meas.checked_transect_idx:
+                        if meas.transects[transect_idx].gps is not None:
+                            lon = meas.transects[transect_idx].gps.gga_lon_ens_deg
+                            lon = lon[np.logical_not(np.isnan(lon))]
+                            lat = meas.transects[transect_idx].gps.gga_lat_ens_deg
+                            lat = lat[np.logical_not(np.isnan(lat))]
+                            line_name = meas.transects[transect_idx].file_name[:-4]
+                            lon_lat = tuple(zip(lon, lat))
+                            _ = meas_folder.newlinestring(name=line_name, coords=lon_lat)
+
+            if any_line:
+                kml.save(select_save.full_Name)
+
+                try:
+                    os.startfile(select_save.full_Name)
+                except os.error:
+                    self.reportWarning(
+                        text=self._translate(
+                            "Google Earth is not installed or is not associated with "
+                            "kml files."
+                        )
+                    )
+            else:
+                self.reportWarning(
+                    text=self._translate(
+                        "No measurements equipped with GPS."
+                    )
+                )
+
+
     @staticmethod
     def reportWarning(text):
         """ Display a message box with messages specified in text
-- 
GitLab