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

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 20 14:28:20 EDT 2011


Author: huhabla
Date: 2011-10-20 11:28:19 -0700 (Thu, 20 Oct 2011)
New Revision: 48882

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:
Removed some performance issues


Modified: grass/trunk/lib/python/temporal/abstract_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_dataset.py	2011-10-20 12:00:07 UTC (rev 48881)
+++ grass/trunk/lib/python/temporal/abstract_dataset.py	2011-10-20 18:28:19 UTC (rev 48882)
@@ -103,6 +103,16 @@
         
     def select(self, dbif=None):
 	"""Select temporal dataset entry from database and fill up the internal structure"""
+
+        connect = False
+
+        if dbif == None:
+            dbif = sql_database_interface()
+            dbif.connect()
+            connect = True
+
+        dbif.cursor.execute("BEGIN TRANSACTION")
+
 	self.base.select(dbif)
 	if self.is_time_absolute():
 	    self.absolute_time.select(dbif)
@@ -110,6 +120,11 @@
 	    self.relative_time.select(dbif)
 	self.spatial_extent.select(dbif)
 	self.metadata.select(dbif)
+
+        dbif.cursor.execute("COMMIT TRANSACTION")
+
+        if connect:
+            dbif.close()
         
     def is_in_db(self, dbif=None):
 	"""Check if the temporal dataset entry is in the database"""
@@ -121,6 +136,17 @@
 
     def insert(self, dbif=None):
 	"""Insert temporal dataset entry into database from the internal structure"""
+
+        connect = False
+
+        if dbif == None:
+            dbif = sql_database_interface()
+            dbif.connect()
+            connect = True
+
+        dbif.cursor.execute("BEGIN TRANSACTION")
+
+
 	self.base.insert(dbif)
 	if self.is_time_absolute():
 	    self.absolute_time.insert(dbif)
@@ -129,10 +155,26 @@
 	self.spatial_extent.insert(dbif)
 	self.metadata.insert(dbif)
 
+        dbif.cursor.execute("COMMIT TRANSACTION")
+
+        if connect:
+            dbif.close()
+ 
     def update(self, dbif=None):
 	"""Update temporal dataset entry of database from the internal structure
 	   excluding None variables
 	"""
+
+        connect = False
+
+        if dbif == None:
+            dbif = sql_database_interface()
+            dbif.connect()
+            connect = True
+
+        dbif.cursor.execute("BEGIN TRANSACTION")
+
+
 	self.base.update(dbif)
 	if self.is_time_absolute():
 	    self.absolute_time.update(dbif)
@@ -141,12 +183,28 @@
 	self.spatial_extent.update(dbif)
 	self.metadata.update(dbif)
 
+        dbif.cursor.execute("COMMIT TRANSACTION")
+
+        if connect:
+            dbif.close()
+ 
     def update_all(self, dbif=None):
 	"""Update temporal dataset entry of database from the internal structure
 	   and include None varuables.
 
            @param dbif: The database interface to be used
 	"""
+
+        connect = False
+
+        if dbif == None:
+            dbif = sql_database_interface()
+            dbif.connect()
+            connect = True
+
+        dbif.cursor.execute("BEGIN TRANSACTION")
+
+
 	self.base.update_all(dbif)
 	if self.is_time_absolute():
 	    self.absolute_time.update_all(dbif)
@@ -155,6 +213,11 @@
 	self.spatial_extent.update_all(dbif)
 	self.metadata.update_all(dbif)
 
+        dbif.cursor.execute("COMMIT TRANSACTION")
+
+        if connect:
+            dbif.close()
+ 
     def print_self(self):
 	"""Print the content of the internal structure to stdout"""
 	self.base.print_self()

Modified: grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2011-10-20 12:00:07 UTC (rev 48881)
+++ grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2011-10-20 18:28:19 UTC (rev 48882)
@@ -87,6 +87,10 @@
         self.metadata.set_title(title)
         self.metadata.set_description(description)
 
+    def get_semantic_type(self):
+        """Return the semantic type of this dataset"""
+        return self.base.get_semantic_type()
+
     def get_initial_values(self):
         """Return the initial values: temporal_type, semantic_type, title, description"""
         
@@ -134,7 +138,6 @@
 
         return map_time
 
-
     def print_temporal_relation_matrix(self, maps):
         """Print the temporal relation matrix of all registered maps to stdout
 
@@ -342,9 +345,66 @@
 
         return True
 
+    def get_registered_maps_as_objects_with_gaps(self, where=None, dbif=None):
+        """Return all registered maps as ordered (by start_time) object list with 
+           "gap" map objects (id==None) for temporal topological operations
+
+           Gaps between maps are identified as maps with id==None
+
+           The objects are initialized with the id and the temporal extent (temporal type, start time, end time).
+           In case more map informations are needed, use the select() method for each listed object.
+
+           @param where: The SQL where statement to select a subset of the registered maps without "WHERE"
+           @param dbif: The database interface to be used
+
+           In case nothing found None is returned
+        """
+
+        connect = False
+
+        if dbif == None:
+            dbif = sql_database_interface()
+            dbif.connect()
+            connect = True
+
+        obj_list = []
+        
+        maps = self.get_registered_maps_as_objects(where, "start_time", dbif)
+
+        if maps and len(maps) > 0:
+            for i in range(len(maps)):
+                obj_list.append(maps[i])
+                # Detect and insert gaps
+                if i < len(maps) - 1:
+                    relation = maps[i + 1].temporal_relation(maps[i])
+                    if relation == "after":
+                        start1, end1 = maps[i].get_valid_time()
+                        start2, end2 = maps[i + 1].get_valid_time()
+                        end = start2
+                        if end1:
+                            start = end1
+                        else:
+                            start = start1
+
+                        map = self.get_new_map_instance(None)
+
+                        if self.is_time_absolute():
+                            map.set_absolute_time(start, end)
+                        elif self.is_time_relative():
+                            map.set_relative_time(start, end)
+                        obj_list.append(copy.copy(map))
+
+        if connect == True:
+            dbif.close()
+
+        return obj_list
+
     def get_registered_maps_as_objects(self, where=None, order="start_time", dbif=None):
-        """Return all registered maps as ordered object list
+        """Return all registered maps as ordered object list for temporal topological operations
 
+           The objects are initialized with the id and the temporal extent (temporal type, start time, end time).
+           In case more map informations are needed, use the select() method for each listed object.
+
            @param where: The SQL where statement to select a subset of the registered maps without "WHERE"
            @param order: The SQL order statement to be used to order the objects in the list without "ORDER BY"
            @param dbif: The database interface to be used
@@ -361,14 +421,22 @@
 
         obj_list = []
         
-        rows = self.get_registered_maps("id", where, order, dbif)
+        rows = self.get_registered_maps("id,start_time,end_time", where, order, dbif)
 
+        count = 0
         if rows:
             for row in rows:
+                core.percent(count, len(rows), 1)
                 map = self.get_new_map_instance(row["id"])
-                map.select(dbif)
+                if self.is_time_absolute():
+                    map.set_absolute_time(row["start_time"], row["end_time"])
+                elif self.is_time_relative():
+                    map.set_relative_time(row["start_time"], row["end_time"])
                 obj_list.append(copy.copy(map))
+                count += 1
 
+        core.percent(1, 1, 1)
+
         if connect == True:
             dbif.close()
 
@@ -377,7 +445,7 @@
     def get_registered_maps(self, columns=None, where = None, order = None, dbif=None):
         """Return sqlite rows of all registered maps.
         
-           Each row includes all columns specified in the datatype specific view
+           In case columsn are not specified, each row includes all columns specified in the datatype specific view
 
            @param columns: Columns to be selected as SQL compliant string
            @param where: The SQL where statement to select a subset of the registered maps without "WHERE"

Modified: grass/trunk/lib/python/temporal/base.py
===================================================================
--- grass/trunk/lib/python/temporal/base.py	2011-10-20 12:00:07 UTC (rev 48881)
+++ grass/trunk/lib/python/temporal/base.py	2011-10-20 18:28:19 UTC (rev 48882)
@@ -188,20 +188,15 @@
        * Temporal extent
        * Metadata
     """
-    def __init__(self, table=None, ident=None, database=None):
+    def __init__(self, table=None, ident=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 database: A specific string used in the dbmi connect method. This should be the path to the database , user name, ...
         """
         dict_sql_serializer.__init__(self)
 
         self.table = table # Name of the table, set in the subclass
-        if database == None:
-            self.database = get_temporal_dbmi_init_string()
-        else:
-            self.database = database
         self.ident = ident
 
     def get_table_name(self):
@@ -213,16 +208,17 @@
 
            Supported backends are sqlite3 and postgresql
         """
+        init = get_temporal_dbmi_init_string()
         #print "Connect to",  self.database
         if dbmi.__name__ == "sqlite3":
-	    self.connection = dbmi.connect(self.database, detect_types=dbmi.PARSE_DECLTYPES|dbmi.PARSE_COLNAMES)
+	    self.connection = dbmi.connect(init, detect_types=dbmi.PARSE_DECLTYPES|dbmi.PARSE_COLNAMES)
 	    self.connection.row_factory = dbmi.Row
             self.connection.isolation_level = None
 	    self.cursor = self.connection.cursor()
             self.cursor.execute("PRAGMA synchronous = OFF")
             self.cursor.execute("PRAGMA journal_mode = MEMORY")
         elif dbmi.__name__ == "psycopg2":
-	    self.connection = dbmi.connect(self.database)
+	    self.connection = dbmi.connect(init)
 	    self.connection.set_isolation_level(dbmi.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
 	    self.cursor = self.connection.cursor(cursor_factory=dbmi.extras.DictCursor)
 
@@ -391,6 +387,9 @@
 	sql_database_interface.__init__(self, table, ident)
 
 	self.set_id(ident)
+        if ident != None and name == None and mapset == None:
+            if ident.find("@") >= 0:
+                name, mapset = ident.split("@")
 	self.set_name(name)
 	self.set_mapset(mapset)
 	self.set_creator(creator)

Modified: grass/trunk/lib/python/temporal/space_time_datasets_tools.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2011-10-20 12:00:07 UTC (rev 48881)
+++ grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2011-10-20 18:28:19 UTC (rev 48882)
@@ -524,7 +524,6 @@
        @param type: the dataset type: rast, rast3d, vect, strds, str3ds, stvds
        @param id: The id of the dataset ("name at mapset")
     """
-    print type, id
     if type == "strds":
         sp = space_time_raster_dataset(id)
     elif type == "str3ds":



More information about the grass-commit mailing list