[GRASS-SVN] r58020 - grass/trunk/lib/python/temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 16 16:30:12 PDT 2013


Author: huhabla
Date: 2013-10-16 16:30:12 -0700 (Wed, 16 Oct 2013)
New Revision: 58020

Modified:
   grass/trunk/lib/python/temporal/abstract_dataset.py
   grass/trunk/lib/python/temporal/abstract_map_dataset.py
   grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
   grass/trunk/lib/python/temporal/core.py
   grass/trunk/lib/python/temporal/extract.py
   grass/trunk/lib/python/temporal/mapcalc.py
   grass/trunk/lib/python/temporal/open.py
   grass/trunk/lib/python/temporal/register.py
   grass/trunk/lib/python/temporal/sampling.py
   grass/trunk/lib/python/temporal/space_time_datasets.py
   grass/trunk/lib/python/temporal/stds_import.py
   grass/trunk/lib/python/temporal/univar_statistics.py
Log:
Implemented a check for the correct mapset when datasets are modified.


Modified: grass/trunk/lib/python/temporal/abstract_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_dataset.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/abstract_dataset.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -378,6 +378,11 @@
             @return The SQL insert statement in case execute=False, or an empty string otherwise
         """
 
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to insert dataset <%(ds)s> of type %(type)s in the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+
         dbif, connected = init_dbif(dbif)
 
         # Build the INSERT SQL statement
@@ -408,6 +413,12 @@
            @return The SQL update statement in case execute=False, or an empty string otherwise
         """
 
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to update dataset <%(ds)s> of type %(type)s in the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                                 {"ds":self.get_id(), "type":self.get_type()})
+
+
         dbif, connected = init_dbif(dbif)
 
         # Build the UPDATE SQL statement
@@ -440,6 +451,11 @@
            @return The SQL update statement in case execute=False, or an empty string otherwise
         """
 
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to update dataset <%(ds)s> of type %(type)s in the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+
         dbif, connected = init_dbif(dbif)
 
         # Build the UPDATE SQL statement

Modified: grass/trunk/lib/python/temporal/abstract_map_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_map_dataset.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/abstract_map_dataset.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -410,7 +410,13 @@
                   map, None in case or time instance
            @param timezone Thee timezone of the map (not used)
            @param dbif The database interface to be used
-        """
+           """
+           
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to update dataset <%(ds)s> of type %(type)s in the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+            
         dbif, connected = init_dbif(dbif)
 
         self.set_absolute_time(start_time, end_time, timezone)
@@ -439,7 +445,6 @@
            @return True for success and False otherwise
 
         """
-
         if not self.check_relative_time_unit(unit):
             if self.get_layer() is not None:
                 core.error(_("Unsupported relative time unit type for %(type)s"
@@ -497,6 +502,11 @@
            @param unit The relative time unit
            @param dbif The database interface to be used
         """
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to update dataset <%(ds)s> of type %(type)s in the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+
         dbif, connected = init_dbif(dbif)
 
         if self.set_relative_time(start_time, end_time, unit):
@@ -799,7 +809,11 @@
            @return The SQL statements if execute=False, else an empty string,
                    None in case of a failure
         """
-
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to delete dataset <%(ds)s> of type %(type)s from the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+            
         dbif, connected = init_dbif(dbif)
         statement = ""
 
@@ -814,7 +828,7 @@
 
             # Remove the strds register table
             if self.get_stds_register() is not None:
-                statement += "DROP TABLE " + self.get_stds_register() + ";\n"
+                statement += "DROP TABLE IF EXISTS " + self.get_stds_register() + ";\n"
 
             # Commented because of performance issue calling g.message thousend times
             #core.verbose(_("Delete %s dataset <%s> from temporal database")
@@ -866,7 +880,12 @@
         #    core.verbose(_("Unregister %(type)s map <%(map)s> "
         #                   "from space time datasets"
         #                 % {'type':self.get_type(), 'map':self.get_map_id()}))
-
+        
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to unregister dataset <%(ds)s> of type %(type)s from the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+            
         statement = ""
         dbif, connected = init_dbif(dbif)
 

Modified: grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -1467,7 +1467,7 @@
         """
         if maps is None:
             return None
-
+ 
         if not check_granularity_string(gran, maps[-1].get_temporal_type()):
             core.error(_("Wrong granularity format: %s" % (gran)))
             return None
@@ -1496,6 +1496,11 @@
                    granularity
 
         """
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to shift dataset <%(ds)s> of type %(type)s in the temporal database."
+            " The mapset of the dataset does not match the current mapset")%\
+            ({"ds":self.get_id()}, {"type":self.get_type()}))
+
         if not check_granularity_string(gran, self.get_temporal_type()):
             core.error(_("Wrong granularity format: %s" % (gran)))
             return False
@@ -1657,6 +1662,12 @@
            @param dbif The database interface to be used
 
         """
+
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to snap dataset <%(ds)s> of type %(type)s in the temporal database."
+            " The mapset of the dataset does not match the current mapset")%\
+            ({"ds":self.get_id()}, {"type":self.get_type()}))
+
         dbif, connected = init_dbif(dbif)
 
         maps = self.get_registered_maps_as_objects(dbif=dbif)
@@ -1751,7 +1762,12 @@
            @param ident The new identifier "name at mapset"
            @param dbif The database interface to be used
         """
-
+        
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to rename dataset <%(ds)s> of type %(type)s in the temporal database."
+            " The mapset of the dataset does not match the current mapset")%\
+            ({"ds":self.get_id()}, {"type":self.get_type()}))
+            
         dbif, connected = init_dbif(dbif)
 
         # SELECT all needed information from the database
@@ -1816,6 +1832,11 @@
         #             (self.get_new_map_instance(ident=None).get_type(),
         #              self.get_id()))
 
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to delete dataset <%(ds)s> of type %(type)s from the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+
         statement = ""
         dbif, connected = init_dbif(dbif)
 
@@ -1835,7 +1856,7 @@
                         map=map, dbif=dbif, execute=False)
 
             # Safe the DROP table statement
-            statement += "DROP TABLE " + self.get_map_register() + ";\n"
+            statement += "DROP TABLE IF EXISTS " + self.get_map_register() + ";\n"
 
         # Remove the primary key, the foreign keys will be removed by trigger
         statement += self.base.get_delete_statement()
@@ -1868,6 +1889,12 @@
            @param dbif The database interface to be used
            @return True if success, False otherwise
         """
+
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to register map in dataset <%(ds)s> of type %(type)s."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+
         dbif, connected = init_dbif(dbif)
 
         if map.is_in_db(dbif) == False:
@@ -2033,6 +2060,9 @@
         if stds_register_table is None:
             # Create table name
             stds_register_table = self.create_map_register_name()
+            # Assure that the table and index do not exist
+            dbif.execute_transaction("DROP INDEX IF EXISTS %s; DROP TABLE IF EXISTS  %s;"%(stds_register_table + "_index", stds_register_table))
+
             # Read the SQL template
             sql = open(os.path.join(sql_path,
                                     "stds_map_register_table_template.sql"),
@@ -2121,6 +2151,11 @@
                    string, None in case of a failure
         """
 
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to unregister map from dataset <%(ds)s> of type %(type)s in the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+
         statement = ""
 
         dbif, connected = init_dbif(dbif)
@@ -2221,6 +2256,12 @@
 
            @param dbif The database interface to be used
         """
+
+        if self.get_mapset() != get_current_mapset():
+            core.fatal(_("Unable to update dataset <%(ds)s> of type %(type)s in the temporal database."
+                         " The mapset of the dataset does not match the current mapset")%\
+                         {"ds":self.get_id(), "type":self.get_type()})
+
         core.verbose(_("Update metadata, spatial and temporal extent from "
                        "all registered maps of <%s>") % (self.get_id()))
 

Modified: grass/trunk/lib/python/temporal/core.py
===================================================================
--- grass/trunk/lib/python/temporal/core.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/core.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -50,6 +50,9 @@
 except:
     pass
 
+# Uncomment this to raise and exception in case of a fatal error
+# core.set_raise_on_error(True)
+
 # Global variable that defines the backend
 # of the temporal GIS
 # It can either be "sqlite" or "pg"
@@ -65,8 +68,32 @@
 # temporal database SQL layout
 tgis_db_version=1
 
+# We need to access the current mapset quite often in the framework, so we make
+# global variable that will be initiated when init() is called
+current_mapset=None
+
 ###############################################################################
 
+def get_current_mapset():
+    """!Return the current mapset
+
+       This is the fastest way to receive the current mapset.
+       The current mapset is set by init() and stored in a global variable.
+       This function provides access to this global variable.
+    """
+    global current_mapset
+    return current_mapset
+
+def _set_current_mapset():
+    """!This functions set the global current mapset variable to the current mapset
+    by calling g.gisenv. 
+    """
+    global current_mapset
+    current_mapset = core.gisenv()["MAPSET"]
+
+
+###############################################################################
+
 def get_tgis_version():
     """!Get the verion number of the temporal framework
        @return The version number of the temporal framework as string
@@ -178,6 +205,9 @@
     # We need to set the correct database backend from the environment variables
     global tgis_backend
 
+    # Set the global variable current_mapset for fast mapset access 
+    _set_current_mapset()
+
     core.run_command("t.connect", flags="c")
     kv = core.parse_command("t.connect", flags="pg")
 
@@ -541,6 +571,37 @@
 
                 return statement
 
+    def check_table(self, table_name):
+        """!Check if a table exists in the temporal database
+        
+           @param table_name The name of the table to be checked for existance
+           @return True if the table exists, False otherwise
+        """
+        table_exists = False
+        connected = False
+        if not self.connected:
+            self.connect()
+            connected = True
+
+        # Check if the database already exists
+        if self.dbmi.__name__ == "sqlite3":
+
+            self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='%s';"%table_name)
+            name = self.cursor.fetchone()
+            if name and name[0] == table_name:
+                table_exists = True
+        else:
+            # Check for raster_base table
+            self.cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables "
+                    "WHERE table_name=%s)", ('%s'%table_name,))
+            if self.cursor.fetchone()[0]:
+                table_exists = True
+
+        if connected:
+            self.close()
+
+        return table_exists
+        
     def execute_transaction(self, statement):
         """!Execute a transactional SQL statement
 

Modified: grass/trunk/lib/python/temporal/extract.py
===================================================================
--- grass/trunk/lib/python/temporal/extract.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/extract.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -52,7 +52,7 @@
     if expression and not base:
         core.fatal(_("You need to specify the base name of new created maps"))
 
-    mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     dbif = SQLDatabaseInterfaceConnection()
     dbif.connect()

Modified: grass/trunk/lib/python/temporal/mapcalc.py
===================================================================
--- grass/trunk/lib/python/temporal/mapcalc.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/mapcalc.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -79,7 +79,7 @@
     dbif = SQLDatabaseInterfaceConnection()
     dbif.connect()
 
-    mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     input_name_list = inputs.split(",")
 

Modified: grass/trunk/lib/python/temporal/open.py
===================================================================
--- grass/trunk/lib/python/temporal/open.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/open.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -42,7 +42,7 @@
        @param dbif The optional database interface to be used
 
     """
-    mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     # Check if the dataset name contains the mapset as well
     if name.find("@") < 0:
@@ -93,7 +93,7 @@
 
     #Get the current mapset to create the id of the space time dataset
 
-    mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     if name.find("@") < 0:
         id = name + "@" + mapset
@@ -175,16 +175,13 @@
 
 ############################################################################
 
-def check_new_map_dataset(name, layer=None, mapset=None,
-                          type="raster", overwrite=False, dbif=None):
+def check_new_map_dataset(name, layer=None, type="raster", 
+                          overwrite=False, dbif=None):
     """!Check if a new map dataset of a specific type can be created in
         the temporal database
 
        @param name The name of the new map dataset
        @param layer The layer of the new map dataset
-       @param mapset The current mapset the new map dataset is created in,
-                     this argument is optional, if not provided g.gisenv
-                     will be called to reveive the current mapset
        @param type The type of the new map dataset (raster, vector, raster3d)
        @param dbif The temporal database interface to be used
        @param overwrite Flag to allow overwriting
@@ -193,8 +190,7 @@
 
        This function will raise a ScriptError in case of an error.
     """
-    if not mapset:
-        mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     dbif, connected = init_dbif(dbif)
     map_id = AbstractMapDataset.build_id(name, mapset, layer)
@@ -215,7 +211,7 @@
 
 ############################################################################
 
-def open_new_map_dataset(name, layer=None, mapset=None, type="raster",
+def open_new_map_dataset(name, layer=None, type="raster",
                          temporal_extent=None, overwrite=False,
                          dbif=None):
     """!Create a new map dataset object of a specific type that can be
@@ -223,9 +219,6 @@
 
        @param name The name of the new map dataset
        @param layer The layer of the new map dataset
-       @param mapset The current mapset the new map dataset is created in,
-                     this argument is optional, if not provided g.gisenv
-                     will be called to reveive the current mapset
        @param type The type of the new map dataset (raster, vector, raster3d)
        @param dbif The temporal database interface to be used
        @param overwrite Flag to allow overwriting
@@ -235,12 +228,10 @@
        This function will raise a ScriptError in case of an error.
     """
 
-    if not mapset:
-        mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     dbif, connected = init_dbif(dbif)
-    new_map = check_new_map_dataset(name, layer, mapset, "raster",
-                                    overwrite, dbif)
+    new_map = check_new_map_dataset(name, layer, "raster", overwrite, dbif)
 
     # Check if new map is in the temporal database
     if new_map.is_in_db(dbif):

Modified: grass/trunk/lib/python/temporal/register.py
===================================================================
--- grass/trunk/lib/python/temporal/register.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/register.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -83,7 +83,7 @@
                                                            'f': "file"})
 
     # We may need the mapset
-    mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
     dbif, connected = init_dbif(None)
 
     # The name of the space time dataset is optional

Modified: grass/trunk/lib/python/temporal/sampling.py
===================================================================
--- grass/trunk/lib/python/temporal/sampling.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/sampling.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -56,7 +56,7 @@
 
         @return The map matrix or None if nothing found
     """
-    mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     # Make a method list
     method = method.split(",")

Modified: grass/trunk/lib/python/temporal/space_time_datasets.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/space_time_datasets.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -52,7 +52,7 @@
         >>> grass.run_command("r.mapcalc", overwrite=True,
         ... expression="strds_map_test_case = 1")
         0
-        >>> mapset = grass.gisenv()["MAPSET"]
+        >>> mapset = get_current_mapset()
         >>> name = "strds_map_test_case"
         >>> identifier = "%s@%s" % (name, mapset)
         >>> rmap = RasterDataset(identifier)

Modified: grass/trunk/lib/python/temporal/stds_import.py
===================================================================
--- grass/trunk/lib/python/temporal/stds_import.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/stds_import.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -269,7 +269,7 @@
 
         fs = "|"
         maplist = []
-        mapset = core.gisenv()["MAPSET"]
+        mapset = get_current_mapset()
         list_file = open(list_file_name, "r")
         new_list_file = open(new_list_file_name, "w")
 
@@ -284,7 +284,7 @@
 
             # The filename is actually the base name of the map
             # that must be extended by the file suffix
-            filename = line_list[0].strip()
+            filename = line_list[0].strip().split(":")[0]
             if base:
                 mapname = "%s_%i"%(base, line_count)
                 mapid= "%s@%s"%(mapname, mapset)

Modified: grass/trunk/lib/python/temporal/univar_statistics.py
===================================================================
--- grass/trunk/lib/python/temporal/univar_statistics.py	2013-10-16 19:10:48 UTC (rev 58019)
+++ grass/trunk/lib/python/temporal/univar_statistics.py	2013-10-16 23:30:12 UTC (rev 58020)
@@ -46,7 +46,7 @@
     dbif = SQLDatabaseInterfaceConnection()
     dbif.connect()
 
-    sp = open_old_space_time_dataset(input, "strds", dbif)
+    sp = open_old_space_time_dataset(input, type, dbif)
 
     rows = sp.get_registered_maps(
         "id,start_time,end_time", where, "start_time", dbif)
@@ -123,7 +123,7 @@
     dbif = SQLDatabaseInterfaceConnection()
     dbif.connect()
 
-    mapset = core.gisenv()["MAPSET"]
+    mapset = get_current_mapset()
 
     if input.find("@") >= 0:
         id = input



More information about the grass-commit mailing list