From 043d8d839a9b4a79032761609dbbc0a6a1fb273a Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Tue, 25 Jul 2023 13:28:47 +0200 Subject: [PATCH] SL: Reach: Profile: Add Plot. --- src/Model/Geometry/Profile.py | 33 +++++++ src/View/SedimentLayers/Reach/Plot.py | 1 + src/View/SedimentLayers/Reach/Profile/Plot.py | 97 +++++++++++++++++++ .../SedimentLayers/Reach/Profile/Window.py | 15 +-- 4 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 src/View/SedimentLayers/Reach/Profile/Plot.py diff --git a/src/Model/Geometry/Profile.py b/src/Model/Geometry/Profile.py index 483082b0..7199730f 100644 --- a/src/Model/Geometry/Profile.py +++ b/src/Model/Geometry/Profile.py @@ -275,6 +275,39 @@ class Profile(object): ) self._status.modified() + # Sediment Layers + + def get_sl(self): + """Get sediment layer height of points + + Get sediment layer of points (without spesific point sl) + + Returns: + List of sediment layers height + """ + res = [] + psl = [point.sl for point in self.points] + + # Compute max number of layers + sl_max = 0 + for sl in psl: + n = 0 if sl is None else len(sl) + sl_max = max(n, sl_max) + + # Create list of height for each sl and each layer + for i in range(0, sl_max): + cur = [] + # Compute new layer line for each sl + for sl in psl: + if sl is not None and i < len(sl): + cur.append(sl.get(i).height) + else: + cur.append(0) + # Add layer line to result + res.append(cur) + + return res + # Abstract method, must be implemented for in non abstract class def get_station(self): raise NotImplementedMethodeError(self, self.get_station) diff --git a/src/View/SedimentLayers/Reach/Plot.py b/src/View/SedimentLayers/Reach/Plot.py index af7a54ed..1ab6daba 100644 --- a/src/View/SedimentLayers/Reach/Plot.py +++ b/src/View/SedimentLayers/Reach/Plot.py @@ -58,6 +58,7 @@ class Plot(APlot): left = min(kp), right = max(kp) ) + # Compute sediment layer in function to profile z_min z_sl = reduce( lambda acc, v: acc + [ list( diff --git a/src/View/SedimentLayers/Reach/Profile/Plot.py b/src/View/SedimentLayers/Reach/Profile/Plot.py new file mode 100644 index 00000000..b8c9b66f --- /dev/null +++ b/src/View/SedimentLayers/Reach/Profile/Plot.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- + +import logging + +from functools import reduce + +from tools import timer +from View.Plot.APlot import APlot + +from PyQt5.QtCore import ( + QCoreApplication +) + +_translate = QCoreApplication.translate + +logger = logging.getLogger() + +class Plot(APlot): + def __init__(self, canvas=None, data=None, toolbar=None, + display_current=True): + super(Plot, self).__init__( + canvas=canvas, + data=data, + toolbar=toolbar + ) + + self._display_current = display_current + + self.line_kp_zmin = None + self.line_kp_sl = [] + + @timer + def draw(self): + self.canvas.axes.cla() + self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5) + + if self.data is None: + return + + if self.data.number_points == 0: + return + + self.canvas.axes.set_xlabel( + _translate("MainWindow_reach", "X (m)"), + color='green', fontsize=12 + ) + self.canvas.axes.set_ylabel( + _translate("MainWindow_reach", "Height (m)"), + color='green', fontsize=12 + ) + + x = self.data.get_station() + z = self.data.z() + sl = self.data.get_sl() + + self.canvas.axes.set_xlim( + left = min(x), right = max(x) + ) + + # Compute sediment layer in function to point z + z_sl = reduce( + lambda acc, v: acc + [ + list( + map(lambda x, y: y - x, v, acc[-1]) + ) + ], + sl, + [z] + ) + + for i, zsl in enumerate(z_sl): + self.line_kp_sl.append(None) + self.line_kp_sl[i], = self.canvas.axes.plot( + x, zsl, + linestyle="solid" if i == 0 else "--", + lw=1.8, + color='grey' if i == 0 else None + ) + + self.canvas.figure.tight_layout() + self.canvas.figure.canvas.draw_idle() + if self.toolbar is not None: + self.toolbar.update() + + self._init = True + + @timer + def update(self, ind=None): + if self._init == False: + self.draw() + return + + if ind is None: + logger.info("TODO: Update") + + self.canvas.axes.autoscale_view(True, True, True) + self.canvas.figure.canvas.draw_idle() diff --git a/src/View/SedimentLayers/Reach/Profile/Window.py b/src/View/SedimentLayers/Reach/Profile/Window.py index 8f15a27e..a622bfb5 100644 --- a/src/View/SedimentLayers/Reach/Profile/Window.py +++ b/src/View/SedimentLayers/Reach/Profile/Window.py @@ -26,6 +26,7 @@ from PyQt5.QtWidgets import ( from View.SedimentLayers.Reach.Profile.UndoCommand import * from View.SedimentLayers.Reach.Profile.Table import * +from View.SedimentLayers.Reach.Profile.Plot import Plot from View.Plot.MplCanvas import MplCanvas from View.SedimentLayers.Reach.Profile.translate import * @@ -108,13 +109,13 @@ class ProfileSedimentLayersWindow(ASubMainWindow, ListedSubWindow): self.plot_layout = self.find(QVBoxLayout, "verticalLayout") self.plot_layout.addWidget(self.canvas) - # self.plot = PlotKPC( - # canvas = self.canvas, - # data = self._reach.reach, - # toolbar = None, - # display_current = False - # ) - # self.plot.draw() + self.plot = Plot( + canvas = self.canvas, + data = self._profile, + toolbar = None, + display_current = False + ) + self.plot.draw() def setup_connections(self): -- GitLab