[GRASS-SVN] r54196 - in grass/trunk: lib/python/temporal temporal/t.create temporal/t.sample

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 5 02:42:09 PST 2012


Author: huhabla
Date: 2012-12-05 02:42:08 -0800 (Wed, 05 Dec 2012)
New Revision: 54196

Modified:
   grass/trunk/lib/python/temporal/space_time_datasets_tools.py
   grass/trunk/temporal/t.create/t.create.py
   grass/trunk/temporal/t.sample/t.sample.py
   grass/trunk/temporal/t.sample/test.t.sample.sh
Log:
Temporal sampling method now returns the map matrix.
Sampling was moved from t.sample into the temporal library.
STDS creation was moved from t.create into the temporal library.


Modified: grass/trunk/lib/python/temporal/space_time_datasets_tools.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2012-12-05 10:18:06 UTC (rev 54195)
+++ grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2012-12-05 10:42:08 UTC (rev 54196)
@@ -606,9 +606,12 @@
 ###############################################################################
 
 
-def sample_stds_by_stds_topology(intype, sampletype, inputs, sampler, header, separator, method, spatial=False):
+def sample_stds_by_stds_topology(intype, sampletype, inputs, sampler, header, 
+                                 separator, method, spatial=False, 
+                                 print_only=True):
     """!Sample the input space time datasets with a sample 
-       space time dataset and print the result to stdout
+       space time dataset, return the created map matrix and optionally 
+       print the result to stdout
 
         In case multiple maps are located in the current granule, 
         the map names are separated by comma.
@@ -616,20 +619,29 @@
         In case a layer is present, the names map ids are extended 
         in this form: name:layer at mapset
 
-        Attention: Do not use the comma as separator
+        Attention: Do not use the comma as separator for printing
 
         @param intype:  Type of the input space time dataset (strds, stvds or str3ds)
         @param samtype: Type of the sample space time dataset (strds, stvds or str3ds)
-        @param input: Name of a space time dataset
+        @param inputs: Name or comma separated names of space time datasets
         @param sampler: Name of a space time dataset used for temporal sampling
         @param header: Set True to print column names
         @param separator: The field separator character between the columns
         @param method: The method to be used for temporal sampling 
                        (start,during,contain,overlap,equal)
         @param spatial: Perform spatial overlapping check
+        @param print_only: If set True (default) then the result of the sampling will be 
+                    printed to stdout, if set to False the resulting map matrix 
+                    will be returned. 
+                    
+        @return The map matrix or None if nothing found
     """
     mapset = core.gisenv()["MAPSET"]
 
+    # Make a method list
+    method = method.split(",")
+
+    # Split the inputs
     input_list = inputs.split(",")
     sts = []
 
@@ -672,6 +684,11 @@
             mapmatrizes.append(mapmatrix)
 
     if len(mapmatrizes) > 0:
+        
+        # Simply return the map matrix
+        if not print_only:
+            dbif.close()
+            return mapmatrizes
 
         if header:
             string = ""
@@ -726,6 +743,10 @@
             print string
 
     dbif.close()
+    if len(mapmatrizes) > 0:
+        return mapmatrizes
+    
+    return None
 
 ###############################################################################
 
@@ -821,3 +842,60 @@
 
     return output
 
+###############################################################################
+
+def create_space_time_dataset(name, type, temporaltype, title, descr, semantic,
+                              dbif=None, overwrite=False):
+    """!Create a new space time dataset
+    
+       This function is sensitive to the settings in grass.core.overwrite to
+       overwrute existing space time datasets.
+    
+       @param name: The name of the new space time dataset
+       @param type: The type (strds, stvds, str3ds) of the new space time dataset
+       @param temporaltype: The temporal type (relative or absolute)
+       @param title: The title
+       @param descr: The dataset description
+       @param semantic: Semantical information
+       @param dbif: The temporal database interface to be used
+       
+       @return The new created space time dataset
+       
+       This function will raise a ScriptError in case of an error.
+    """
+    
+    #Get the current mapset to create the id of the space time dataset
+
+    mapset = core.gisenv()["MAPSET"]
+    id = name + "@" + mapset
+
+    sp = dataset_factory(type, id)
+
+    dbif, connect = init_dbif(dbif)
+
+    if sp.is_in_db(dbif) and overwrite == False:
+        if connect:
+            dbif.close()
+        core.fatal(_("Space time %s dataset <%s> is already in the database. "
+                      "Use the overwrite flag.") %
+                    (sp.get_new_map_instance(None).get_type(), name))
+        return None
+
+    if sp.is_in_db(dbif) and overwrite == True:
+        core.info(_("Overwrite space time %s dataset <%s> "
+                     "and unregister all maps.") %
+                   (sp.get_new_map_instance(None).get_type(), name))
+        sp.delete(dbif)
+        sp = sp.get_new_instance(id)
+
+    core.verbose(_("Create space time %s dataset.") %
+                  sp.get_new_map_instance(None).get_type())
+
+    sp.set_initial_values(temporal_type=temporaltype, semantic_type=semantic,
+                          title=title, description=descr)
+    sp.insert(dbif)
+
+    if connect:
+        dbif.close()
+        
+    return sp

Modified: grass/trunk/temporal/t.create/t.create.py
===================================================================
--- grass/trunk/temporal/t.create/t.create.py	2012-12-05 10:18:06 UTC (rev 54195)
+++ grass/trunk/temporal/t.create/t.create.py	2012-12-05 10:42:08 UTC (rev 54196)
@@ -75,38 +75,9 @@
 
     tgis.init()
     
-    #Get the current mapset to create the id of the space time dataset
+    tgis.create_space_time_dataset(name, type, temporaltype, title, descr, 
+                                   semantic, None, grass.overwrite())
 
-    mapset = grass.gisenv()["MAPSET"]
-    id = name + "@" + mapset
-
-    sp = tgis.dataset_factory(type, id)
-
-    dbif = tgis.SQLDatabaseInterfaceConnection()
-    dbif.connect()
-
-    if sp.is_in_db(dbif) and grass.overwrite() == False:
-        dbif.close()
-        grass.fatal(_("Space time %s dataset <%s> is already in the database. "
-                      "Use the overwrite flag.") %
-                    (sp.get_new_map_instance(None).get_type(), name))
-
-    if sp.is_in_db(dbif) and grass.overwrite() == True:
-        grass.info(_("Overwrite space time %s dataset <%s> "
-                     "and unregister all maps.") %
-                   (sp.get_new_map_instance(None).get_type(), name))
-        sp.delete(dbif)
-        sp = sp.get_new_instance(id)
-
-    grass.verbose(_("Create space time %s dataset.") %
-                  sp.get_new_map_instance(None).get_type())
-
-    sp.set_initial_values(temporal_type=temporaltype, semantic_type=semantic,
-                          title=title, description=descr)
-    sp.insert(dbif)
-
-    dbif.close()
-
 if __name__ == "__main__":
     options, flags = grass.parser()
     main()

Modified: grass/trunk/temporal/t.sample/t.sample.py
===================================================================
--- grass/trunk/temporal/t.sample/t.sample.py	2012-12-05 10:18:06 UTC (rev 54195)
+++ grass/trunk/temporal/t.sample/t.sample.py	2012-12-05 10:42:08 UTC (rev 54196)
@@ -77,14 +77,12 @@
     method = options["method"]
     header = flags["c"]
     spatial = flags["s"]
-
-    method = method.split(",")
-
+    
     # Make sure the temporal database exists
     tgis.init()
 
     tgis.sample_stds_by_stds_topology(intype, samtype, inputs, sampler,
-                                      header, separator, method, spatial)
+                                      header, separator, method, spatial, True)
 
 if __name__ == "__main__":
     options, flags = grass.parser()

Modified: grass/trunk/temporal/t.sample/test.t.sample.sh
===================================================================
--- grass/trunk/temporal/t.sample/test.t.sample.sh	2012-12-05 10:18:06 UTC (rev 54195)
+++ grass/trunk/temporal/t.sample/test.t.sample.sh	2012-12-05 10:42:08 UTC (rev 54196)
@@ -51,15 +51,18 @@
 EOF
 
 
-t.create --o type=strds temporaltype=absolute output=precip_abs0 title="A test with raster input files" descr="A test with raster input files"
-t.create --o type=stvds temporaltype=absolute output=pnts_abs0 title="A test with vector input files" descr="A test with vector input files"
-t.create --o type=stvds temporaltype=absolute output=pnts_abs1 title="A test with vector input files" descr="A test with vector input files"
+t.create --o type=strds temporaltype=absolute output=precip_abs0 \
+	title="A test with raster input files" descr="A test with raster input files"
+t.create --o type=stvds temporaltype=absolute output=pnts_abs0 \
+	title="A test with vector input files" descr="A test with vector input files"
+t.create --o type=stvds temporaltype=absolute output=pnts_abs1 \
+	title="A test with vector input files" descr="A test with vector input files"
 
-t.register type=rast -i input=precip_abs0 file="${n1}" start="2001-01-01" increment="1 months"
+t.register --o type=rast -i input=precip_abs0 file="${n1}" start="2001-01-01" increment="1 months"
 t.rast.list precip_abs0 -h
-t.register type=vect    input=pnts_abs0 file="${n2}" start=file end=file
+t.register --o type=vect    input=pnts_abs0 file="${n2}"
 t.vect.list pnts_abs0 -h
-t.register type=vect    input=pnts_abs1 file="${n3}" start=file end=file
+t.register --o type=vect    input=pnts_abs1 file="${n3}"
 t.vect.list pnts_abs1 -h
 
 # The @test
@@ -74,7 +77,7 @@
 
 
 # Test with temporal point data
-t.register type=rast    input=precip_abs0 file="${n1}" start="2001-01-01" increment="1 months"
+t.register --o type=rast input=precip_abs0 file="${n1}" start="2001-01-01" increment="1 months"
 t.rast.list precip_abs0 -h
 t.sample input=precip_abs0 samtype=stvds sample=pnts_abs0 -cs
 t.sample input=precip_abs0 samtype=stvds sample=pnts_abs1 -cs



More information about the grass-commit mailing list