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

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 27 12:46:27 EDT 2011


Author: huhabla
Date: 2011-10-27 09:46:27 -0700 (Thu, 27 Oct 2011)
New Revision: 48969

Modified:
   grass/trunk/lib/python/temporal/abstract_map_dataset.py
   grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
   grass/trunk/lib/python/temporal/space_time_datasets_tools.py
   grass/trunk/lib/python/temporal/temporal_extent.py
Log:
Bug fixing. New method to sample a spcae time dataset by granularity.


Modified: grass/trunk/lib/python/temporal/abstract_map_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_map_dataset.py	2011-10-27 16:32:10 UTC (rev 48968)
+++ grass/trunk/lib/python/temporal/abstract_map_dataset.py	2011-10-27 16:46:27 UTC (rev 48969)
@@ -104,7 +104,7 @@
            @param end_time: A double value in days
 
         """
-        if start_time and end_time:
+        if start_time != None and end_time != None:
             if abs(float(start_time)) > abs(float(end_time)):
                 core.fatal(_("End time must be greater than start time for %s map <%s>") % (self.get_type(), self.get_id()))
             else:
@@ -161,8 +161,8 @@
         else:
             start, end = self.get_relative_time()
 
-        if start:
-            if end:
+        if start != None:
+            if end != None:
                 if start >= end:
                     core.error(_("Map <%s> has incorrect time interval, start time is greater than end time") % (self.get_id()))
                     return False

Modified: grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2011-10-27 16:32:10 UTC (rev 48968)
+++ grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2011-10-27 16:46:27 UTC (rev 48969)
@@ -223,9 +223,9 @@
             if maps[i].is_time_relative():
                 start, end = maps[i].get_relative_time()
 
-            if start and end:
+            if start != None and end != None:
                 time_interval += 1
-            elif start and not end:
+            elif start != None and end == None:
                 time_point += 1
             else:
                 time_invalid += 1
@@ -345,6 +345,78 @@
 
         return True
 
+    def get_registered_maps_as_objects_by_granularity(self, gran=None, dbif=None):
+        """Return all registered maps as ordered (by start_time) object list with 
+           "gap" map objects (id==None) for temporal topological operations using the
+           granularity of the space time dataset as increment
+
+           A valid temporal topology (no overlapping or inclusion allowed) is needed to get correct results. 
+    
+           The dataset must have "interval" as temporal map type, so all maps have valid interval time.
+
+           Gaps between maps are identified as unregistered maps with id==None.
+
+           The objects are initialized with the id and the temporal extent (temporal type, start time, end time).
+           In case more map informations are needed, use the select() method for each listed object.
+
+           @param gran: The granularity to be used 
+           @param dbif: The database interface to be used
+
+           In case nothing found None is returned
+        """
+
+        connect = False
+
+        if dbif == None:
+            dbif = sql_database_interface()
+            dbif.connect()
+            connect = True
+
+        obj_list = []
+
+        if self.get_map_time() != "interval":
+            core.error(_("The temporal map type must be interval"))
+            #TODO: Check for valid topology too?
+            return None
+        
+        if gran == None:
+            gran = self.get_granularity()
+
+        start, end = self.get_valid_time()
+
+        while start < end:
+            if self.is_time_absolute():
+                next = increment_datetime_by_string(start, gran)
+            else:
+                next = start + gran
+
+            where = "(start_time <= '%s' and end_time >= '%s')" % (start, next)
+
+            rows = self.get_registered_maps("id", where, "start_time", dbif)
+
+            if rows:
+                if len(rows) > 1:
+                    core.error(_("More than one map found in a granule. Temporal granularity seems to be invalid or the chosen granularity is not a greatest common divider of all intervals and gaps in the dataset."))
+                # Take the first map    
+                map = self.get_new_map_instance(rows[0]["id"])
+            else:
+                # In case of a gap, create a dummy map with identifier None
+                map = self.get_new_map_instance(None)
+
+            if self.is_time_absolute():
+                map.set_absolute_time(start, next)
+            elif self.is_time_relative():
+                map.set_relative_time(start, next)
+
+            obj_list.append(copy.copy(map))
+
+            start = next
+
+        if connect == True:
+            dbif.close()
+
+        return obj_list
+
     def get_registered_maps_as_objects_with_gaps(self, where=None, dbif=None):
         """Return all registered maps as ordered (by start_time) object list with 
            "gap" map objects (id==None) for temporal topological operations
@@ -881,12 +953,15 @@
 		if dbmi.__name__ == "sqlite3":
 		    tstring = row[0]
 		    # Convert the unicode string into the datetime format
-		    if tstring.find(":") > 0:
-			time_format = "%Y-%m-%d %H:%M:%S"
-		    else:
-			time_format = "%Y-%m-%d"
+                    if self.is_time_absolute():
+                        if tstring.find(":") > 0:
+                            time_format = "%Y-%m-%d %H:%M:%S"
+                        else:
+                            time_format = "%Y-%m-%d"
 
-		    max_start_time = datetime.strptime(tstring, time_format)
+		        max_start_time = datetime.strptime(tstring, time_format)
+                    else:
+		        max_start_time = row[0]
 		else:
 		    max_start_time = row[0]
 
@@ -939,7 +1014,7 @@
             if self.is_time_absolute():
                 gran = compute_absolute_time_granularity(maps)
             elif self.is_time_relative():
-                gran = compute_absolute_time_granularity(maps)
+                gran = compute_relative_time_granularity(maps)
         else:
             gran = None
 

Modified: grass/trunk/lib/python/temporal/space_time_datasets_tools.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2011-10-27 16:32:10 UTC (rev 48968)
+++ grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2011-10-27 16:46:27 UTC (rev 48969)
@@ -28,7 +28,6 @@
 
 def register_maps_in_space_time_dataset(type, name, maps=None, file=None, start=None, end=None, increment=None, dbif = None, interval=False, fs="|"):
     """Use this method to register maps in space time datasets. This function is generic and
-       can handle raster, vector and raster3d maps as well as there space time datasets.
 
        Additionally a start time string and an increment string can be specified
        to assign a time interval automatically to the maps.
@@ -407,7 +406,7 @@
             else:
                 mapid = entry
 
-        sp = dataset_factory(type, id)
+        map = dataset_factory(type, mapid)
 
         # Use the time data from file
         if start_time_in_file:
@@ -557,6 +556,7 @@
             * "comma": Print the map ids (name at mapset) as comma separated string
             * "delta": Print the map ids (name at mapset) with start time, end time, relative length of intervals and the relative distance to the begin
             * "deltagaps": Same as "delta" with addtitionakl listing of gaps. Gaps can be simply identified as the id is "None"
+            * "gran": List map using the granularity of the space time dataset, columns are identical to deltagaps 
         @param header: Set True to print column names 
     """
     mapset =  core.gisenv()["MAPSET"]
@@ -577,12 +577,14 @@
         separator = "\t"
            
     # This method expects a list of objects for gap detection
-    if method == "delta" or method == "deltagaps":
+    if method == "delta" or method == "deltagaps" or method == "gran":
         columns = "id,start_time,end_time"
         if method == "deltagaps":
             maps = sp.get_registered_maps_as_objects_with_gaps(where, None)
-        else:
+        elif method == "delta":
             maps = sp.get_registered_maps_as_objects(where, "start_time", None)
+        elif method == "gran":
+            maps = sp.get_registered_maps_as_objects_by_granularity(None)
 
         if header:
             string = ""

Modified: grass/trunk/lib/python/temporal/temporal_extent.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_extent.py	2011-10-27 16:32:10 UTC (rev 48968)
+++ grass/trunk/lib/python/temporal/temporal_extent.py	2011-10-27 16:46:27 UTC (rev 48969)
@@ -288,8 +288,6 @@
 	    return "started"
 	if self.finished(map):
 	    return "finished"
-	if self.equivalent(map):
-	    return "equivalent"
 	if self.follows(map):
 	    return "follows"
 	if self.precedes(map):



More information about the grass-commit mailing list