[GRASS-SVN] r48289 - in grass/trunk/temporal: . t.create t.list t.remove tr.register tr.unregister

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Sep 13 21:06:44 EDT 2011


Author: huhabla
Date: 2011-09-13 18:06:44 -0700 (Tue, 13 Sep 2011)
New Revision: 48289

Added:
   grass/trunk/temporal/t.list/
   grass/trunk/temporal/t.list/Makefile
   grass/trunk/temporal/t.list/t.list.html
   grass/trunk/temporal/t.list/t.list.py
   grass/trunk/temporal/tr.register/
   grass/trunk/temporal/tr.register/Makefile
   grass/trunk/temporal/tr.register/tr.register.html
   grass/trunk/temporal/tr.register/tr.register.py
   grass/trunk/temporal/tr.unregister/
   grass/trunk/temporal/tr.unregister/Makefile
   grass/trunk/temporal/tr.unregister/tr.unregister.html
   grass/trunk/temporal/tr.unregister/tr.unregister.py
Modified:
   grass/trunk/temporal/t.create/t.create.py
   grass/trunk/temporal/t.remove/t.remove.py
Log:
New modules for temporal dataset handling and management.


Modified: grass/trunk/temporal/t.create/t.create.py
===================================================================
--- grass/trunk/temporal/t.create/t.create.py	2011-09-14 01:00:28 UTC (rev 48288)
+++ grass/trunk/temporal/t.create/t.create.py	2011-09-14 01:06:44 UTC (rev 48289)
@@ -5,7 +5,7 @@
 # MODULE:	t.create
 # AUTHOR(S):	Soeren Gebbert
 #               
-# PURPOSE:	Create a space-time dataset
+# PURPOSE:	Create a space time dataset
 # COPYRIGHT:	(C) 2011 by the GRASS Development Team
 #
 #		This program is free software under the GNU General Public
@@ -15,15 +15,16 @@
 #############################################################################
 
 #%module
-#% description: Create a space-time dataset
-#% keywords: spacetime dataset
+#% description: Create a space time dataset
+#% keywords: spacetime
+#% keywords: dataset
 #% keywords: create
 #%end
 
 #%option
-#% key: name
+#% key: dataset
 #% type: string
-#% description: Name of the new space-time dataset
+#% description: Name of the new space time dataset
 #% required: yes
 #% multiple: no
 #%end
@@ -31,7 +32,7 @@
 #%option
 #% key: granularity
 #% type: string
-#% description: The granularity of the new space-time dataset (NNN day, NNN week, NNN month)
+#% description: The granularity of the new space time dataset (NNN day, NNN week, NNN month)
 #% required: yes
 #% multiple: no
 #%end
@@ -39,7 +40,7 @@
 #%option
 #% key: semantictype
 #% type: string
-#% description: The semantic type of the space-time dataset
+#% description: The semantic type of the space time dataset
 #% required: yes
 #% multiple: no
 #% options: event, const, continuous
@@ -51,9 +52,10 @@
 #% type: string
 #% description: Type of the space time dataset, default is strds
 #% required: no
-#% options: strds
+#% options: strds, str3ds, stvds
 #% answer: strds
 #%end
+
 #%option
 #% key: temporaltype
 #% type: string
@@ -66,7 +68,7 @@
 #%option
 #% key: title
 #% type: string
-#% description: Title of the new space-time dataset
+#% description: Title of the new space time dataset
 #% required: yes
 #% multiple: no
 #%end
@@ -74,22 +76,19 @@
 #%option
 #% key: description
 #% type: string
-#% description: Description of the new space-time dataset
+#% description: Description of the new space time dataset
 #% required: yes
 #% multiple: no
 #%end
 
-import sys
-import os
-import getpass
-import subprocess
 import grass.script as grass
+
 ############################################################################
 
 def main():
 
     # Get the options
-    name = options["name"]
+    name = options["dataset"]
     type = options["type"]
     temporaltype = options["temporaltype"]
     title = options["title"]
@@ -97,6 +96,33 @@
     semantic = options["semantictype"]
     gran = options["granularity"]
 
+    # Make sure the temporal database exists
+    grass.create_temporal_database()
+
+    #Get the current mapset to create the id of the space time dataset
+
+    mapset =  grass.gisenv()["MAPSET"]
+    id = name + "@" + mapset
+
+    if type == "strds":
+        sp = grass.space_time_raster_dataset(id)
+    if type == "str3ds":
+        sp = grass.space_time_raster3d_dataset(id)
+    if type == "stvds":
+        sp = grass.space_time_vector_dataset(id)
+
+    if sp.is_in_db() and grass.overwrite() == False:
+        grass.fatal("Space time " + sp.get_new_map_instance(None).get_type() + " dataset <" + name + "> is already in the database. Use the overwrite flag.")
+
+    if sp.is_in_db() and grass.overwrite() == True:
+        grass.info("Overwrite space time " + sp.get_new_map_instance(None).get_type() + " dataset <" + name + "> and unregister all maps.")
+        sp.delete()
+
+    grass.info("Create space time " + sp.get_new_map_instance(None).get_type() + " dataset.")
+
+    sp.set_initial_values(granularity=gran, temporal_type=temporaltype, semantic_type=semantic, title=title, description=descr)
+    sp.insert()
+
 if __name__ == "__main__":
     options, flags = grass.core.parser()
     main()

Added: grass/trunk/temporal/t.list/Makefile
===================================================================
--- grass/trunk/temporal/t.list/Makefile	                        (rev 0)
+++ grass/trunk/temporal/t.list/Makefile	2011-09-14 01:06:44 UTC (rev 48289)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = t.list
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/t.list/t.list.html
===================================================================
Added: grass/trunk/temporal/t.list/t.list.py
===================================================================
--- grass/trunk/temporal/t.list/t.list.py	                        (rev 0)
+++ grass/trunk/temporal/t.list/t.list.py	2011-09-14 01:06:44 UTC (rev 48289)
@@ -0,0 +1,172 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	t.list
+# AUTHOR(S):	Soeren Gebbert
+#               
+# PURPOSE:	List space time datasets
+# 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: List space time and map datasets
+#% keywords: dataset
+#% keywords: spacetime
+#% keywords: list
+#%end
+
+#%option
+#% key: type
+#% type: string
+#% description: Type of the space time dataset, default is strds
+#% required: no
+#% options: strds, str3ds, stvds, raster, raster3d, vector
+#% answer: strds
+#%end
+
+#%option
+#% key: sort
+#% type: string
+#% description: Sort the space time dataset by category
+#% required: no
+#% multiple: yes
+#% options: id, name, creator, mapset, number_of_maps, creation_time, modification_time, start_time, end_time, interval, north, south, west, east, granularity
+#% answer: id
+#%end
+
+#%option
+#% key: columns
+#% type: string
+#% description: Which columns should be printed to stdout
+#% required: no
+#% multiple: yes
+#% options: id, name, creator, mapset, number_of_maps, creation_time, modification_time, start_time, end_time, interval, north, south, west, east, granularity, all
+#% answer: id
+#%end
+
+#%option
+#% key: where
+#% type: string
+#% description: A where statement for selected listing e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
+#% required: no
+#% multiple: no
+#%end
+
+#%option
+#% key: temporaltype
+#% type: string
+#% description: The temporal type of the space time dataset, default is absolute
+#% required: no
+#% options: absolute,relative
+#% answer: absolute
+#%end
+
+#%option
+#% key: fs
+#% type: string
+#% description: The field separator character between the columns, default is tabular "\t"
+#% required: no
+#%end
+
+#%flag
+#% key: c
+#% description: Print the column names as first row
+#%end
+
+import grass.script as grass
+
+############################################################################
+
+def main():
+
+    # Get the options
+    type = options["type"]
+    temporaltype = options["temporaltype"]
+    columns = options["columns"]
+    sort = options["sort"]
+    where = options["where"]
+    separator = options["fs"]
+    colhead = flags['c']
+
+    # Make sure the temporal database exists
+    grass.create_temporal_database()
+
+    id = None
+
+    if type == "strds":
+        sp = grass.space_time_raster_dataset(id)
+    if type == "str3ds":
+        sp = grass.space_time_raster3d_dataset(id)
+    if type == "stvds":
+        sp = grass.space_time_vector_dataset(id)
+    if type == "raster":
+        sp = grass.raster_dataset(id)
+    if type == "raster3d":
+        sp = grass.raster3d_dataset(id)
+    if type == "vector":
+        sp = grass.vector_dataset(id)
+        
+    # Insert content from db
+    sp.select()
+
+    # Create the sql selection statement
+    # Table name
+    if temporaltype == "absolute":
+        table = sp.get_type() + "_view_abs_time"
+    else:
+        table = sp.get_type() + "_view_rel_time"
+
+    if columns.find("all") == -1:
+        sql = "SELECT " + str(columns) + " FROM " + table
+    else:
+        sql = "SELECT * FROM " + table
+
+    if sort:
+        sql += " ORDER BY " + sort
+
+    if where:
+        sql += " WHERE " + where
+
+    sp.base.connect()
+    sp.base.cursor.execute(sql)
+    rows = sp.base.cursor.fetchall()
+    sp.base.close()
+
+    # Print the query result to stout
+    if rows:
+        if separator == None or separator == "":
+            separator = "\t"
+
+        # Print the column names if requested
+        if colhead == True:
+            output = ""
+            count = 0
+            for key in rows[0].keys():
+                if count > 0:
+                    output += separator + str(key)
+                else:
+                    output += str(key)
+                count += 1
+            print output
+
+        for row in rows:
+            output = ""
+            count = 0
+            for col in row:
+                if count > 0:
+                    output += separator + str(col)
+                else:
+                    output += str(col)
+                count += 1
+                
+            print output
+
+if __name__ == "__main__":
+    options, flags = grass.core.parser()
+    main()


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

Modified: grass/trunk/temporal/t.remove/t.remove.py
===================================================================
--- grass/trunk/temporal/t.remove/t.remove.py	2011-09-14 01:00:28 UTC (rev 48288)
+++ grass/trunk/temporal/t.remove/t.remove.py	2011-09-14 01:06:44 UTC (rev 48289)
@@ -5,7 +5,7 @@
 # MODULE:	t.remove
 # AUTHOR(S):	Soeren Gebbert
 #               
-# PURPOSE:	Remove a space-time raster dataset
+# PURPOSE:	Remove a space time raster dataset
 # COPYRIGHT:	(C) 2011 by the GRASS Development Team
 #
 #		This program is free software under the GNU General Public
@@ -15,39 +15,65 @@
 #############################################################################
 
 #%module
-#% description: Remove a space-time dataset
-#% keywords: spacetime dataset
+#% description: Remove a space time and map datasets from temporal database
+#% keywords: spacetime
+#% keywords: dataset
 #% keywords: remove
 #%end
 
 #%option
-#% key: name
+#% key: dataset
 #% type: string
-#% description: Name of the new space-time dataset
+#% description: Name of the new space time or map dataset
 #% required: yes
 #% multiple: no
 #%end
+
 #%option
 #% key: type
 #% type: string
 #% description: Type of the space time dataset, default is strds
 #% required: no
-#% options: strds
+#% options: strds, str3ds, stvds, raster, raster3d, vector
 #% answer: strds
 #%end
 
-import sys
-import os
 import grass.script as grass
 
 ############################################################################
 
 def main():
-
+    
     # Get the options
-    name = options["name"]
+    name = options["dataset"]
     type = options["type"]
 
+    # Make sure the temporal database exists
+    grass.create_temporal_database()
+    
+    mapset =  grass.gisenv()["MAPSET"]
+    id = name + "@" + mapset
+
+    if type == "strds":
+        sp = grass.space_time_raster_dataset(id)
+    if type == "str3ds":
+        sp = grass.space_time_raster3d_dataset(id)
+    if type == "stvds":
+        sp = grass.space_time_vector_dataset(id)
+    if type == "raster":
+        sp = grass.raster_dataset(id)
+    if type == "raster3d":
+        sp = grass.raster3d_dataset(id)
+    if type == "vector":
+        sp = grass.vector_dataset(id)
+
+    if sp.is_in_db() == False:
+        grass.fatal("Dataset <" + name + "> not found in temporal database")
+
+    # Insert content from db
+    sp.select()
+    sp.delete()
+
 if __name__ == "__main__":
     options, flags = grass.core.parser()
     main()

Added: grass/trunk/temporal/tr.register/Makefile
===================================================================
--- grass/trunk/temporal/tr.register/Makefile	                        (rev 0)
+++ grass/trunk/temporal/tr.register/Makefile	2011-09-14 01:06:44 UTC (rev 48289)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = tr.register
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/tr.register/tr.register.html
===================================================================
Added: grass/trunk/temporal/tr.register/tr.register.py
===================================================================
--- grass/trunk/temporal/tr.register/tr.register.py	                        (rev 0)
+++ grass/trunk/temporal/tr.register/tr.register.py	2011-09-14 01:06:44 UTC (rev 48289)
@@ -0,0 +1,148 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	tr.register
+# AUTHOR(S):	Soeren Gebbert
+#
+# PURPOSE:	Register a raster map in a space time raster 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: Register raster maps in a space time raster dataset
+#% keywords: spacetime raster dataset
+#% keywords: raster
+#%end
+
+#%option
+#% key: dataset
+#% type: string
+#% description: Name of an existing space time raster dataset
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: maps
+#% type: string
+#% description: Name(s) of existing raster map(s), multiple maps must be provided in temporal order in case datetime should be attached
+#% required: yes
+#% multiple: yes
+#%end
+
+#%option
+#% key: start
+#% type: string
+#% description: The start date and time of the first raster map, in case the map has no date (format absolute: "yyyy-mm-dd HH:MM:SS", relative 5.0)
+#% required: no
+#% multiple: no
+#%end
+
+#%option
+#% key: increment
+#% type: string
+#% description: Time increment between maps for time stamp creation (NNN seconds, minutes, hours, days, weeks)
+#% required: no
+#% multiple: no
+#%end
+
+import datetime
+from datetime import timedelta
+import time
+import grass.script as grass
+#######################
+#####################################################
+
+def main():
+
+    # Get the options
+    name = options["dataset"]
+    maps = options["maps"]
+    start = options["start"]
+    increment = options["increment"]
+
+    mapset =  grass.gisenv()["MAPSET"]
+    id = name + "@" + mapset
+
+    sp = grass.space_time_raster_dataset(id)
+    # Insert content from db
+    sp.select()
+    
+    if sp.is_in_db() == False:
+        grass.fatal("Space time " + sp.get_new_map_instance(None).get_type() + " dataset <" + name + "> not found")
+
+    if maps.find(",") == -1:
+        maplist = (maps,)
+    else:
+        maplist = tuple(maps.split(","))
+
+    if increment:
+        if sp.is_time_absolute():
+            inc_value = float(increment.split(" ")[0])
+            inc_unit = increment.split(" ")[1]
+        else:
+            inc_value = float(increment)
+
+    count = 0
+    for mapname in maplist:
+        mapid = mapname + "@" + mapset
+        map = grass.raster_dataset(mapid)
+        map.load()
+
+        # In case the map is already registered print a message and continue to the next map
+
+        
+        # Put the map into the database
+        if map.is_in_db() == False:
+
+            # Set the time interval
+            if start:
+                grass.info("Set time interval for map " + mapname)
+                if sp.is_time_absolute():
+                    
+                    if start.find(":") > 0:
+                        time_format = "%Y-%m-%d %H:%M:%S"
+                    else:
+                        time_format = "%Y-%m-%d"
+
+                    start_time = datetime.datetime.fromtimestamp(time.mktime(time.strptime(start, time_format)))
+                    end_time = None
+
+                    if increment:
+                        if inc_unit.find("seconds") >= 0:
+                            tdelta = timedelta(seconds=inc_value)
+                        elif inc_unit.find("minutes") >= 0:
+                            tdelta = timedelta(minutes=inc_value)
+                        elif inc_unit.find("hours") >= 0:
+                            tdelta = timedelta(hours=inc_value)
+                        elif inc_unit.find("days") >= 0:
+                            tdelta = timedelta(days=inc_value)
+                        elif inc_unit.find("weeks") >= 0:
+                            tdelta = timedelta(weeks=inc_value)
+                        else:
+                            grass.fatal("Wrong increment format: " + increment)
+
+                        start_time += count * tdelta
+                        end_time = start_time + tdelta
+
+                    map.set_absolute_time(start_time, end_time)
+                else:
+                    interval = float(start) + count * inc_value
+                    map.set_relative_time(interval)
+            # Put map with time interval in the database
+            map.insert()
+            
+        # Register map
+        sp.register_map(map)
+        count += 1
+
+if __name__ == "__main__":
+    options, flags = grass.core.parser()
+    main()
+


Property changes on: grass/trunk/temporal/tr.register/tr.register.py
___________________________________________________________________
Added: svn:executable
   + *

Added: grass/trunk/temporal/tr.unregister/Makefile
===================================================================
--- grass/trunk/temporal/tr.unregister/Makefile	                        (rev 0)
+++ grass/trunk/temporal/tr.unregister/Makefile	2011-09-14 01:06:44 UTC (rev 48289)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = tr.unregister
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/tr.unregister/tr.unregister.html
===================================================================
Added: grass/trunk/temporal/tr.unregister/tr.unregister.py
===================================================================
--- grass/trunk/temporal/tr.unregister/tr.unregister.py	                        (rev 0)
+++ grass/trunk/temporal/tr.unregister/tr.unregister.py	2011-09-14 01:06:44 UTC (rev 48289)
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	tr.unregister
+# AUTHOR(S):	Soeren Gebbert
+#               
+# PURPOSE:	Unregister raster map a space time raster 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: Register raster maps in a space time raster dataset
+#% keywords: spacetime raster dataset
+#% keywords: raster
+#%end
+
+#%option
+#% key: input
+#% type: string
+#% description: Name of an existing space time raster dataset
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: maps
+#% type: string
+#% description: Name(s) of existing raster map(s) to unregister
+#% required: yes
+#% multiple: yes
+#%end
+
+
+import sys
+import os
+import getpass
+import subprocess
+import grass.script as grass
+############################################################################
+
+def main():
+
+    # Get the options
+    input = options["input"]
+    maps = options["maps"]
+
+if __name__ == "__main__":
+    options, flags = grass.core.parser()
+    main()
+


Property changes on: grass/trunk/temporal/tr.unregister/tr.unregister.py
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list