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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Apr 1 04:23:17 PDT 2015


Author: huhabla
Date: 2015-04-01 04:23:17 -0700 (Wed, 01 Apr 2015)
New Revision: 64970

Modified:
   grass/trunk/lib/python/temporal/temporal_algebra.py
   grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py
   grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py
Log:
temporal framework: Patch from Thomas Leppelt, adding merge function to algebra


Modified: grass/trunk/lib/python/temporal/temporal_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_algebra.py	2015-04-01 06:19:00 UTC (rev 64969)
+++ grass/trunk/lib/python/temporal/temporal_algebra.py	2015-04-01 11:23:17 UTC (rev 64970)
@@ -466,6 +466,7 @@
         'tsnap'  : 'TSNAP',
         'tshift' : 'TSHIFT',
         'tmap' : 'TMAP',
+        'merge' : 'MERGE',
         'strds' : 'STRDS',
         'str3ds' : 'STR3DS',
         'stvds' : 'STVDS',
@@ -629,7 +630,7 @@
         t.lineno += len(t.value)
 
     def t_NAME(self, t):
-        r'[a-zA-Z_][a-zA-Z_0-9]*'
+        r'[a-zA-Z_][a-zA-Z_0-9\@]*'
         self.temporal_symbol(t)
         return t
 
@@ -976,7 +977,7 @@
                 if temp_ext != None:
                     mapA.set_temporal_extent(temp_ext)
                 else:
-                    returncode = 0 
+                    returncode = 0
         return(returncode)
 
     def set_temporal_extent_list(self, maplist, topolist = ["EQUAL"], temporal = 'l' ):
@@ -2226,6 +2227,50 @@
         if self.debug:
             print "tmap(", t[3] , ")"
 
+    def p_expr_tmerge_function(self, t):
+        # Merge two maplists of same STDS type into a result map list.
+        # Only possible for same data types!
+        # Examples:
+        #    R = merge(A, B)
+        """
+        expr : MERGE LPAREN stds COMMA stds RPAREN
+               | MERGE LPAREN expr COMMA stds RPAREN
+               | MERGE LPAREN stds COMMA expr RPAREN
+               | MERGE LPAREN expr COMMA expr RPAREN
+        """
+        if self.run:
+            # Check input map.
+            maplistA   = self.check_stds(t[3])
+            maplistB   = self.check_stds(t[5])
+            
+            # Check empty lists.
+            if len(maplistA) == 0 and len(maplistB) == 0:
+                self.msgr.warning(_("Merging empty map lists"))
+                resultlist = maplistA + maplistB
+            elif len(maplistA) == 0:
+                self.msgr.message(_("First Map list is empty, can't merge it. Return only last map list"))
+                resultlist = maplistB
+            elif len(maplistB) == 0:
+                self.msgr.message(_("Second Map list is empty, can't merge it. Return only first map list"))
+                resultlist = maplistA
+            else:
+                # Check for identical data types in map lists.
+                typeA = maplistA[0].metadata.get_datatype()
+                typeB = maplistB[0].metadata.get_datatype()
+
+                if typeA != typeB:
+                    grass.fatal(_("Space time datasets to merge must have the same temporal type"))
+
+                resultlist = maplistA + maplistB
+            
+            # Return map list.
+            t[0] = resultlist
+        else:
+            t[0] = "merge(",  t[3], ",", t[5], ")"
+
+        if self.debug:
+            print "merge(", t[3], ",", t[5], ")"
+
     def p_t_hash(self,t):
         """
         t_hash_var : stds HASH stds

Modified: grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py	2015-04-01 06:19:00 UTC (rev 64969)
+++ grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py	2015-04-01 11:23:17 UTC (rev 64970)
@@ -395,6 +395,7 @@
             # Generate an intermediate map for the result map list.
             map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', 
                                                                         copy = True,  rename = True)
+            
             # Combine temporal and spatial extents of intermediate map with related maps.
             for topo in topolist:
                 if topo in tbrelations.keys():
@@ -406,6 +407,7 @@
                         # Create overlayed map extent.
                         returncode = self.overlay_map_extent(map_new, map_j, 'and', \
                                                                 temp_op = temporal)
+                        
                         # Stop the loop if no temporal or spatial relationship exist.
                         if returncode == 0:
                             break
@@ -1006,7 +1008,7 @@
                                operator_cmd = True, compop = function)
             # Set temporal extent based on topological relationships.
             resultlist = self.set_temporal_extent_list(complist, topolist = relations, 
-                                temporal = temporal)
+                                temporal = temporal)            
         
         t[0] = resultlist
   
@@ -1461,8 +1463,7 @@
             elif t[7] == 'null':
                 theninput = self.check_stds(t[5])
                 elseinput = str(t[7] + t[8] + t[9])
-        print(theninput)
-        print(elseinput)
+
         # Create conditional command map list.
         resultlist = self.build_condition_cmd_list(ifmaplist, theninput,  elseinput,  
                                                         condition_topolist = ["EQUAL"], 

Modified: grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py	2015-04-01 06:19:00 UTC (rev 64969)
+++ grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py	2015-04-01 11:23:17 UTC (rev 64970)
@@ -350,5 +350,39 @@
         self.assertEqual( D.check_temporal_topology(),  True)
         self.assertEqual(D.get_granularity(),  u'1 day')
 
+    def test_merge_function1(self):
+        """Testing the merge function. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression='R = merge(A,D)',  stdstype = 'strds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="strds")
+        D.select()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 7)
+        self.assertEqual(D.metadata.get_min_min(), 1) 
+        self.assertEqual(D.metadata.get_max_max(), 10) 
+        start, end = D.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 6))
+        self.assertEqual( D.check_temporal_topology(),  False)
+        self.assertEqual(D.get_granularity(),  u'1 day')
+
+    def test_merge_function2(self):
+        """Testing the merge function. """
+        ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+        ta.parse(expression='R = merge(A, B {!:,contains} A)',  stdstype = 'strds', basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="strds")
+        D.select()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 4)
+        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')
+
 if __name__ == '__main__':
     grass.gunittest.test()



More information about the grass-commit mailing list