From d0b7c4d1897f6c22e589b3a0771134ccaa53d406 Mon Sep 17 00:00:00 2001 From: Markus Metz Date: Mon, 4 Sep 2023 20:17:08 +0200 Subject: [PATCH] fix mapset support --- python/grass/temporal/base.py | 51 +++++++++++++++++++++++------- python/grass/temporal/open_stds.py | 1 + python/grass/temporal/register.py | 3 +- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/python/grass/temporal/base.py b/python/grass/temporal/base.py index 36b738aa500..d43815b5040 100644 --- a/python/grass/temporal/base.py +++ b/python/grass/temporal/base.py @@ -30,6 +30,7 @@ get_tgis_dbmi_paramstyle, SQLDatabaseInterfaceConnection, get_current_mapset, + get_available_temporal_mapsets, ) ############################################################################### @@ -242,13 +243,13 @@ class SQLDatabaseInterface(DictSQLSerializer): """ - def __init__(self, table=None, ident=None): + def __init__(self, table=None, ident=None, tgis_mapset=None): """Constructor of this class :param table: The name of the table :param ident: The identifier (primary key) of this object in the database table - :param tgis_mapset: the mapset of the tgis db to be used + :param tgis_mapset: the mapset to be used for the tgis db """ DictSQLSerializer.__init__(self) @@ -261,6 +262,7 @@ def __init__(self, table=None, ident=None): self.data_mapset = self.ident.split("@" "")[1] else: self.data_mapset = None + self.tgis_mapset = tgis_mapset def get_table_name(self): """Return the name of the table in which the internal @@ -330,17 +332,38 @@ def is_in_db(self, dbif=None, mapset=None): # determine correct mapset for the temporal database if mapset is None: - mapset = get_current_mapset() + mapset = self.tgis_mapset - if dbif: - dbif.execute(sql, mapset=mapset) - row = dbif.fetchone(mapset=mapset) + row = None + if mapset is not None: + # search only in the tgis db in the given mapset + if dbif: + dbif.execute(sql, mapset=mapset) + row = dbif.fetchone(mapset=mapset) + else: + dbif = SQLDatabaseInterfaceConnection() + dbif.connect() + dbif.execute(sql, mapset=mapset) + row = dbif.fetchone(mapset=mapset) + dbif.close() else: - dbif = SQLDatabaseInterfaceConnection() - dbif.connect() - dbif.execute(sql, mapset=mapset) - row = dbif.fetchone(mapset=mapset) - dbif.close() + # search all available datasets + tgis_mapsets = get_available_temporal_mapsets() + for mapset in tgis_mapsets: + if dbif: + dbif.execute(sql, mapset=mapset) + row = dbif.fetchone(mapset=mapset) + else: + dbif = SQLDatabaseInterfaceConnection() + dbif.connect() + dbif.execute(sql, mapset=mapset) + row = dbif.fetchone(mapset=mapset) + dbif.close() + dbif = None + if row is not None: + # set tgis mapset for this instance + self.tgis_mapset = mapset + break # Nothing found if row is None: @@ -544,10 +567,14 @@ def get_update_all_statement_mogrified(self, dbif=None, ident=None): :param ident: The identifier to be updated, useful for renaming :return: The UPDATE string """ + + # use the temporal database in the current mapset + mapset = get_current_mapset() + if not dbif: dbif = SQLDatabaseInterfaceConnection() - return dbif.mogrify_sql_statement(self.get_update_all_statement(ident)) + return dbif.mogrify_sql_statement(self.get_update_all_statement(ident), mapset) def update_all(self, dbif=None, ident=None): """Serialize the content of this object, including None objects, diff --git a/python/grass/temporal/open_stds.py b/python/grass/temporal/open_stds.py index c264a587661..c99cda270c8 100644 --- a/python/grass/temporal/open_stds.py +++ b/python/grass/temporal/open_stds.py @@ -49,6 +49,7 @@ def open_old_stds(name, type, dbif=None): mapset = get_current_mapset() else: name, mapset = name.split("@") + semantic_label = None if name.find(".") > -1: try: diff --git a/python/grass/temporal/register.py b/python/grass/temporal/register.py index 1efff686075..6e2b5f6c533 100644 --- a/python/grass/temporal/register.py +++ b/python/grass/temporal/register.py @@ -597,6 +597,7 @@ def register_map_object_list( import copy dbif, connection_state_changed = init_dbif(dbif) + mapset = get_current_mapset() filename = gscript.tempfile(True) file = open(filename, "w") @@ -651,7 +652,7 @@ def register_map_object_list( if map.get_type() == "vector": mod(type="vector", name=map.get_name()) mod.run() - if map.is_in_db(dbif): + if map.is_in_db(dbif, mapset): map.delete(dbif) if connection_state_changed: