浏览代码

temporal framework: Bugfixing and code cleanup in the temporal algebra

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@62775 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert 10 年之前
父节点
当前提交
d462215047

+ 2 - 2
lib/python/temporal/abstract_dataset.py

@@ -151,9 +151,9 @@ class AbstractDataset(SpatialTopologyDatasetConnector,
     def get_type(self):
     def get_type(self):
         """Return the type of this class as string
         """Return the type of this class as string
 
 
-           The type can be "vect", "rast", "rast3d", "stvds", "strds" or "str3ds"
+           The type can be "vector", "raster", "raster3d", "stvds", "strds" or "str3ds"
 
 
-           :return: "vect", "rast", "rast3d", "stvds", "strds" or "str3ds"
+           :return: "vector", "raster", "raster3d", "stvds", "strds" or "str3ds"
         """
         """
 
 
     @abstractmethod
     @abstractmethod

+ 91 - 38
lib/python/temporal/temporal_algebra.py

@@ -679,7 +679,7 @@ class TemporalAlgebraParser(object):
         self.dbif = SQLDatabaseInterfaceConnection()
         self.dbif = SQLDatabaseInterfaceConnection()
         self.dbif.connect()
         self.dbif.connect()
         self.register_null = register_null
         self.register_null = register_null
-        self.empty_maps = {}
+        self.removable_maps = {}
         self.m_mremove = pymod.Module('g.remove')
         self.m_mremove = pymod.Module('g.remove')
         self.m_copy = pymod.Module('g.copy')
         self.m_copy = pymod.Module('g.copy')
         self.nprocs = nprocs
         self.nprocs = nprocs
@@ -788,10 +788,9 @@ class TemporalAlgebraParser(object):
         self.parser.parse(expression)
         self.parser.parse(expression)
 
 
     def generate_map_name(self):
     def generate_map_name(self):
-        """Generate an unique intermediate vector map name
-            and register it in the objects map list for later removement.
+        """Generate an unique  map name and register it in the objects map list
 
 
-            The vector map names are unique between processes. Do not use the
+            The map names are unique between processes. Do not use the
             same object for map name generation in multiple threads.
             same object for map name generation in multiple threads.
         """
         """
         self.count += 1
         self.count += 1
@@ -803,7 +802,8 @@ class TemporalAlgebraParser(object):
         self.names[name] = name
         self.names[name] = name
         return name
         return name
 
 
-    def generate_new_map(self, base_map, bool_op = 'and', copy = True,  rename = True):
+    def generate_new_map(self, base_map, bool_op = 'and', copy = True,  rename = True, 
+                                              remove = False):
         """Generate a new map using the spatio-temporal extent of the base map
         """Generate a new map using the spatio-temporal extent of the base map
 
 
            :param base_map: This map is used to create the new map
            :param base_map: This map is used to create the new map
@@ -813,6 +813,7 @@ class TemporalAlgebraParser(object):
                   copied to mapA
                   copied to mapA
            :param rename: Specifies if the generated map get a random name or get
            :param rename: Specifies if the generated map get a random name or get
                   the id from the base map.
                   the id from the base map.
+            :param remove: Set this True if this map is an intermediate or empty map that should be removed
            :return: Map object
            :return: Map object
         """
         """
         # Generate an intermediate name for the result map list.
         # Generate an intermediate name for the result map list.
@@ -826,6 +827,8 @@ class TemporalAlgebraParser(object):
         if not rename:
         if not rename:
             name = base_map.get_id()
             name = base_map.get_id()
             map_new.set_id(name)
             map_new.set_id(name)
+        if remove is True:
+            self.removable_maps[name] = map_new
         # Make sure to set the uid that is used in several dictionaries
         # Make sure to set the uid that is used in several dictionaries
         map_new.uid = name
         map_new.uid = name
         return map_new
         return map_new
@@ -966,11 +969,58 @@ class TemporalAlgebraParser(object):
     
     
     ######################### Temporal functions ##############################
     ######################### Temporal functions ##############################
 
 
-    def check_stds(self, input, clear = False):
+    def remove_maps(self):
+        """Removes empty or intermediate maps of different type.
+        """
+        
+        map_names = {}
+        map_names["raster"] = []
+        map_names["raster3d"] = []
+        map_names["vector"] = []
+                
+        if self.removable_maps:
+            for map in self.removable_maps.values():
+                    map_names[map.get_type()].append(map.get_name())
+        
+        for key in map_names.keys():
+            if map_names[key]:
+                self.msgr.message(_("Removing un-needed or empty %s maps"%(key)))
+                self._remove_maps(map_names[key],  key)
+
+    def _remove_maps(self,  namelist,  map_type):
+        """Remove maps of specific type
+        
+            :param namelist: List of map names to be removed
+            :param map_type: The type of the maps  (rast, rast3d or vect)
+        """
+        max = 100
+        chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
+        for chunk in chunklist:
+            stringlist = ",".join(chunk)
+            
+            # Transform the map type
+            if map_type == "raster":
+                map_type = "rast"
+            if map_type == "raster3d":
+                map_type = "rast3d"
+            if map_type == "vector":
+                map_type = "vect"
+
+            if self.run:
+                m = copy.deepcopy(self.m_mremove)
+                m.inputs["type"].value = map_type
+                m.inputs["name"].value = stringlist
+                m.flags["f"].value = True
+                print m.get_bash()
+                m.run()
+
+    def check_stds(self, input, clear = False,  stds_type = None):
         """ Check if input space time dataset exist in database and return its map list.
         """ Check if input space time dataset exist in database and return its map list.
 
 
             :param input: Name of space time data set as string or list of maps.
             :param input: Name of space time data set as string or list of maps.
             :param clear: Reset the stored conditional values to empty list.
             :param clear: Reset the stored conditional values to empty list.
+            :param stds_type: The type of the space time dataset to be opened, if not provided 
+                                          then self.stdstype will be used
 
 
             :return: List of maps.
             :return: List of maps.
 
 
@@ -982,7 +1032,10 @@ class TemporalAlgebraParser(object):
             else:
             else:
                 id_input = input + "@" + self.mapset
                 id_input = input + "@" + self.mapset
             # Create empty spacetime dataset.
             # Create empty spacetime dataset.
-            stds = dataset_factory(self.stdstype, id_input)
+            if stds_type:
+                stds = dataset_factory(stds_type, id_input)
+            else:
+                stds = dataset_factory(self.stdstype, id_input)
             # Check for occurence of space time dataset.
             # Check for occurence of space time dataset.
             if stds.is_in_db(dbif=self.dbif) == False:
             if stds.is_in_db(dbif=self.dbif) == False:
                 raise FatalError(_("Space time %s dataset <%s> not found") %
                 raise FatalError(_("Space time %s dataset <%s> not found") %
@@ -1895,45 +1948,47 @@ class TemporalAlgebraParser(object):
 
 
         """
         """
         if self.run:
         if self.run:
+            dbif, connected = init_dbif(self.dbif) 
             if isinstance(t[3], list):
             if isinstance(t[3], list):
                 num = len(t[3])
                 num = len(t[3])
                 count = 0
                 count = 0
-                changeflag = False
-                register_list = []                
+                register_list = []      
                 if num > 0:
                 if num > 0:
+                    process_queue = pymod.ParallelModuleQueue(int(self.nprocs))
                     for map_i in t[3]:
                     for map_i in t[3]:
                         # Test if temporal extents have been changed by temporal 
                         # Test if temporal extents have been changed by temporal 
-                        # relation oeprators (&|+). 
+                        # relation operators (i|r). 
                         map_i_extent = map_i.get_temporal_extent_as_tuple()
                         map_i_extent = map_i.get_temporal_extent_as_tuple()
                         map_test = map_i.get_new_instance(map_i.get_id())
                         map_test = map_i.get_new_instance(map_i.get_id())
-                        map_test.select()
+                        map_test.select(dbif)
                         map_test_extent = map_test.get_temporal_extent_as_tuple()
                         map_test_extent = map_test.get_temporal_extent_as_tuple()
                         if map_test_extent != map_i_extent:
                         if map_test_extent != map_i_extent:
-                            changeflag = True
-                    for map_i in t[3]:
-                        if changeflag:
-                            # Check if resultmap names exist in GRASS database.
-                            mapname = self.basename + "_" + str(count) + "@" + self.mapset
-                            process_queue = pymod.ParallelModuleQueue(int(self.nprocs))
-                            if self.stdstype == 'strds':
-                                map_test = RasterDataset(mapname)
-                            elif self.stdstype == 'stvds':
-                                map_test = VectorDataset(mapname)
+                            # Create new map with basename
+                            newident = self.basename + "_" + str(count)
+                            map_result = map_i.get_new_instance(newident + "@" + self.mapset)
+
                             if map_test.map_exists() and self.overwrite == False:
                             if map_test.map_exists() and self.overwrite == False:
                                 self.msgr.fatal("Error raster maps with basename %s exist. Use --o flag to overwrite existing file" \
                                 self.msgr.fatal("Error raster maps with basename %s exist. Use --o flag to overwrite existing file" \
                                                     %(mapname))
                                                     %(mapname))
-                            # Create new maps with basename.
-                            newident = self.basename + "_" + str(count)
-                            map_result = map_i.get_new_instance(newident + "@" + self.mapset)
+
                             map_result.set_temporal_extent(map_i.get_temporal_extent())
                             map_result.set_temporal_extent(map_i.get_temporal_extent())
                             map_result.set_spatial_extent(map_i.get_spatial_extent())
                             map_result.set_spatial_extent(map_i.get_spatial_extent())
+                            # Attention we attach a new attribute
+                            map_result.is_new = True
                             register_list.append(map_result)
                             register_list.append(map_result)
-                            if self.stdstype == 'strds':
+
+                            # Copy the map
+                            if map_i.get_type() == 'raster':
                                 m = copy.deepcopy(self.m_copy)
                                 m = copy.deepcopy(self.m_copy)
                                 m.inputs["rast"].value = map_i.get_id(),  newident
                                 m.inputs["rast"].value = map_i.get_id(),  newident
                                 m.flags["overwrite"].value = self.overwrite
                                 m.flags["overwrite"].value = self.overwrite
                                 process_queue.put(m)
                                 process_queue.put(m)
-                            elif self.stdstype == 'stvds':
+                            elif map_i.get_type() == 'raster3d':
+                                m = copy.deepcopy(self.m_copy)
+                                m.inputs["rast3d"].value = map_i.get_id(),  newident
+                                m.flags["overwrite"].value = self.overwrite
+                                process_queue.put(m)
+                            elif map_i.get_type() == 'vector':
                                 m = copy.deepcopy(self.m_copy)
                                 m = copy.deepcopy(self.m_copy)
                                 m.inputs["vect"].value = map_i.get_id(),  newident
                                 m.inputs["vect"].value = map_i.get_id(),  newident
                                 m.flags["overwrite"].value = self.overwrite
                                 m.flags["overwrite"].value = self.overwrite
@@ -1941,11 +1996,11 @@ class TemporalAlgebraParser(object):
                         else:
                         else:
                             register_list.append(map_i)
                             register_list.append(map_i)
                         count  += 1
                         count  += 1
-                    if changeflag:
-                        process_queue.wait()
+
+                    # Wait for running processes
+                    process_queue.wait()
                     
                     
                     # Open connection to temporal database.
                     # Open connection to temporal database.
-                    dbif, connect = init_dbif(self.dbif)
                     # Create result space time dataset.                        
                     # Create result space time dataset.                        
                     resultstds = open_new_stds(t[1], self.stdstype, \
                     resultstds = open_new_stds(t[1], self.stdstype, \
                                                              'absolute', t[1], t[1], \
                                                              'absolute', t[1], t[1], \
@@ -1954,21 +2009,21 @@ class TemporalAlgebraParser(object):
                     for map_i in register_list:                
                     for map_i in register_list:                
                         # Get meta data from grass database.
                         # Get meta data from grass database.
                         map_i.load()
                         map_i.load()
-                        # Check if temporal extents have changed.
-                        if changeflag:
+                        # Check if temporal extents have changed and a new map was created
+                        if hasattr(map_i,  "is_new") is True:
                             # Do not register empty maps if not required
                             # Do not register empty maps if not required
                             # In case of a null map continue, do not register null maps
                             # In case of a null map continue, do not register null maps
                             if map_i.metadata.get_min() is None and \
                             if map_i.metadata.get_min() is None and \
                                map_i.metadata.get_max() is None:
                                map_i.metadata.get_max() is None:
                                 if not self.register_null:
                                 if not self.register_null:
-                                    self.empty_maps[map_i.get_name()] = map_i.get_name()
+                                    self.removable_maps[map_i.get_name()] = map_i
                                     continue
                                     continue
                             if map_i.is_in_db(dbif) and self.overwrite:
                             if map_i.is_in_db(dbif) and self.overwrite:
                                 # Update map in temporal database.
                                 # Update map in temporal database.
                                 map_i.update_all(dbif)
                                 map_i.update_all(dbif)
                             elif map_i.is_in_db(dbif) and self.overwrite == False:
                             elif map_i.is_in_db(dbif) and self.overwrite == False:
                                 # Raise error if map exists and no overwrite flag is given.
                                 # Raise error if map exists and no overwrite flag is given.
-                                self.msgr.fatal("Error vector map %s exist in temporal database. Use overwrite flag.  : \n%s" \
+                                self.msgr.fatal("Error map %s exist in temporal database. Use overwrite flag.  : \n%s" \
                                                     %(map_i.get_map_id(), cmd.popen.stderr))
                                                     %(map_i.get_map_id(), cmd.popen.stderr))
                             else:
                             else:
                                 # Insert map into temporal database.
                                 # Insert map into temporal database.
@@ -1976,20 +2031,18 @@ class TemporalAlgebraParser(object):
                         # Register map in result space time dataset.
                         # Register map in result space time dataset.
                         success = resultstds.register_map(map_i, dbif)
                         success = resultstds.register_map(map_i, dbif)
                     resultstds.update_from_registered_maps(dbif)
                     resultstds.update_from_registered_maps(dbif)
-                    dbif.close()
                 elif num == 0:
                 elif num == 0:
                     self.msgr.warning('Empty result space time dataset. No map has \
                     self.msgr.warning('Empty result space time dataset. No map has \
 been registered in %s'  %(t[1] ))
 been registered in %s'  %(t[1] ))
                     # Open connection to temporal database.
                     # Open connection to temporal database.
-                    dbif, connect = init_dbif(self.dbif)
                     # Create result space time dataset.                        
                     # Create result space time dataset.                        
                     resultstds = open_new_stds(t[1], self.stdstype, \
                     resultstds = open_new_stds(t[1], self.stdstype, \
                                                              'absolute', t[1], t[1], \
                                                              'absolute', t[1], t[1], \
-                                                             'mean', self.dbif, \
+                                                             'mean', dbif, \
                                                              overwrite = self.overwrite)
                                                              overwrite = self.overwrite)
-                    dbif.close()
+            if connected:
+                dbif.close()
             t[0] = t[3]
             t[0] = t[3]
-
         else:
         else:
             t[0] = t[3]
             t[0] = t[3]
 
 

+ 0 - 18
lib/python/temporal/temporal_raster3d_algebra.py

@@ -39,24 +39,6 @@ class TemporalRaster3DAlgebraParser(TemporalRasterBaseAlgebraParser):
         self.expression = expression
         self.expression = expression
         self.parser.parse(expression)
         self.parser.parse(expression)
 
 
-    def remove_empty_maps(self):
-        """! Removes the intermediate vector maps.
-        """
-        if self.empty_maps:
-            self.msgr.message(_("Removing empty 3D raster maps"))
-            namelist = self.empty_maps.values()
-            max = 100
-            chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
-            for chunk in chunklist:
-                stringlist = ",".join(chunk)
-
-                if self.run:
-                    m = copy.deepcopy(self.m_mremove)
-                    m.inputs["type"].value = "rast3d"
-                    m.inputs["names"].value = stringlist
-                    m.flags["f"].value = True
-                    m.run()
-
     ######################### Temporal functions ##############################
     ######################### Temporal functions ##############################
 
 
     def p_statement_assign(self, t):
     def p_statement_assign(self, t):

+ 0 - 18
lib/python/temporal/temporal_raster_algebra.py

@@ -79,24 +79,6 @@ class TemporalRasterAlgebraParser(TemporalRasterBaseAlgebraParser):
         self.expression = expression
         self.expression = expression
         self.parser.parse(expression)
         self.parser.parse(expression)
 
 
-    def remove_empty_maps(self):
-        """! Removes the intermediate raster maps.
-        """
-        if self.empty_maps:
-            self.msgr.message(_("Removing empty raster maps"))
-            namelist = self.empty_maps.values()
-            max = 100
-            chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
-            for chunk in chunklist:
-                stringlist = ",".join(chunk)
-
-                if self.run:
-                    m = copy.deepcopy(self.m_mremove)
-                    m.inputs["type"].value = "rast"
-                    m.inputs["name"].value = stringlist
-                    m.flags["f"].value = True
-                    m.run()
-
     ######################### Temporal functions ##############################
     ######################### Temporal functions ##############################
 
 
     def p_statement_assign(self, t):
     def p_statement_assign(self, t):

+ 12 - 8
lib/python/temporal/temporal_raster_base_algebra.py

@@ -142,7 +142,6 @@ class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
                   nprocs = 1, register_null = False):
                   nprocs = 1, register_null = False):
         TemporalAlgebraParser.__init__(self, pid, run, debug, spatial)
         TemporalAlgebraParser.__init__(self, pid, run, debug, spatial)
         self.nprocs = nprocs
         self.nprocs = nprocs
-        self.empty_maps = {}
         self.register_null = register_null
         self.register_null = register_null
 
 
     def check_null(self, t):
     def check_null(self, t):
@@ -543,11 +542,14 @@ class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
                 register_list = []
                 register_list = []
                 for i in range(num):
                 for i in range(num):
                     # Check if resultmap names exist in GRASS database.
                     # Check if resultmap names exist in GRASS database.
-                    rastername = self.basename + "_" + str(i) + "@" + self.mapset
-                    rastermap = RasterDataset(rastername)
-                    if rastermap.map_exists() and self.overwrite == False:
-                        self.msgr.fatal("Error raster maps with basename %s exist. Use --o flag to overwrite existing file" \
-                                            %(rastername))
+                    map_name = self.basename + "_" + str(i) + "@" + self.mapset
+                    if self.stdstype == "strds":
+                        new_map = RasterDataset(map_name)
+                    else:
+                        new_map = Raster3DDataset(map_name)
+                    if new_map.map_exists() and self.overwrite == False:
+                        self.msgr.fatal("Error maps with basename %s exist. Use --o flag to overwrite existing file" \
+                                            %(map_name))
                 map_test_list = []
                 map_test_list = []
                 for map_i in t[3]:
                 for map_i in t[3]:
                     newident = self.basename + "_" + str(count)
                     newident = self.basename + "_" + str(count)
@@ -568,6 +570,7 @@ class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
                         process_queue.put(m)
                         process_queue.put(m)
                     
                     
                     elif map_i.map_exists():
                     elif map_i.map_exists():
+                        # Copy map if it exists
                         map_test = map_i.get_new_instance(newident + "@" + self.mapset)
                         map_test = map_i.get_new_instance(newident + "@" + self.mapset)
                         map_test.set_temporal_extent(map_i.get_temporal_extent())
                         map_test.set_temporal_extent(map_i.get_temporal_extent())
                         map_test.set_spatial_extent(map_i.get_spatial_extent())
                         map_test.set_spatial_extent(map_i.get_spatial_extent())
@@ -577,6 +580,7 @@ class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
                         m_expression = newident + "=" + map_i.get_map_id()
                         m_expression = newident + "=" + map_i.get_map_id()
                         m.inputs["expression"].value = str(m_expression)
                         m.inputs["expression"].value = str(m_expression)
                         m.flags["overwrite"].value = self.overwrite
                         m.flags["overwrite"].value = self.overwrite
+                        print m.get_bash()
                         process_queue.put(m)
                         process_queue.put(m)
                         
                         
                     else:
                     else:
@@ -603,7 +607,7 @@ class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
                     if map_i.metadata.get_min() is None and \
                     if map_i.metadata.get_min() is None and \
                        map_i.metadata.get_max() is None:
                        map_i.metadata.get_max() is None:
                         if not self.register_null:
                         if not self.register_null:
-                            self.empty_maps[map_i.get_name()] = map_i.get_name()
+                            self.removable_maps[map_i.get_name()] = map_i
                             continue
                             continue
 
 
                     if map_i.is_in_db(dbif) and self.overwrite:
                     if map_i.is_in_db(dbif) and self.overwrite:
@@ -622,7 +626,7 @@ class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
                 dbif.close()
                 dbif.close()
                 t[0] = register_list
                 t[0] = register_list
 
 
-                self.remove_empty_maps()
+                self.remove_maps()
     
     
     def p_expr_spmap_function(self, t):
     def p_expr_spmap_function(self, t):
         # Add a single map.
         # Add a single map.

+ 48 - 37
lib/python/temporal/temporal_vector_algebra.py

@@ -136,25 +136,6 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
 
 
     ######################### Temporal functions ##############################
     ######################### Temporal functions ##############################
 
 
-    def remove_intermediate_vector_maps(self):
-        """ Removes the intermediate vector maps.
-        """
-        if self.names != {}:
-            namelist = self.names.values()
-            max = 100
-            chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
-            for chunk in chunklist:
-                stringlist = ",".join(chunk)
-                if self.debug:
-                    print "g.remove type=vect name=%s"%(stringlist)
-
-                if self.run:
-                    m = copy.deepcopy(self.m_mremove)
-                    m.inputs["type"].value = "vect"
-                    m.inputs["name"].value = stringlist
-                    m.flags["f"].value = True
-                    m.run()
-
     def get_temporal_topo_list(self, maplistA, maplistB = None, topolist = ["EQUAL"],
     def get_temporal_topo_list(self, maplistA, maplistB = None, topolist = ["EQUAL"],
                                assign_val = False, count_map = False, compare_bool = False,  
                                assign_val = False, count_map = False, compare_bool = False,  
                                compare_cmd = False,  compop = None, aggregate = None,  
                                compare_cmd = False,  compop = None, aggregate = None,  
@@ -272,6 +253,8 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
                         resultlist = resultlist + relationmap.cmd_list
                         resultlist = resultlist + relationmap.cmd_list
                     # Generate an intermediate name
                     # Generate an intermediate name
                     name = self.generate_map_name()
                     name = self.generate_map_name()
+                    # Put it into the removalbe map list
+                    self.removable_maps[name] = VectorDataset(name + "@%s"%(self.mapset))
                     map_i.set_id(name + "@" + self.mapset)
                     map_i.set_id(name + "@" + self.mapset)
                     # Set second input for overlay module.
                     # Set second input for overlay module.
                     mapbinput = relationmap.get_id()
                     mapbinput = relationmap.get_id()
@@ -320,7 +303,8 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
             tbrelations = map_i.get_temporal_relations()
             tbrelations = map_i.get_temporal_relations()
             # Generate an intermediate map for the result map list.
             # Generate an intermediate map for the result map list.
             map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', 
             map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', 
-                                                                        copy = True,  rename = False)
+                                                                        copy = True,  rename = False,  
+                                                                        remove = True)
             # Combine temporal and spatial extents of intermediate map with related maps.
             # Combine temporal and spatial extents of intermediate map with related maps.
             for topo in topolist:
             for topo in topolist:
                 if topo in tbrelations.keys():
                 if topo in tbrelations.keys():
@@ -328,7 +312,8 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
                         if temporal == 'r':
                         if temporal == 'r':
                             # Generate an intermediate map for the result map list.
                             # Generate an intermediate map for the result map list.
                             map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', 
                             map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', 
-                                                                                        copy = True,  rename = False)
+                                                                                        copy = True,  rename = False,  
+                                                                                        remove = True)
                         # Create overlayed map extent.
                         # Create overlayed map extent.
                         returncode = self.overlay_map_extent(map_new, map_j, 'and', \
                         returncode = self.overlay_map_extent(map_new, map_j, 'and', \
                                                                 temp_op = temporal)
                                                                 temp_op = temporal)
@@ -360,6 +345,8 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
         """
         """
         # Execute the command lists
         # Execute the command lists
         if self.run:
         if self.run:
+            # Open connection to temporal database.
+            dbif, connected = init_dbif(dbif=self.dbif)
             if isinstance(t[3], list):
             if isinstance(t[3], list):
                 num = len(t[3])
                 num = len(t[3])
                 count = 0
                 count = 0
@@ -415,8 +402,8 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
                                 break
                                 break
                         if returncode == 0:
                         if returncode == 0:
                             # We remove the invalid vector name from the remove list.
                             # We remove the invalid vector name from the remove list.
-                            if self.names.has_key(map_i.get_name()):
-                                self.names.pop(map_i.get_name())
+                            if self.removable_maps.has_key(map_i.get_name()):
+                                self.removable_maps.pop(map_i.get_name())
                             mapset = map_i.get_mapset()
                             mapset = map_i.get_mapset()
                             # Change map name to given basename.
                             # Change map name to given basename.
                             newident = self.basename + "_" + str(count)
                             newident = self.basename + "_" + str(count)
@@ -424,17 +411,41 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
                             m.inputs["vect"].value = (map_i.get_name(),newident)
                             m.inputs["vect"].value = (map_i.get_name(),newident)
                             m.flags["overwrite"].value = self.overwrite
                             m.flags["overwrite"].value = self.overwrite
                             m.run()
                             m.run()
-                            #m(vect = (map_i.get_name(),newident), \
-                            #    overwrite = self.overwrite)
                             map_i.set_id(newident + "@" + mapset)
                             map_i.set_id(newident + "@" + mapset)
                             count += 1
                             count += 1
                             register_list.append(map_i)
                             register_list.append(map_i)
                     else:
                     else:
-                        register_list.append(map_i)
+                        # Test if temporal extents have been changed by temporal 
+                        # relation operators (i|r). This is a code copy from temporal_algebra.py
+                        map_i_extent = map_i.get_temporal_extent_as_tuple()
+                        map_test = map_i.get_new_instance(map_i.get_id())
+                        map_test.select(dbif)
+                        map_test_extent = map_test.get_temporal_extent_as_tuple()
+                        if map_test_extent != map_i_extent:
+                            # Create new map with basename
+                            newident = self.basename + "_" + str(count)
+                            map_result = map_i.get_new_instance(newident + "@" + self.mapset)
+
+                            if map_test.map_exists() and self.overwrite == False:
+                                self.msgr.fatal("Error raster maps with basename %s exist. Use --o flag to overwrite existing file" \
+                                                    %(mapname))
+
+                            map_result.set_temporal_extent(map_i.get_temporal_extent())
+                            map_result.set_spatial_extent(map_i.get_spatial_extent())
+                            # Attention we attach a new attribute
+                            map_result.is_new = True
+                            count += 1
+                            register_list.append(map_result)
+
+                            # Copy the map
+                            m = copy.deepcopy(self.m_copy)
+                            m.inputs["vect"].value = map_i.get_id(),  newident
+                            m.flags["overwrite"].value = self.overwrite
+                            m.run()
+                        else:
+                            register_list.append(map_i)
 
 
                 if len(register_list) > 0:
                 if len(register_list) > 0:
-                    # Open connection to temporal database.
-                    dbif, connect = init_dbif(dbif=self.dbif)
                     # Create result space time dataset.
                     # Create result space time dataset.
                     resultstds = open_new_stds(t[1], self.stdstype, \
                     resultstds = open_new_stds(t[1], self.stdstype, \
                                                                 'absolute', t[1], t[1], \
                                                                 'absolute', t[1], t[1], \
@@ -442,7 +453,7 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
                                                                 overwrite = self.overwrite)
                                                                 overwrite = self.overwrite)
                     for map_i in register_list:
                     for map_i in register_list:
                         # Check if modules should be executed from command list.
                         # Check if modules should be executed from command list.
-                        if "cmd_list" in dir(map_i):
+                        if hasattr(map_i, "cmd_list") or hasattr(map_i, "is_new"):
                             # Get meta data from grass database.
                             # Get meta data from grass database.
                             map_i.load()
                             map_i.load()
                             if map_i.is_in_db(dbif=dbif) and self.overwrite:
                             if map_i.is_in_db(dbif=dbif) and self.overwrite:
@@ -457,17 +468,17 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
                                 # Insert map into temporal database.
                                 # Insert map into temporal database.
                                 map_i.insert(dbif=dbif)
                                 map_i.insert(dbif=dbif)
                         else:
                         else:
-                            #Get metadata from temporal database.                          
-                            #map_i.select(dbif=dbif)
+                            # Map is original from an input STVDS
                             map_i.load()
                             map_i.load()
                         # Register map in result space time dataset.
                         # Register map in result space time dataset.
+                        print map_i.get_temporal_extent_as_tuple()
                         success = resultstds.register_map(map_i, dbif=dbif)
                         success = resultstds.register_map(map_i, dbif=dbif)
-                        #count += 1
-                        #if count % 10 == 0:
-                        #    grass.percent(count, num, 1)
                     resultstds.update_from_registered_maps(dbif)
                     resultstds.update_from_registered_maps(dbif)
-                    dbif.close()
-                self.remove_intermediate_vector_maps()
+                
+            # Remove intermediate maps    
+            self.remove_maps()
+            if connected:
+                dbif.close()
             t[0] = t[3]
             t[0] = t[3]
 
 
     def p_overlay_operation(self, t):
     def p_overlay_operation(self, t):
@@ -549,7 +560,7 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
             for map_i in bufflist:
             for map_i in bufflist:
                 # Generate an intermediate name for the result map list.
                 # Generate an intermediate name for the result map list.
                 map_new = self.generate_new_map(base_map=map_i, bool_op=None,
                 map_new = self.generate_new_map(base_map=map_i, bool_op=None,
-                                                copy=True)
+                                                copy=True,  remove = True)
                 # Change spatial extent based on buffer size.
                 # Change spatial extent based on buffer size.
                 map_new.spatial_buffer(float(t[5]))
                 map_new.spatial_buffer(float(t[5]))
                 # Check buff type.
                 # Check buff type.

+ 18 - 16
lib/python/temporal/testsuite/unittests_temporal_algebra.py

@@ -70,7 +70,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_select1(self):
     def test_temporal_select1(self):
         """Testing the temporal select operator with equal relations. """
         """Testing the temporal select operator with equal relations. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A : A", basename="r", overwrite=True)
+        ta.parse(expression="R = A : A",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -86,7 +86,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_select2(self):
     def test_temporal_select2(self):
         """Testing the temporal select operator with equal relations. """
         """Testing the temporal select operator with equal relations. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A : D", basename="r", overwrite=True)
+        ta.parse(expression="R = A : D",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -102,7 +102,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_select3(self):
     def test_temporal_select3(self):
         """Testing the temporal select operator with equal relations. """
         """Testing the temporal select operator with equal relations. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A !: D", basename="r", overwrite=True)
+        ta.parse(expression="R = A !: D",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -118,7 +118,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_select_operators1(self):
     def test_temporal_select_operators1(self):
         """Testing the temporal select operator. Including temporal relations. """
         """Testing the temporal select operator. Including temporal relations. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A {:,during} C", basename="r", overwrite=True)
+        ta.parse(expression="R = A {:,during} C",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -134,7 +134,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_select_operators2(self):
     def test_temporal_select_operators2(self):
         """Testing the temporal select operator. Including temporal relations. """
         """Testing the temporal select operator. Including temporal relations. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A {:,equal|during} C", basename="r", overwrite=True)
+        ta.parse(expression="R = A {:,equal|during} C",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -151,7 +151,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
         """Testing the temporal select operator. Including temporal relations 
         """Testing the temporal select operator. Including temporal relations 
             and negation operation. """
             and negation operation. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A {!:,during} C", basename="r", overwrite=True)
+        ta.parse(expression="R = A {!:,during} C",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -168,7 +168,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
         """Testing the temporal select operator. Including temporal relations and 
         """Testing the temporal select operator. Including temporal relations and 
             temporal operators. """
             temporal operators. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A {:,during,d} C", basename="r", overwrite=True)
+        ta.parse(expression="R = A {:,during,d} C",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -190,7 +190,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
         """Testing the temporal select operator. Including temporal relations and 
         """Testing the temporal select operator. Including temporal relations and 
             temporal operators. """
             temporal operators. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = C {:,contains} A", basename="r", overwrite=True)
+        ta.parse(expression="R = C {:,contains} A",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -211,7 +211,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_extent1(self):
     def test_temporal_extent1(self):
         """Testing the temporal extent operators. """
         """Testing the temporal extent operators. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A {:,during,r} C", basename="r", overwrite=True)
+        ta.parse(expression="R = A {:,during,r} C",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -228,7 +228,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_extent2(self):
     def test_temporal_extent2(self):
         """Testing the temporal extent operators. """
         """Testing the temporal extent operators. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A {:,during,d} C", basename="r", overwrite=True)
+        ta.parse(expression="R = A {:,during,d} C",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -245,7 +245,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_extent3(self):
     def test_temporal_extent3(self):
         """Testing the temporal extent operators. """
         """Testing the temporal extent operators. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = A {:,during,u} C", basename="r", overwrite=True)
+        ta.parse(expression="R = A {:,during,u} C",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -266,7 +266,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_hash1(self):
     def test_temporal_hash1(self):
         """Testing the hash function in conditional statement. """
         """Testing the hash function in conditional statement. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = if(A # D == 1, A)", basename="r", overwrite=True)
+        ta.parse(expression="R = if(A # D == 1, A)",  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -283,7 +283,8 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_hash_operator1(self):
     def test_temporal_hash_operator1(self):
         """Testing the hash operator function in conditional statement. """
         """Testing the hash operator function in conditional statement. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = if(A {#,during} C == 1, A)", basename="r", overwrite=True)
+        ta.parse(expression="R = if(A {#,during} C == 1, A)",  stdstype = 'strds', 
+                                       basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -300,7 +301,8 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_temporal_hash_operator2(self):
     def test_temporal_hash_operator2(self):
         """Testing the hash operator function in conditional statement. """
         """Testing the hash operator function in conditional statement. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression="R = if({during}, C {#,contains} A == 2, A)", basename="r", overwrite=True)
+        ta.parse(expression="R = if({during}, C {#,contains} A == 2, A)",  
+                                                 stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -317,7 +319,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_tmap_function1(self):
     def test_tmap_function1(self):
         """Testing the tmap function. """
         """Testing the tmap function. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression='R = tmap(singletmap)', basename="r", overwrite=True)
+        ta.parse(expression='R = tmap(singletmap)',  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()
@@ -334,7 +336,7 @@ class TestTemporalAlgebra(grass.gunittest.TestCase):
     def test_tmap_function2(self):
     def test_tmap_function2(self):
         """Testing the tmap function. """
         """Testing the tmap function. """
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
         ta = tgis.TemporalAlgebraParser(run = True, debug = True)
-        ta.parse(expression='R = A : tmap(singletmap)', basename="r", overwrite=True)
+        ta.parse(expression='R = A : tmap(singletmap)',  stdstype = 'strds', basename="r", overwrite=True)
 
 
         D = tgis.open_old_stds("R", type="strds")
         D = tgis.open_old_stds("R", type="strds")
         D.select()
         D.select()

+ 17 - 0
lib/python/temporal/testsuite/unittests_temporal_raster_algebra.py

@@ -68,6 +68,23 @@ class TestTemporalRasterAlgebra(gunittest.TestCase):
         cls.runModule("t.unregister", maps="singletmap", quiet=True)
         cls.runModule("t.unregister", maps="singletmap", quiet=True)
         cls.del_temp_region()
         cls.del_temp_region()
 
 
+    def test_temporal_extent1(self):
+        """Testing the temporal extent operators. """
+        ta = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = A {:,during,r} C",   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(), 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(),  False)
+        self.assertEqual(D.get_granularity(),  u'2 days')
+
     def test_temporal_conditional_time_dimension_bug(self):
     def test_temporal_conditional_time_dimension_bug(self):
         """Testing the conditional time dimension bug, that uses the time 
         """Testing the conditional time dimension bug, that uses the time 
             dimension of the conditional statement instead the time dimension 
             dimension of the conditional statement instead the time dimension 

+ 16 - 0
lib/python/temporal/testsuite/unittests_temporal_vector_algebra.py

@@ -84,6 +84,22 @@ class TestTemporalVectorAlgebra(gunittest.TestCase):
         self.assertEqual( D.check_temporal_topology(),  True)
         self.assertEqual( D.check_temporal_topology(),  True)
         self.assertEqual(D.get_granularity(),  u'1 day')
         self.assertEqual(D.get_granularity(),  u'1 day')
 
 
+    def test_temporal_extent1(self):
+        """Testing the temporal extent operators. """
+        ta = tgis.TemporalVectorAlgebraParser(run = True, debug = True)
+        ta.parse(expression="R = A {:,during,r} C",  basename="r", overwrite=True)
+
+        D = tgis.open_old_stds("R", type="stvds")
+        D.select()
+        D.print_info()
+        maplist = D.get_registered_maps_as_objects()
+        self.assertEqual(D.metadata.get_number_of_maps(), 2)
+        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(),  False)
+        self.assertEqual(D.get_granularity(),  u'2 days')
+
     def test_temporal_select_operators(self):
     def test_temporal_select_operators(self):
         """Testing the temporal select operator. Including temporal relations. """
         """Testing the temporal select operator. Including temporal relations. """
         tva = tgis.TemporalVectorAlgebraParser(run = True, debug = True)
         tva = tgis.TemporalVectorAlgebraParser(run = True, debug = True)