[GRASS-SVN] r51923 - in grass/trunk: lib/python/temporal temporal temporal/t.rast.series temporal/t.vect.db.select temporal/t.vect.observe.strds

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jun 1 15:39:20 PDT 2012


Author: huhabla
Date: 2012-06-01 15:39:20 -0700 (Fri, 01 Jun 2012)
New Revision: 51923

Added:
   grass/trunk/temporal/t.vect.db.select/
   grass/trunk/temporal/t.vect.db.select/Makefile
   grass/trunk/temporal/t.vect.db.select/t.vect.db.select.html
   grass/trunk/temporal/t.vect.db.select/t.vect.db.select.py
   grass/trunk/temporal/t.vect.db.select/test.t.vect.db.select.sh
Modified:
   grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
   grass/trunk/lib/python/temporal/space_time_datasets_tools.py
   grass/trunk/temporal/t.rast.series/t.rast.series.py
   grass/trunk/temporal/t.rast.series/test.t.rast.series.sh
   grass/trunk/temporal/t.vect.observe.strds/t.vect.observe.strds.py
Log:
New module to print selected database columns from space time vector
datasets. Small bugfixes.


Modified: grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2012-06-01 22:09:42 UTC (rev 51922)
+++ grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2012-06-01 22:39:20 UTC (rev 51923)
@@ -799,9 +799,9 @@
 
         if not map.check_valid_time():
 	    if map.get_layer():
-		core.fatal(_("Map <%s> with layer %s has invalid time") % map.get_map_id(), map.get_layer())
+		core.fatal(_("Map <%s> with layer %s has invalid time") % (map.get_map_id(), map.get_layer()))
 	    else:
-		core.fatal(_("Map <%s> has invalid time") % map.get_map_id())
+		core.fatal(_("Map <%s> has invalid time") % (map.get_map_id()))
 
         map_id = map.base.get_id()
         map_name = map.base.get_name()

Modified: grass/trunk/lib/python/temporal/space_time_datasets_tools.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2012-06-01 22:09:42 UTC (rev 51922)
+++ grass/trunk/lib/python/temporal/space_time_datasets_tools.py	2012-06-01 22:39:20 UTC (rev 51923)
@@ -197,6 +197,8 @@
  
         else:
             is_in_db = True
+            if core.overwrite == False:
+		continue
             map.select(dbif)
             if name and map.get_temporal_type() != sp.get_temporal_type():
                 dbif.close()

Modified: grass/trunk/temporal/t.rast.series/t.rast.series.py
===================================================================
--- grass/trunk/temporal/t.rast.series/t.rast.series.py	2012-06-01 22:09:42 UTC (rev 51922)
+++ grass/trunk/temporal/t.rast.series/t.rast.series.py	2012-06-01 22:39:20 UTC (rev 51923)
@@ -51,7 +51,7 @@
 
 #%flag
 #% key: t
-#% description: Assign the space time raster dataset start and end time to the output map
+#% description: Do not assign the space time raster dataset start and end time to the output map
 #%end
 
 import grass.script as grass
@@ -100,7 +100,7 @@
 
         ret = grass.run_command("r.series", flags="z", file=filename, output=output, overwrite=grass.overwrite(), method=method)
 
-        if ret == 0 and add_time:
+        if ret == 0 and not add_time:
             if sp.is_time_absolute():
                 start_time, end_time, tz = sp.get_absolute_time()
             else:
@@ -123,7 +123,7 @@
 
             # Register the map in the temporal database
             if map.is_in_db():
-                map.update()
+                map.update_all()
             else:
                 map.insert()
 

Modified: grass/trunk/temporal/t.rast.series/test.t.rast.series.sh
===================================================================
--- grass/trunk/temporal/t.rast.series/test.t.rast.series.sh	2012-06-01 22:09:42 UTC (rev 51922)
+++ grass/trunk/temporal/t.rast.series/test.t.rast.series.sh	2012-06-01 22:39:20 UTC (rev 51923)
@@ -13,17 +13,23 @@
 r.mapcalc --o expr="prec_6 = 600"
 
 # @test
-t.create --v --o type=strds temporaltype=absolute output=precip_abs title="A test" descr="A test"
+t.create --o type=strds temporaltype=absolute output=precip_abs title="A test" descr="A test"
 
-t.register --v type=rast input=precip_abs maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="1 months"
+t.register type=rast input=precip_abs maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="1 months"
+t.rast.list precip_abs
 
-t.rast.series --o input=precip_abs method=average output=prec_average where="start_time > '2001-03-01'"
-t.rast.series --o -t input=precip_abs method=maximum output=prec_max order=start_time
-t.rast.series --o -t input=precip_abs method=sum output=prec_sum
+t.rast.series --o -t input=precip_abs method=average output=prec_average where="start_time > '2001-03-01'"
+t.rast.series --o    input=precip_abs method=maximum output=prec_max order=start_time
+t.rast.series        input=precip_abs method=sum output=prec_sum
+t.rast.series --o    input=precip_abs method=sum output=prec_sum
+# This test should raise an error
+t.rast.series        input=precip_abs method=sum output=prec_sum
 
-t.unregister --v type=rast maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
-t.remove --v type=strds input=precip_abs
+r.info prec_average
+r.info prec_max
+r.info prec_sum
 
-r.info prec_average
-t.info type=rast input=prec_max
-t.info type=rast input=prec_sum
+t.unregister type=rast maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6,prec_sum,prec_max
+t.remove type=strds input=precip_abs
+
+g.remove rast=prec_sum,prec_max,prec_average

Added: grass/trunk/temporal/t.vect.db.select/Makefile
===================================================================
--- grass/trunk/temporal/t.vect.db.select/Makefile	                        (rev 0)
+++ grass/trunk/temporal/t.vect.db.select/Makefile	2012-06-01 22:39:20 UTC (rev 51923)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = t.vect.db.select
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/t.vect.db.select/t.vect.db.select.html
===================================================================
Added: grass/trunk/temporal/t.vect.db.select/t.vect.db.select.py
===================================================================
--- grass/trunk/temporal/t.vect.db.select/t.vect.db.select.py	                        (rev 0)
+++ grass/trunk/temporal/t.vect.db.select/t.vect.db.select.py	2012-06-01 22:39:20 UTC (rev 51923)
@@ -0,0 +1,130 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	t.vect.db.select
+# AUTHOR(S):	Soeren Gebbert
+#
+# PURPOSE:	Prints attributes of vector maps registered in a space time vector dataset.
+# COPYRIGHT:	(C) 2011 by the GRASS Development Team
+#
+#		This program is free software under the GNU General Public
+#		License (version 2). Read the file COPYING that comes with GRASS
+#		for details.
+#
+#############################################################################
+
+#%module
+#% description: Prints attributes of vector maps registered in a space time vector dataset.
+#% keywords: temporal
+#% keywords: vector
+#% keywords: database
+#% keywords: select
+#%end
+
+#%option G_OPT_STVDS_INPUT
+#%end
+
+#%option G_OPT_DB_COLUMNS
+#%end
+
+#%option
+#% key: fs
+#% type: string
+#% description: Field separator character between the output columns
+#% required: no
+#% answer: |
+#%end
+
+#%option G_OPT_V_FIELD
+#%end
+
+#%option G_OPT_DB_WHERE
+#%end
+
+#%option G_OPT_T_WHERE
+#% key: t_where
+#%end
+
+import grass.script as grass
+import grass.temporal as tgis
+import grass.script.raster as raster
+
+############################################################################
+
+def main():
+
+    # Get the options
+    input = options["input"]
+    where = options["where"]
+    columns = options["columns"]
+    tempwhere = options["t_where"]
+    layer = options["layer"]
+    fs = options["fs"]
+
+    if where == "" or where == " " or where == "\n":
+        where = None
+
+    if columns == "" or columns == " " or columns == "\n":
+        columns = None
+
+    # Make sure the temporal database exists
+    tgis.create_temporal_database()
+    # We need a database interface
+    dbif = tgis.sql_database_interface_connection()
+    dbif.connect()
+   
+    mapset =  grass.gisenv()["MAPSET"]
+
+    if input.find("@") >= 0:
+        id = input
+    else:
+        id = input + "@" + mapset
+
+    sp = tgis.space_time_vector_dataset(id)
+    
+    if sp.is_in_db() == False:
+        dbif.close()
+        grass.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
+
+    sp.select(dbif)
+    
+    rows = sp.get_registered_maps("name,layer,mapset,start_time,end_time", tempwhere, "start_time", dbif)
+
+    col_names = ""
+    if rows:
+	for row in rows:
+	    vector_name = "%s@%s"%(row["name"], row["mapset"])
+	    # In case a layer is defined in the vector dataset, we override the option layer
+	    if row["layer"]:
+		layer = row["layer"]
+	    
+	    select = grass.read_command("v.db.select", map=vector_name, layer=layer, columns=columns, fs="%s"%(fs), where=where)
+	    
+	    if not select:
+		dbif.close()
+		grass.fatal(_("Unable to run v.db.select for vector map <%s> with layer %s")%(vector_name, layer))
+	    # The first line are the column names
+	    list = select.split("\n")
+	    count = 0
+	    for entry in list:
+	        if entry.strip() != "":
+		    # print the column names in case they change
+		    if count == 0:
+			col_names_new = "start_time%send_time%s%s"%(fs, fs, entry)
+			if col_names != col_names_new:
+			    col_names = col_names_new
+			    print col_names
+		    else:
+			if row["end_time"]:
+			    print "%s%s%s%s%s"%(row["start_time"],fs, row["end_time"], fs, entry)
+			else:
+			    print "%s%s%s%s"%(row["start_time"], fs, fs, entry)
+		    count += 1
+	    
+    dbif.close()
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()
+


Property changes on: grass/trunk/temporal/t.vect.db.select/t.vect.db.select.py
___________________________________________________________________
Added: svn:executable
   + *

Added: grass/trunk/temporal/t.vect.db.select/test.t.vect.db.select.sh
===================================================================
--- grass/trunk/temporal/t.vect.db.select/test.t.vect.db.select.sh	                        (rev 0)
+++ grass/trunk/temporal/t.vect.db.select/test.t.vect.db.select.sh	2012-06-01 22:39:20 UTC (rev 51923)
@@ -0,0 +1,43 @@
+#!/bin/sh
+# Test the database output of vector maps in a space time vector dataset
+# We need to set a specific region in the
+# @preprocess step of this test. 
+# The region setting should work for UTM and LL test locations
+g.region s=0 n=80 w=0 e=120 b=0 t=50 res=10 res3=10 -p3
+
+r.mapcalc --o expr="prec_1 = 100.0"
+r.mapcalc --o expr="prec_2 = 200.0"
+r.mapcalc --o expr="prec_3 = 300"
+r.mapcalc --o expr="prec_4 = 400"
+r.mapcalc --o expr="prec_5 = 500.0"
+r.mapcalc --o expr="prec_6 = 600.0"
+
+v.random --o -z output=prec n=5 seed=1
+
+t.create --o type=strds temporaltype=absolute output=precip_abs1 title="A test" descr="A test"
+t.register -i input=precip_abs1 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-03-01 00:00:00" increment="1 months"
+
+# The @test
+# We need to create a nice space time vector dataset with layers for testing
+t.vect.observe.strds input=prec strds=precip_abs1 output=prec_observer column=observation
+t.vect.list input=prec_observer
+t.vect.db.select input=prec_observer
+t.vect.db.select input=prec_observer where="cat = 1" fs="  |  "
+t.vect.db.select input=prec_observer where="cat = 1" fs="  |  " columns=observation
+
+# Time instances
+t.register --o input=precip_abs1 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2004-01-01 00:00:00" increment="3 months"
+
+t.vect.observe.strds --o input=prec strds=precip_abs1 output=prec_observer
+t.vect.list input=prec_observer
+t.vect.db.select input=prec_observer
+t.vect.db.select input=prec_observer where="cat = 1" fs="  |  "
+
+# @postprocess
+t.unregister type=rast maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
+t.remove type=strds input=precip_abs1
+t.remove type=stvds input=prec_observer
+t.unregister type=vect maps=prec_observer:1,prec_observer:2,prec_observer:3,prec_observer:4,prec_observer:5,prec_observer:6
+
+g.remove vect=prec_observer,test_extract
+g.remove rast=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6


Property changes on: grass/trunk/temporal/t.vect.db.select/test.t.vect.db.select.sh
___________________________________________________________________
Added: svn:executable
   + *

Modified: grass/trunk/temporal/t.vect.observe.strds/t.vect.observe.strds.py
===================================================================
--- grass/trunk/temporal/t.vect.observe.strds/t.vect.observe.strds.py	2012-06-01 22:09:42 UTC (rev 51922)
+++ grass/trunk/temporal/t.vect.observe.strds/t.vect.observe.strds.py	2012-06-01 22:39:20 UTC (rev 51923)
@@ -189,7 +189,7 @@
             vect.set_relative_time(start, end, strds_ds.get_relative_time_unit())
        
         if vect.is_in_db(dbif):
-            vect.update(dbif)
+            vect.update_all(dbif)
         else:
             vect.insert(dbif)
 



More information about the grass-commit mailing list