From 216239bfce820aa02a1db2edfe55e7b0b4630d69 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Fri, 15 Aug 2025 10:08:26 +0200 Subject: [PATCH] Scenario: Fix profile saving and loading for scenario. --- src/Model/Geometry/Point.py | 2 +- src/Model/Geometry/PointXYZ.py | 18 +++++++++------ src/Model/Geometry/ProfileXYZ.py | 38 +++++++++++++++++++------------- src/Model/Geometry/Reach.py | 9 +++++--- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/Model/Geometry/Point.py b/src/Model/Geometry/Point.py index 8c8137ea..f55335e5 100644 --- a/src/Model/Geometry/Point.py +++ b/src/Model/Geometry/Point.py @@ -24,7 +24,7 @@ class Point(object): profile=None, status=None, owner_scenario=-1): super(Point, self).__init__( - status=status, + id=id, status=status, owner_scenario=owner_scenario ) diff --git a/src/Model/Geometry/PointXYZ.py b/src/Model/Geometry/PointXYZ.py index 08d59d32..aa6549e9 100644 --- a/src/Model/Geometry/PointXYZ.py +++ b/src/Model/Geometry/PointXYZ.py @@ -164,13 +164,15 @@ class PointXYZ(Point, SQLSubModel): @classmethod def _db_load(cls, execute, data=None): + new = [] + status = data["status"] profile = data["profile"] scenario = data["scenario"] loaded = data['loaded_pid'] if scenario is None: - return + return new table = execute( "SELECT pamhyr_id, deleted, " + @@ -194,7 +196,7 @@ class PointXYZ(Point, SQLSubModel): sl = next(it) owner_scenario = next(it) - new = cls( + point = cls( id=pid, name=name, x=x, y=y, z=z, @@ -203,12 +205,12 @@ class PointXYZ(Point, SQLSubModel): owner_scenario=owner_scenario ) if deleted: - new.set_as_deleted() + point.set_as_deleted() if sl == -1 or sl is None: - new._sl = None + point._sl = None else: - new._sl = next( + point._sl = next( filter( lambda s: s.pamhyr_id == sl, data["sediment_layers_list"].sediment_layers @@ -216,12 +218,14 @@ class PointXYZ(Point, SQLSubModel): ) loaded.add(pid) - yield new + new.append(point) data["scenario"] = scenario.parent - yield from cls._db_load(execute, data) + new += cls._db_load(execute, data) data["scenario"] = scenario + return new + def _db_save(self, execute, data=None): if not self.must_be_saved(): return True diff --git a/src/Model/Geometry/ProfileXYZ.py b/src/Model/Geometry/ProfileXYZ.py index 18ebe64b..6353bc54 100644 --- a/src/Model/Geometry/ProfileXYZ.py +++ b/src/Model/Geometry/ProfileXYZ.py @@ -49,6 +49,7 @@ class ProfileXYZ(Profile, SQLSubModel): def __init__(self, id: int = -1, + ind: int = -1, name: str = "", rk: float = 0., reach=None, @@ -70,9 +71,8 @@ class ProfileXYZ(Profile, SQLSubModel): """ super(ProfileXYZ, self).__init__( id=id, - num=num, name=name, - rk=rk, + num=num, rk=rk, code1=code1, code2=code2, _type="XYZ", reach=reach, @@ -80,6 +80,7 @@ class ProfileXYZ(Profile, SQLSubModel): owner_scenario=owner_scenario ) + self._db_ind = ind self.tab = Tabulation([], [], []) self.tab_up_to_date = False self.time_z = 0.0 @@ -219,16 +220,18 @@ class ProfileXYZ(Profile, SQLSubModel): @classmethod def _db_load(cls, execute, data=None): + new = [] + status = data["status"] scenario = data["scenario"] loaded = data['loaded_pid'] reach = data["reach"] if scenario is None: - return + return new table = execute( - "SELECT pamhyr_id, deleted, name, rk, num, " + + "SELECT pamhyr_id, ind, deleted, name, rk, num, " + "code1, code2, sl, scenario " + "FROM geometry_profileXYZ " + f"WHERE reach = {reach.id} " + @@ -241,6 +244,7 @@ class ProfileXYZ(Profile, SQLSubModel): it = iter(row) pid = next(it) + ind = next(it) deleted = (next(it) == 1) name = next(it) rk = next(it) @@ -250,8 +254,8 @@ class ProfileXYZ(Profile, SQLSubModel): sl = next(it) owner_scenario = next(it) - new = cls( - id=pid, num=num, + profile = cls( + id=pid, ind=ind, num=num, name=name, rk=rk, code1=code1, code2=code2, reach=reach, @@ -259,27 +263,31 @@ class ProfileXYZ(Profile, SQLSubModel): owner_scenario=owner_scenario ) if deleted: - new.set_as_deleted() + profile.set_as_deleted() if sl == -1 or sl is None: - new._sl = None + profile._sl = None else: - new._sl = next( + profile._sl = next( filter( lambda s: s.id == sl, data["sediment_layers_list"].sediment_layers - ), None) + ), + None + ) - data["profile"] = new - new._points = PointXYZ._db_load(execute, data.copy()) + data["profile"] = profile + profile._points = PointXYZ._db_load(execute, data.copy()) loaded.add(pid) - yield new + new.append(profile) data["scenario"] = scenario.parent - yield from cls._db_load(execute, data) + new += cls._db_load(execute, data) data["scenario"] = scenario + return new + def _db_save(self, execute, data=None): if not self.must_be_saved(): return True @@ -991,7 +999,7 @@ class ProfileXYZ(Profile, SQLSubModel): self.modified() def shift(self, x, y, z): - for p in self.points: + for p in self._points: p.x = p.x + x p.y = p.y + y p.z = p.z + z diff --git a/src/Model/Geometry/Reach.py b/src/Model/Geometry/Reach.py index f791c4b7..487563a8 100644 --- a/src/Model/Geometry/Reach.py +++ b/src/Model/Geometry/Reach.py @@ -72,9 +72,12 @@ class Reach(SQLSubModel): def _db_load(cls, execute, data=None): new = cls(status=data["status"], parent=data["reach"]) - new._profiles = ProfileXYZ._db_load( - execute, - data=data.copy() + new._profiles = sorted( + ProfileXYZ._db_load( + execute, + data=data.copy() + ), + key=lambda p: p._db_ind ) return new