[GRASS-SVN] r51921 - in grass/trunk: lib/python/temporal temporal/t.rast.aggregate temporal/t.rast.aggregate.ds temporal/t.vect.what.strds

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jun 1 11:42:46 PDT 2012


Author: huhabla
Date: 2012-06-01 11:42:45 -0700 (Fri, 01 Jun 2012)
New Revision: 51921

Modified:
   grass/trunk/lib/python/temporal/aggregation.py
   grass/trunk/temporal/t.rast.aggregate.ds/t.rast.aggregate.ds.py
   grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py
   grass/trunk/temporal/t.vect.what.strds/t.vect.what.strds.py
   grass/trunk/temporal/t.vect.what.strds/test.t.vect.what.strds.sh
Log:
Support for aggregation in t.vect.what.strds. Aggregation interface simplified.


Modified: grass/trunk/lib/python/temporal/aggregation.py
===================================================================
--- grass/trunk/lib/python/temporal/aggregation.py	2012-06-01 16:41:31 UTC (rev 51920)
+++ grass/trunk/lib/python/temporal/aggregation.py	2012-06-01 18:42:45 UTC (rev 51921)
@@ -23,6 +23,7 @@
 """
 
 from space_time_datasets import *
+import grass.lib.gis as libgis
 
 ###############################################################################
 
@@ -80,12 +81,9 @@
 
 ###############################################################################
 
-def aggregate_raster_maps(orig_ds, dataset, mapset, inputs, base, start, end, count, method, register_null, dbif):
+def aggregate_raster_maps(inputs, base, start, end, count, method, register_null, dbif):
     """!Aggregate a list of raster input maps with r.series
        
-       @param orig_ds: Original space time raster dataset from which the maps are selected
-       @param dataset: The new space time raster dataset to insert the aggregated map
-       @param mapset: The current mapset
        @param inputs: The names of the raster maps to be aggregated
        @param base: The basename of the new created raster maps
        @param start: The start time of the sample interval, may be relative or absolute
@@ -98,17 +96,19 @@
 
     core.verbose(_("Aggregate %s raster maps") %(len(inputs)))
     output = "%s_%i" % (base, count)
+    
+    mapset = libgis.G_mapset()
 
     map_id = output + "@" + mapset
 
-    new_map = dataset.get_new_map_instance(map_id)
+    new_map = raster_dataset(map_id)
 
     # Check if new map is in the temporal database
     if new_map.is_in_db(dbif):
         if core.overwrite() == True:
             # Remove the existing temporal database entry
             new_map.delete(dbif)
-            new_map = dataset.get_new_map_instance(map_id)
+            new_map = raster_dataset(map_id)
         else:
             core.error(_("Raster map <%s> is already in temporal database, use overwrite flag to overwrite"))
             return
@@ -130,6 +130,7 @@
     if ret != 0:
         dbif.close()
         core.fatal(_("Error while r.series computation"))
+        
 
     # Read the raster map data
     new_map.load()
@@ -138,15 +139,6 @@
     if new_map.metadata.get_min() == None and new_map.metadata.get_max() == None:
         if not register_null:
             core.run_command("g.remove", rast=output)
-            return
-
-    # Set the time stamp and write it to the raster map
-    if dataset.is_time_absolute():
-        new_map.set_absolute_time(start, end, None)
-    else:
-        new_map.set_relative_time(start, end, orig_ds.get_relative_time_unit())
-
-    # Insert map in temporal database
-    new_map.insert(dbif)
-
-    dataset.register_map(new_map, dbif)
+            return None
+    
+    return new_map

Modified: grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py
===================================================================
--- grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py	2012-06-01 16:41:31 UTC (rev 51920)
+++ grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py	2012-06-01 18:42:45 UTC (rev 51921)
@@ -151,10 +151,21 @@
         input_map_names = tgis.collect_map_names(sp, dbif, start, end, sampling)
 
         if input_map_names:
-            tgis.aggregate_raster_maps(sp, new_sp, mapset, input_map_names, base, start, end, count, method, register_null, dbif)
-	    
-        count += 1
+            new_map = tgis.aggregate_raster_maps(input_map_names, base, start, end, count, method, register_null, dbif)
 
+            if new_map:
+                # Set the time stamp and write it to the raster map
+                if sp.is_time_absolute():
+                    new_map.set_absolute_time(start, end, None)
+                else:
+                    new_map.set_relative_time(start, end, sp.get_relative_time_unit())
+
+                # Insert map in temporal database
+                new_map.insert(dbif)
+                new_sp.register_map(new_map, dbif)
+
+                count += 1
+
     # Update the spatio-temporal extent and the raster metadata table entries
     new_sp.update_from_registered_maps(dbif)
         

Modified: grass/trunk/temporal/t.rast.aggregate.ds/t.rast.aggregate.ds.py
===================================================================
--- grass/trunk/temporal/t.rast.aggregate.ds/t.rast.aggregate.ds.py	2012-06-01 16:41:31 UTC (rev 51920)
+++ grass/trunk/temporal/t.rast.aggregate.ds/t.rast.aggregate.ds.py	2012-06-01 18:42:45 UTC (rev 51921)
@@ -161,8 +161,19 @@
         input_map_names = tgis.collect_map_names(sp, dbif, start, end, sampling)
 
         if input_map_names:
-            tgis.aggregate_raster_maps(sp, new_sp, mapset, input_map_names, base, start, end, count, method, register_null, dbif)
+            new_map = tgis.aggregate_raster_maps(input_map_names, base, start, end, count, method, register_null, dbif)
 
+            if new_map:
+                # Set the time stamp and write it to the raster map
+                if sp.is_time_absolute():
+                    new_map.set_absolute_time(start, end, None)
+                else:
+                    new_map.set_relative_time(start, end, sp.get_relative_time_unit())
+
+                # Insert map in temporal database
+                new_map.insert(dbif)
+                new_sp.register_map(new_map, dbif)
+                
     # Update the spatio-temporal extent and the raster metadata table entries
     new_sp.update_from_registered_maps(dbif)
         

Modified: grass/trunk/temporal/t.vect.what.strds/t.vect.what.strds.py
===================================================================
--- grass/trunk/temporal/t.vect.what.strds/t.vect.what.strds.py	2012-06-01 16:41:31 UTC (rev 51920)
+++ grass/trunk/temporal/t.vect.what.strds/t.vect.what.strds.py	2012-06-01 18:42:45 UTC (rev 51921)
@@ -5,7 +5,7 @@
 # MODULE:	t.vect.what.strds
 # AUTHOR(S):	Soeren Gebbert
 #
-# PURPOSE:	Uploads raster map values at spatial and temporal positions of vector points to the tables. The maps are registered in space time datasets
+# PURPOSE:	Uploads raster map values at spatial and temporal positions of vector points to the tables.
 # COPYRIGHT:	(C) 2011 by the GRASS Development Team
 #
 #		This program is free software under the GNU General Public
@@ -15,7 +15,7 @@
 #############################################################################
 
 #%module
-#% description: Uploads raster map values at spatial and temporal positions of vector points to the tables. The maps are registered in space time datasets.
+#% description: Uploads raster map values at spatial and temporal positions of vector points to the tables.
 #% keywords: temporal
 #% keywords: sampling
 #%end
@@ -36,6 +36,16 @@
 #% multiple: no
 #%end
 
+#%option
+#% key: method
+#% type: string
+#% description: Aggregate operation to be performed on the raster maps
+#% required: yes
+#% multiple: no
+#% options: disabled,average,count,median,mode,minimum,min_raster,maximum,max_raster,stddev,range,sum,variance,diversity,slope,offset,detcoeff,quart1,quart3,perc90,quantile,skewness,kurtosis
+#% answer: disabled
+#%end
+
 #%option G_OPT_DB_WHERE
 #%end
 
@@ -46,7 +56,7 @@
 #%option G_OPT_T_SAMPLE
 #%end
 
-
+import os
 import grass.script as grass
 import grass.temporal as tgis
 import grass.script.raster as raster
@@ -60,6 +70,7 @@
     strds = options["strds"]
     where = options["where"]
     column = options["column"]
+    method = options["method"]
     tempwhere = options["t_where"]
     sampling = options["sampling"]
 
@@ -127,11 +138,24 @@
         end = row["end_time"]
         vectmap = row["name"] + "@" + row["mapset"]
         layer = row["layer"]
+        count = 0
 
         raster_maps = tgis.collect_map_names(strds_sp, dbif, start, end, sampling)
-
+        
+        aggreagated_map_name = None
+                
         if raster_maps:
-            # Collect the names of the raster maps
+	    # Aggregation
+	    if method != "disabled" and len(raster_maps) > 1:
+	      # Generate the temporary map name
+	      aggreagated_map_name = "aggreagated_map_name_"+ str(os.getpid()) 
+	      new_map = tgis.aggregate_raster_maps(raster_maps, aggreagated_map_name, start, end, 0, method, False, dbif)
+	      aggreagated_map_name = aggreagated_map_name + "_0" 
+	      if new_map == None:
+		  continue
+	      # We overwrite the raster_maps list
+	      raster_maps = (new_map.get_id(), )
+	      
             for rastermap in raster_maps:
                 
                 if column:
@@ -164,6 +188,12 @@
                     dbif.close()
                     grass.fatal(_("Unable to run v.what.rast for vector map <%s> and raster map <%s>")%(vectmap, rastermap))
 
+                if aggreagated_map_name:
+		    ret = grass.run_command("g.remove", rast=aggreagated_map_name)
+		    if ret != 0:
+			dbif.close()
+			grass.fatal(_("Unable to remove raster map <%s>")%(aggreagated_map_name))
+                    
                 # Use the first map in case a column names was provided
                 if column:
                     break

Modified: grass/trunk/temporal/t.vect.what.strds/test.t.vect.what.strds.sh
===================================================================
--- grass/trunk/temporal/t.vect.what.strds/test.t.vect.what.strds.sh	2012-06-01 16:41:31 UTC (rev 51920)
+++ grass/trunk/temporal/t.vect.what.strds/test.t.vect.what.strds.sh	2012-06-01 18:42:45 UTC (rev 51921)
@@ -37,6 +37,11 @@
 v.db.select map=soil_2
 v.db.select map=soil_3
 
+t.vect.what.strds --v input=soil_abs1 strds=precip_abs1 sampling=during column=map_vals method=average
+v.db.select map=soil_1
+v.db.select map=soil_2
+v.db.select map=soil_3
+
 t.vect.what.strds --v input=soil_abs1 strds=precip_abs1 sampling=start,during
 v.db.select map=soil_1
 v.db.select map=soil_2



More information about the grass-commit mailing list