[GRASS-SVN] r66205 - in grass/trunk/lib/python/temporal: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 14 03:22:07 PDT 2015


Author: huhabla
Date: 2015-09-14 03:22:07 -0700 (Mon, 14 Sep 2015)
New Revision: 66205

Added:
   grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_mixed_stds.py
Modified:
   grass/trunk/lib/python/temporal/c_libraries_interface.py
   grass/trunk/lib/python/temporal/core.py
   grass/trunk/lib/python/temporal/list_stds.py
Log:
temporal library: Added more debug messages, added new map algebra tests, list_stds supports now user created database interfaces


Modified: grass/trunk/lib/python/temporal/c_libraries_interface.py
===================================================================
--- grass/trunk/lib/python/temporal/c_libraries_interface.py	2015-09-14 02:49:57 UTC (rev 66204)
+++ grass/trunk/lib/python/temporal/c_libraries_interface.py	2015-09-14 10:22:07 UTC (rev 66205)
@@ -287,18 +287,27 @@
     count = 0
     mapset_list = []
     try:
-        # Initilaize the accessable mapset list, this is bad C design!!!
+        # Initialize the accessable mapset list, this is bad C design!!!
         libgis.G_get_mapset_name(0)
         mapsets = libgis.G_get_available_mapsets()
         while mapsets[count]:
             char_list = ""
             mapset = mapsets[count]
-            if libgis.G_mapset_permissions(mapset) == 1 and libgis.G_is_mapset_in_search_path(mapset) == 1:
-                c = 0
-                while mapset[c] != "\x00":
-                    char_list += mapset[c]
-                    c += 1
+            
+            permission = libgis.G_mapset_permissions(mapset)
+            in_search_path = libgis.G_is_mapset_in_search_path(mapset)
+            
+            c = 0
+            while mapset[c] != "\x00":
+                char_list += mapset[c]
+                c += 1
+            
+            if permission == 1 and permission == 1:
                 mapset_list.append(char_list)
+
+            libgis.G_debug(1, "c_library_server._available_mapsets: \n  mapset:  %s\n"\
+                              "  has permission %i\n  in search path: %i"%(char_list,
+                              permission, in_search_path))
             count += 1
 
         # We need to sort the mapset list, but the first one should be

Modified: grass/trunk/lib/python/temporal/core.py
===================================================================
--- grass/trunk/lib/python/temporal/core.py	2015-09-14 02:49:57 UTC (rev 66204)
+++ grass/trunk/lib/python/temporal/core.py	2015-09-14 10:22:07 UTC (rev 66205)
@@ -28,6 +28,7 @@
 
 :author: Soeren Gebbert
 """
+#import traceback
 import os
 # i18N
 import gettext
@@ -446,6 +447,10 @@
     for mapset in mapsets:
         driver = c_library_interface.get_driver_name(mapset)
         database = c_library_interface.get_database_name(mapset)
+        
+        message_interface.debug(1, "get_available_temporal_mapsets: "\
+                                   "\n  mapset %s\n  driver %s\n  database %s"%(mapset,
+                                   driver, database))
 
         if driver and database:
             # Check if the temporal sqlite database exists
@@ -548,6 +553,7 @@
     _init_tgis_c_library_interface()
     msgr = get_tgis_message_interface()
     msgr.debug(1, "Initiate the temporal database")
+                  #"\n  traceback:%s"%(str("  \n".join(traceback.format_stack()))))
 
     ciface = get_tgis_c_library_interface()
     driver_string = ciface.get_driver_name()
@@ -1048,9 +1054,15 @@
         if dbstring is None:
             global tgis_database_string
             self.dbstring = tgis_database_string
+        
+        self.dbstring = dbstring
 
         self.msgr = get_tgis_message_interface()
-        self.msgr.debug(1, "SQLDatabaseInterfaceConnection constructor")
+        self.msgr.debug(1, "DBConnection constructor:"\
+                           "\n  backend: %s"\
+                           "\n  dbstring: %s"%(backend, self.dbstring))
+                           #"\n  traceback:%s"%(backend, self.dbstring, 
+                           #str("  \n".join(traceback.format_stack()))))
 
     def __del__(self):
         if self.connected is True:

Modified: grass/trunk/lib/python/temporal/list_stds.py
===================================================================
--- grass/trunk/lib/python/temporal/list_stds.py	2015-09-14 02:49:57 UTC (rev 66204)
+++ grass/trunk/lib/python/temporal/list_stds.py	2015-09-14 10:22:07 UTC (rev 66205)
@@ -26,7 +26,7 @@
 
 
 def get_dataset_list(type, temporal_type, columns=None, where=None,
-                     order=None):
+                     order=None, dbif=None):
     """ Return a list of time stamped maps or space time datasets of a specific
         temporal type that are registred in the temporal database
 
@@ -41,6 +41,7 @@
         :param where: A where statement for selected listing without "WHERE"
         :param order: A comma separated list of columns to order the
                       datasets by category
+        :param dbif: The database interface to be used
 
         :return: A dictionary with the rows of the SQL query for each
                  available mapset
@@ -71,8 +72,7 @@
     id = None
     sp = dataset_factory(type, id)
 
-    dbif = SQLDatabaseInterfaceConnection()
-    dbif.connect()
+    dbif, connected = init_dbif(dbif)
 
     mapsets = get_available_temporal_mapsets()
 
@@ -105,13 +105,16 @@
         if rows:
             result[mapset] = rows
 
+    if connected:
+        dbif.close()
+
     return result
 
 ###############################################################################
 
 
 def list_maps_of_stds(type, input, columns, order, where, separator,
-                      method, no_header=False, gran=None):
+                      method, no_header=False, gran=None, dbif=None):
     """ List the maps of a space time dataset using diffetent methods
 
         :param type: The type of the maps raster, raster3d or vector
@@ -124,6 +127,7 @@
         :param separator: The field separator character between the columns
         :param method: String identifier to select a method out of cols,
                        comma,delta or deltagaps
+        :param dbif: The database interface to be used
 
             - "cols" Print preselected columns specified by columns
             - "comma" Print the map ids ("name at mapset") as comma separated string
@@ -141,7 +145,7 @@
                      dataset is used
     """
 
-    dbif, connected = init_dbif(None)
+    dbif, connected = init_dbif(dbif)
     msgr = get_tgis_message_interface()
 
     sp = open_old_stds(input, type, dbif)

Added: grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_mixed_stds.py
===================================================================
--- grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_mixed_stds.py	                        (rev 0)
+++ grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_mixed_stds.py	2015-09-14 10:22:07 UTC (rev 66205)
@@ -0,0 +1,206 @@
+"""
+(C) 2013 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+:authors: Soeren Gebbert and Thomas Leppelt
+"""
+
+import grass.script
+import grass.temporal as tgis
+from grass.gunittest.case import TestCase
+from grass.gunittest.main import test
+import datetime
+import os
+
+class TestTemporalAlgebraMixedDatasets(TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        tgis.init(True) # Raise on error instead of exit(1)
+        cls.use_temp_region()
+        cls.runModule("g.region", n=80.0, s=0.0, e=120.0,
+                                       w=0.0, t=1.0, b=0.0, res=10.0)
+
+        cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a1 = 1")
+        cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a2 = 2")
+        cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a3 = 3")
+        cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a4 = 4")
+
+        tgis.open_new_stds(name="A", type="str3ds", temporaltype="absolute",
+                                         title="A", descr="A", semantic="field", overwrite=True)
+
+        tgis.register_maps_in_space_time_dataset(type="raster3d", name="A", maps="a1,a2,a3,a4",
+                                                 start="2001-01-01", increment="1 day", interval=True)
+
+
+        cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b1 = 5")
+        cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b2 = 6")
+
+        tgis.open_new_stds(name="B", type="strds", temporaltype="absolute",
+                                         title="B", descr="B", semantic="field", overwrite=True)
+
+        tgis.register_maps_in_space_time_dataset(type="raster", name="B", maps="b1,b2",
+                                                 start="2001-01-01", increment="2 day", interval=True)
+
+
+        cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=3,  output='c1')
+
+        tgis.open_new_stds(name="C", type="stvds", temporaltype="absolute",
+                                         title="B", descr="C", semantic="field", overwrite=True)
+
+        tgis.register_maps_in_space_time_dataset(type="vector", name="C", maps="c1",
+                                                 start="2001-01-02", increment="2 day", interval=True)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the temporary region
+        """
+        cls.runModule("t.remove", flags="rf", type="str3ds",  inputs="A", quiet=True)
+        cls.runModule("t.remove", flags="rf", type="strds", inputs="B", quiet=True)
+        cls.runModule("t.remove", flags="rf", type="stvds", inputs="C", quiet=True)
+        cls.del_temp_region()
+
+    def test_temporal_select_operators1(self):
+        """Testing the temporal select operator. Including temporal relations. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = A {:,during} stvds(C)",  stdstype = 'str3ds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="str3ds")
+        D.select()
+        self.assertEqual(D.metadata.get_number_of_maps(), 2)
+        self.assertEqual(D.metadata.get_min_min(), 2) 
+        self.assertEqual(D.metadata.get_max_max(), 3) 
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 4))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+    def test_temporal_select_operators2(self):
+        """Testing the temporal select operator. Including temporal relations. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = A {:,equal|during} stvds(C)",  stdstype = 'str3ds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="str3ds")
+        D.select()
+        self.assertEqual(D.metadata.get_number_of_maps(), 2)
+        self.assertEqual(D.metadata.get_min_min(), 2) 
+        self.assertEqual(D.metadata.get_max_max(), 3) 
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 4))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+    def test_temporal_select_operators3(self):
+        """Testing the temporal select operator. Including temporal relations 
+            and negation operation. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = A {!:,during} stvds(C)",  stdstype = 'str3ds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="str3ds")
+        D.select()
+        self.assertEqual(D.metadata.get_number_of_maps(), 2)
+        self.assertEqual(D.metadata.get_min_min(), 1) 
+        self.assertEqual(D.metadata.get_max_max(), 4) 
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 5))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+    def test_temporal_select_operators4(self):
+        """Testing the temporal select operator. Including temporal relations and 
+            temporal operators. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="V = C {:,contains} str3ds(A)",  
+                       stdstype = 'stvds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("V", type="stvds")
+        D.select()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 1)
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 4))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'2 days')
+
+    def test_temporal_select_operators5(self):
+        """Testing the temporal select operator. Including temporal relations and 
+            temporal operators. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = A {:,during} strds(B)",  
+                       stdstype = 'str3ds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="str3ds")
+        D.select()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 4)
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 5))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+    def test_temporal_hash_operator1(self):
+        """Testing the hash operator function in conditional statement. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = if(A {#,during} stvds(C) == 1, A)",  
+                       stdstype = 'str3ds', 
+                       basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="str3ds")
+        D.select()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 2)
+        self.assertEqual(D.metadata.get_min_min(), 2) 
+        self.assertEqual(D.metadata.get_max_max(), 3) 
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 4))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+    def test_temporal_hash_operator2(self):
+        """Testing the hash operator function in conditional statement. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = if({during}, stvds(C) {#,contains} A == 2, A)",  
+                                                 stdstype = 'str3ds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="str3ds")
+        D.select()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 2)
+        self.assertEqual(D.metadata.get_min_min(), 2) 
+        self.assertEqual(D.metadata.get_max_max(), 3) 
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 4))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+    def test_different_stds_handling(self):
+        """Testing the handling of different stds types as output. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = if({during}, stvds(C) {#,contains} str3ds(A) == 2, str3ds(A))",  
+                                                 stdstype = 'strds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="str3ds")
+        D.select()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 2)
+        self.assertEqual(D.metadata.get_min_min(), 2) 
+        self.assertEqual(D.metadata.get_max_max(), 3) 
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 4))
+        self.assertEqual( D.check_temporal_topology(),  True)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+if __name__ == '__main__':
+    test()



More information about the grass-commit mailing list