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

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 1 17:54:34 PDT 2012


Author: huhabla
Date: 2012-11-01 17:54:33 -0700 (Thu, 01 Nov 2012)
New Revision: 53642

Modified:
   grass/trunk/lib/python/temporal/abstract_dataset.py
   grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
   grass/trunk/lib/python/temporal/base.py
   grass/trunk/lib/python/temporal/space_time_datasets_tools.py
Log:
Implemented renaming of existing space time datasets


Modified: grass/trunk/lib/python/temporal/abstract_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_dataset.py	2012-11-01 18:29:13 UTC (rev 53641)
+++ grass/trunk/lib/python/temporal/abstract_dataset.py	2012-11-02 00:54:33 UTC (rev 53642)
@@ -244,7 +244,7 @@
             dbif.close()
         return statement
 
-    def update(self, dbif=None, execute=True):
+    def update(self, dbif=None, execute=True, ident=None):
         """!Update temporal dataset entry of database from the internal structure
            excluding None variables
 
@@ -252,20 +252,22 @@
            @param execute: If True the SQL statements will be executed.
                            If False the prepared SQL statements are returned 
                            and must be executed by the caller.
+           @param ident: The identifier to be updated, useful for renaming
         """
 
         dbif, connect = init_dbif(dbif)
 
         # Build the UPDATE SQL statement
-        statement = self.base.get_update_statement_mogrified(dbif)
+        statement = self.base.get_update_statement_mogrified(dbif, ident)
         if self.is_time_absolute():
             statement += self.absolute_time.get_update_statement_mogrified(
-                dbif)
+                dbif, ident)
         if self.is_time_relative():
             statement += self.relative_time.get_update_statement_mogrified(
-                dbif)
-        statement += self.spatial_extent.get_update_statement_mogrified(dbif)
-        statement += self.metadata.get_update_statement_mogrified(dbif)
+                dbif, ident)
+        statement += self.spatial_extent.get_update_statement_mogrified(dbif, 
+                                                                        ident)
+        statement += self.metadata.get_update_statement_mogrified(dbif, ident)
 
         if execute:
             dbif.execute_transaction(statement)
@@ -277,7 +279,7 @@
             dbif.close()
         return statement
 
-    def update_all(self, dbif=None, execute=True):
+    def update_all(self, dbif=None, execute=True, ident=None):
         """!Update temporal dataset entry of database from the internal structure
            and include None variables.
 
@@ -285,21 +287,22 @@
            @param execute: If True the SQL statements will be executed.
                            If False the prepared SQL statements are returned 
                            and must be executed by the caller.
+           @param ident: The identifier to be updated, useful for renaming
         """
 
         dbif, connect = init_dbif(dbif)
 
         # Build the UPDATE SQL statement
-        statement = self.base.get_update_all_statement_mogrified(dbif)
+        statement = self.base.get_update_all_statement_mogrified(dbif, ident)
         if self.is_time_absolute():
             statement += self.absolute_time.get_update_all_statement_mogrified(
-                dbif)
+                dbif, ident)
         if self.is_time_relative():
             statement += self.relative_time.get_update_all_statement_mogrified(
-                dbif)
+                dbif, ident)
         statement += self.spatial_extent.get_update_all_statement_mogrified(
-            dbif)
-        statement += self.metadata.get_update_all_statement_mogrified(dbif)
+            dbif, ident)
+        statement += self.metadata.get_update_all_statement_mogrified(dbif, ident)
 
         if execute:
             dbif.execute_transaction(statement)

Modified: grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2012-11-01 18:29:13 UTC (rev 53641)
+++ grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2012-11-02 00:54:33 UTC (rev 53642)
@@ -45,6 +45,19 @@
         raise ImplementationError(
             "This method must be implemented in the subclasses")
 
+    def create_map_register_name(self):
+        """!Create the name of the map register table of this space time dataset
+        
+            The name, mapset and the map type are used to create the table name.
+            
+            ATTENTION: It must be assured that the base object has selected its 
+            content from the database.
+        """
+        
+        return self.base.get_name() + "_" + \
+                self.base.get_mapset() + "_" + \
+                self.get_new_map_instance(None).get_type() + "_register"
+
     def get_map_register(self):
         """!Return the name of the map register table"""
         raise ImplementationError(
@@ -610,9 +623,6 @@
            A valid temporal topology (no overlapping or inclusion allowed) 
            is needed to get correct results.
 
-           The dataset must have "interval" as temporal map type, 
-           so all maps have valid interval time.
-
            Gaps between maps are identified as unregistered maps with id==None.
 
            The objects are initialized with the id and the temporal 
@@ -629,12 +639,6 @@
         dbif, connect = init_dbif(dbif)
 
         obj_list = []
-        
-        if self.get_map_time() == "point" or self.get_map_time() == "mixed":
-            core.error(_("The space time %(type)s dataset <%(name)s> must have"
-                         " interval time"%\
-                         {"type":self.get_new_map_instance(None).get_type(),
-                          "name":self.get_id()}))
 
         if gran is None:
             gran = self.get_granularity()
@@ -651,6 +655,8 @@
 
             rows = self.get_registered_maps("id", where, "start_time", dbif)
 
+            found_gap = False
+            
             if rows is not None and len(rows) != 0:
                 if len(rows) > 1:
                     core.warning(_("More than one map found in a granule. "
@@ -673,8 +679,39 @@
                     maplist.append(copy.copy(map))
 
                 obj_list.append(copy.copy(maplist))
-	    else:
-		# Found a gap
+            else:    
+                # Searching for time instances
+                where = "(start_time = '%s')" % (start)
+    
+                rows = self.get_registered_maps("id", where, "start_time", dbif)
+    
+                if rows is not None and len(rows) != 0:
+                    if len(rows) > 1:
+                        core.warning(_("More than one map found in a granule. "
+                                       "Temporal granularity seems to be invalid or"
+                                       " the chosen granularity is not a greatest "
+                                       "common divider of all time instances "
+                                       "in the dataset."))
+    
+                    maplist = []
+                    for row in rows:
+                        # Take the first map
+                        map = self.get_new_map_instance(rows[0]["id"])
+    
+                        if self.is_time_absolute():
+                            map.set_absolute_time(start, None)
+                        elif self.is_time_relative():
+                            map.set_relative_time(start, None, 
+                                                  self.get_relative_time_unit())
+    
+                        maplist.append(copy.copy(map))
+    
+                    obj_list.append(copy.copy(maplist))
+                found_gap = True
+            
+            # Gap handling
+            if found_gap:
+                # Found a gap
                 map = self.get_new_map_instance(None)
 
                 if self.is_time_absolute():
@@ -853,6 +890,57 @@
 
         return rows
 
+    def rename(self, ident, dbif=None):
+        """!Rename the space time dataset
+
+           This method renames the space time dataset, the map register table
+           and updates the entries in registered maps stds register.
+
+           @param ident: The new identifier name at mapset
+           @param dbif: The database interface to be used
+        """
+
+        dbif, connect = init_dbif(dbif)
+
+        # SELECT all needed information from the database
+        self.select(dbif)
+        
+        # We need to select the registered maps here
+        maps = self.get_registered_maps_as_objects(None, "start_time", dbif)
+        
+        # Safe old identifier
+        old_ident = self.get_id()
+        # We need to rename the old table
+        old_map_register_table = self.get_map_register()
+        
+        # Set new identifier
+        self.set_id(ident)
+        # Create map register table name from new identifier
+        new_map_register_table = self.create_map_register_name()
+        # Set new map register table name
+        self.set_map_register(new_map_register_table)
+        
+        # Get the update statement, we update the table entry of the old identifier
+        statement = self.update(dbif, execute=False, ident=old_ident)
+        
+        # We need to rename the raster register table
+        statement += "ALTER TABLE %s RENAME TO \'%s\';\n"%\
+                     (old_map_register_table, new_map_register_table)
+
+        # We need to rename the space time dataset in the maps register table
+        if maps:
+            for map in maps:
+                map.select()
+                statement += "UPDATE %s SET id = \'%s\' WHERE id = \'%s\';\n"%\
+                             (map.get_stds_register(), ident, old_ident)
+                         
+        
+        # Execute the accumulated statements
+        dbif.execute_transaction(statement)
+        
+        if connect:
+            dbif.close()
+    
     def delete(self, dbif=None, execute=True):
         """!Delete a space time dataset from the temporal database
 
@@ -926,6 +1014,7 @@
             In case the map is already registered this function 
             will break with a warning and return False.
 
+           @param map: The AbstractMapDataset object that should be registered
            @param dbif: The database interface to be used
         """
         dbif, connect = init_dbif(dbif)
@@ -1087,8 +1176,7 @@
         # We need to create the table and register it
         if stds_register_table is None:
             # Create table name
-            stds_register_table = stds_name + "_" + \
-                stds_mapset + "_" + map.get_type() + "_register"
+            stds_register_table = self.create_map_register_name()
             # Read the SQL template
             sql = open(os.path.join(sql_path, 
                                     "stds_map_register_table_template.sql"), 

Modified: grass/trunk/lib/python/temporal/base.py
===================================================================
--- grass/trunk/lib/python/temporal/base.py	2012-11-01 18:29:13 UTC (rev 53641)
+++ grass/trunk/lib/python/temporal/base.py	2012-11-02 00:54:33 UTC (rev 53642)
@@ -401,24 +401,33 @@
             dbif.cursor.execute(sql, args)
             dbif.close()
 
-    def get_update_statement(self):
+    def get_update_statement(self, ident=None):
         """!Return the sql statement and the argument list 
-           in database specific style"""
-        return self.serialize("UPDATE", self.get_table_name(), 
+           in database specific style
+           
+           @param ident: The identifier to be updated, useful for renaming
+           
+           """
+        if ident:
+            return self.serialize("UPDATE", self.get_table_name(), 
+                              "WHERE id = \'" + str(ident) + "\'")
+        else:
+            return self.serialize("UPDATE", self.get_table_name(), 
                               "WHERE id = \'" + str(self.ident) + "\'")
 
-    def get_update_statement_mogrified(self, dbif=None):
+    def get_update_statement_mogrified(self, dbif=None, ident=None):
         """!Return the update statement as mogrified string
 
            @param dbif: The database interface to be used, 
                         if None a temporary connection will be established
+           @param ident: The identifier to be updated, useful for renaming
         """
         if not dbif:
             dbif = SQLDatabaseInterfaceConnection()
 
-        return dbif.mogrify_sql_statement(self.get_update_statement())
+        return dbif.mogrify_sql_statement(self.get_update_statement(ident))
 
-    def update(self, dbif=None):
+    def update(self, dbif=None, ident=None):
         """!Serialize the content of this object and update it in the temporal
            database using the internal identifier
 
@@ -426,11 +435,12 @@
 
            @param dbif: The database interface to be used, 
                         if None a temporary connection will be established
+           @param ident: The identifier to be updated, useful for renaming
         """
         if self.ident is None:
             raise IOError("Missing identifer")
 
-        sql, args = self.get_update_statement()
+        sql, args = self.get_update_statement(indent)
         #print sql
         #print args
 
@@ -442,34 +452,43 @@
             dbif.cursor.execute(sql, args)
             dbif.close()
 
-    def get_update_all_statement(self):
+    def get_update_all_statement(self, ident=None):
         """!Return the sql statement and the argument 
-           list in database specific style"""
-        return self.serialize("UPDATE ALL", self.get_table_name(), 
+           list in database specific style
+           
+           @param ident: The identifier to be updated, useful for renaming
+           """
+        if ident:
+            return self.serialize("UPDATE ALL", self.get_table_name(), 
+                              "WHERE id = \'" + str(ident) + "\'")
+        else:
+            return self.serialize("UPDATE ALL", self.get_table_name(), 
                               "WHERE id = \'" + str(self.ident) + "\'")
 
-    def get_update_all_statement_mogrified(self, dbif=None):
+    def get_update_all_statement_mogrified(self, dbif=None, ident=None):
         """!Return the update all statement as mogrified string
 
            @param dbif: The database interface to be used, 
                         if None a temporary connection will be established
+           @param ident: The identifier to be updated, useful for renaming
         """
         if not dbif:
             dbif = SQLDatabaseInterfaceConnection()
 
-        return dbif.mogrify_sql_statement(self.get_update_all_statement())
+        return dbif.mogrify_sql_statement(self.get_update_all_statement(ident))
 
-    def update_all(self, dbif=None):
+    def update_all(self, dbif=None, ident=None):
         """!Serialize the content of this object, including None objects, 
         and update it in the temporal database using the internal identifier
 
            @param dbif: The database interface to be used, 
                         if None a temporary connection will be established
+           @param ident: The identifier to be updated, useful for renaming
         """
         if self.ident is None:
             raise IOError("Missing identifer")
 
-        sql, args = self.get_update_all_statement()
+        sql, args = self.get_update_all_statement(ident)
         #print sql
         #print args
 

Modified: grass/trunk/lib/python/temporal/space_time_datasets_tools.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2012-11-01 18:29:13 UTC (rev 53641)
+++ grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2012-11-02 00:54:33 UTC (rev 53642)
@@ -230,13 +230,17 @@
                                    "Unable to update %s map <%s>. "
                                    "Overwrite flag is not set.") %
                                (map.get_type(), map.get_map_id()))
+                
+                # Simple registration is allowed
+                if name:
+                    map_object_list.append(map)
                 # Jump to next map
                 continue
             
             # Select information from temporal database
             map.select(dbif)
             
-            # Safe the datasets that must be updated
+            # Save the datasets that must be updated
             datasets = map.get_registered_datasets(dbif)
             if datasets:
                 for dataset in datasets:
@@ -293,7 +297,7 @@
         dbif.execute_transaction(statement)
 
     # Finally Register the maps in the space time dataset
-    if name:
+    if name and map_object_list:
         statement = ""
         count = 0
         num_maps = len(map_object_list)
@@ -304,7 +308,7 @@
             count += 1
 
     # Update the space time tables
-    if name:
+    if name and map_object_list:
         core.message(_("Update space time raster dataset"))
         sp.update_from_registered_maps(dbif)
         
@@ -353,7 +357,6 @@
     if ttype == "absolute":
         start_time = string_to_datetime(start)
         if start_time is None:
-            dbif.close()
             core.fatal(_("Unable to convert string \"%s\"into a "
                          "datetime object") % (start))
         end_time = None



More information about the grass-commit mailing list