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

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Aug 16 06:46:08 PDT 2013


Author: huhabla
Date: 2013-08-16 06:46:07 -0700 (Fri, 16 Aug 2013)
New Revision: 57457

Modified:
   grass/trunk/lib/python/temporal/abstract_map_dataset.py
   grass/trunk/lib/python/temporal/core.py
Log:
More temporal extent setting tests

Modified: grass/trunk/lib/python/temporal/abstract_map_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_map_dataset.py	2013-08-16 13:34:57 UTC (rev 57456)
+++ grass/trunk/lib/python/temporal/abstract_map_dataset.py	2013-08-16 13:46:07 UTC (rev 57457)
@@ -507,10 +507,10 @@
 
         self.write_timestamp_to_grass()
 
-    def set_temporal_extent(self, temporal_extent):
+    def set_temporal_extent(self, extent):
         """!Convenient method to set the temporal extent from a temporal extent object
 
-           @param temporal_extent The temporal axtent that should be set for this object
+           @param temporal_extent The temporal extent that should be set for this object
 
            @code
            >>> import datetime
@@ -527,21 +527,31 @@
            >>> print map.get_temporal_extent_as_tuple()
            (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2001, 1, 1, 0, 0))
 
+           >>> map1 = tgis.VectorDataset("A at P")
+           >>> check = map1.set_absolute_time(datetime.datetime(2000,5,5), datetime.datetime(2005,6,6), None)
+           >>> print map1.get_temporal_extent_as_tuple()
+           (datetime.datetime(2000, 5, 5, 0, 0), datetime.datetime(2005, 6, 6, 0, 0))
+           >>> map2 = tgis.RasterDataset("B at P")
+           >>> check = map2.set_absolute_time(datetime.datetime(1990,1,1), datetime.datetime(1999,8,1), None)
+           >>> print map2.get_temporal_extent_as_tuple()
+           (datetime.datetime(1990, 1, 1, 0, 0), datetime.datetime(1999, 8, 1, 0, 0))
+           >>> map2.set_temporal_extent(map1.get_temporal_extent())
+           >>> print map2.get_temporal_extent_as_tuple()
+           (datetime.datetime(2000, 5, 5, 0, 0), datetime.datetime(2005, 6, 6, 0, 0))
+
            @endcode
         """
+        if issubclass(type(extent), RelativeTemporalExtent):
+            start = extent.get_start_time()
+            end = extent.get_end_time()
+            unit = extent.get_unit()
 
-
-        if issubclass(type(temporal_extent), RelativeTemporalExtent):
-            start = temporal_extent.get_start_time()
-            end = temporal_extent.get_end_time()
-            unit = temporal_extent.get_unit()
-
             self.set_relative_time(start, end, unit)
 
-        elif issubclass(type(temporal_extent), AbsoluteTemporalExtent):
-            start = temporal_extent.get_start_time()
-            end = temporal_extent.get_end_time()
-            tz = temporal_extent.get_timezone()
+        elif issubclass(type(extent), AbsoluteTemporalExtent):
+            start = extent.get_start_time()
+            end = extent.get_end_time()
+            tz = extent.get_timezone()
 
             self.set_absolute_time(start, end, tz)
 

Modified: grass/trunk/lib/python/temporal/core.py
===================================================================
--- grass/trunk/lib/python/temporal/core.py	2013-08-16 13:34:57 UTC (rev 57456)
+++ grass/trunk/lib/python/temporal/core.py	2013-08-16 13:46:07 UTC (rev 57457)
@@ -20,7 +20,7 @@
 >>> # Execute a SQL statement
 >>> dbif.execute_transaction("SELECT datetime(0, 'unixepoch', 'localtime');")
 >>> # Mogrify an SQL statement
->>> dbif.mogrify_sql_statement(["SELECT name from raster_base where name = ?", 
+>>> dbif.mogrify_sql_statement(["SELECT name from raster_base where name = ?",
 ... ("precipitation",)])
 "SELECT name from raster_base where name = 'precipitation'"
 >>> dbif.close()
@@ -97,7 +97,7 @@
 
 ###############################################################################
 
-# This variable specifies if the ctypes interface to the grass 
+# This variable specifies if the ctypes interface to the grass
 # libraries should be used to read map specific data. If set to False
 # the grass scripting library will be used to get map informations.
 # The advantage of the ctypes inteface is speed, the disadvantage is that
@@ -107,7 +107,7 @@
 
 def set_use_ctypes_map_access(use_ctype = True):
     """!Define the map access method for the temporal GIS library
-    
+
        Using ctypes to read map metadata is much faster
        then using the grass.script interface that calls grass modules.
        The disadvantage is that GRASS C-library function will call
@@ -138,21 +138,21 @@
 
 def init():
     """!This function set the correct database backend from the environmental variables
-       and creates the grass location database structure for raster, 
-       vector and raster3d maps as well as for the space-time datasets strds, 
+       and creates the grass location database structure for raster,
+       vector and raster3d maps as well as for the space-time datasets strds,
        str3ds and stvds in case it not exists.
 
-        ATTENTION: This functions must be called before any spatio-temporal processing 
+        ATTENTION: This functions must be called before any spatio-temporal processing
                    can be started
     """
     # We need to set the correct database backend from the environment variables
     global tgis_backed
     global has_command_column
-    
 
+
     core.run_command("t.connect", flags="c")
     kv = core.parse_command("t.connect", flags="pg")
-    
+
     if "driver" in kv:
         if kv["driver"] == "sqlite":
             tgis_backed = kv["driver"]
@@ -177,7 +177,7 @@
     else:
         # Set the default sqlite3 connection in case nothing was defined
         core.run_command("t.connect", flags="d")
-            
+
     database = get_temporal_dbmi_init_string()
 
     db_exists = False
@@ -191,11 +191,11 @@
             cursor = connection.cursor()
             # Check for raster_base table
             cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='raster_base';")
-            
+
             name = cursor.fetchone()[0]
             if name == "raster_base":
                 db_exists = True
-                
+
                 # Try to add the command column to the space time dataset metadata tables
                 try:
                     cursor.execute('ALTER TABLE strds_metadata ADD COLUMN command VARCHAR;')
@@ -209,10 +209,10 @@
                     cursor.execute('ALTER TABLE stvds_metadata ADD COLUMN command VARCHAR;')
                 except:
                     pass
-                
+
             connection.commit()
             cursor.close()
-            
+
     elif tgis_backed == "pg":
         # Connect to database
         connection = dbmi.connect(database)
@@ -221,7 +221,7 @@
         cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables "
                        "WHERE table_name=%s)", ('raster_base',))
         db_exists = cursor.fetchone()[0]
-    
+
         if db_exists:
             # Try to add the command column to the space time dataset metadata tables
             try:
@@ -236,7 +236,7 @@
                 cursor.execute('ALTER TABLE stvds_metadata ADD COLUMN command VARCHAR;')
             except:
                 pass
-        
+
         connection.commit()
         cursor.close()
 
@@ -289,12 +289,12 @@
 
 
     if tgis_backed == "sqlite":
-        
+
         # We need to create the sqlite3 database path if it does not exists
         tgis_dir = os.path.dirname(database)
         if not os.path.exists(tgis_dir):
             os.makedirs(tgis_dir)
-            
+
         # Connect to database
         connection = dbmi.connect(database)
         cursor = connection.cursor()
@@ -370,12 +370,12 @@
             self.dbmi = sqlite3
         else:
             self.dbmi = psycopg2
-            
+
     def rollback(self):
         """
-            Roll back the last transaction. This must be called 
+            Roll back the last transaction. This must be called
             in case a new query should be performed after a db error.
-            
+
             This is only relevant for postgresql database.
         """
         if self.dbmi.__name__ == "psycopg2":
@@ -390,7 +390,7 @@
         init = get_temporal_dbmi_init_string()
         #print "Connect to",  self.database
         if self.dbmi.__name__ == "sqlite3":
-            self.connection = self.dbmi.connect(init, 
+            self.connection = self.dbmi.connect(init,
                     detect_types = self.dbmi.PARSE_DECLTYPES | self.dbmi.PARSE_COLNAMES)
             self.connection.row_factory = self.dbmi.Row
             self.connection.isolation_level = None
@@ -413,27 +413,27 @@
 
     def mogrify_sql_statement(self, content):
         """!Return the SQL statement and arguments as executable SQL string
-        
-           @param content The content as tuple with two entries, the first 
+
+           @param content The content as tuple with two entries, the first
                            entry is the SQL statement with DBMI specific
                            place holder (?), the second entry is the argument
                            list that should substitue the place holder.
-            
+
            Usage:
-           
+
            @code
-           
+
            >>> init()
            >>> dbif = SQLDatabaseInterfaceConnection()
            >>> dbif.mogrify_sql_statement(["SELECT ctime FROM raster_base WHERE id = ?",
            ... ["soil at PERMANENT",]])
            "SELECT ctime FROM raster_base WHERE id = 'soil at PERMANENT'"
-           
+
            @endcode
         """
         sql = content[0]
         args = content[1]
-        
+
         if self.dbmi.__name__ == "psycopg2":
             if len(args) == 0:
                 return sql
@@ -471,7 +471,7 @@
                         break
 
                     if args[count] is None:
-                        statement = "%sNULL%s" % (statement[0:pos], 
+                        statement = "%sNULL%s" % (statement[0:pos],
                                                   statement[pos + 1:])
                     elif isinstance(args[count], (int, long)):
                         statement = "%s%d%s" % (statement[0:pos], args[count],
@@ -480,10 +480,10 @@
                         statement = "%s%f%s" % (statement[0:pos], args[count],
                                                 statement[pos + 1:])
                     else:
-                        # Default is a string, this works for datetime 
+                        # Default is a string, this works for datetime
                         # objects too
-                        statement = "%s\'%s\'%s" % (statement[0:pos], 
-                                                    str(args[count]), 
+                        statement = "%s\'%s\'%s" % (statement[0:pos],
+                                                    str(args[count]),
                                                     statement[pos + 1:])
                     count += 1
 
@@ -506,7 +506,7 @@
         sql_script += "BEGIN TRANSACTION;\n"
         sql_script += statement
         sql_script += "END TRANSACTION;"
-        
+
         try:
             if self.dbmi.__name__ == "sqlite3":
                 self.cursor.executescript(statement)
@@ -526,21 +526,21 @@
 ###############################################################################
 
 def init_dbif(dbif):
-    """!This method checks if the database interface connection exists, 
+    """!This method checks if the database interface connection exists,
         if not a new one will be created, connected and True will be returned
 
         Usage code sample:
         @code
-        
+
         dbif, connect = tgis.init_dbif(None)
-        
+
         sql = dbif.mogrify_sql_statement(["SELECT * FROM raster_base WHERE ? = ?"],
                                                ["id", "soil at PERMANENT"])
         dbif.execute_transaction(sql)
-        
+
         if connect:
             dbif.close()
-        
+
         @endcode
     """
     if dbif is None:



More information about the grass-commit mailing list