[GRASS-SVN] r58381 - grass/trunk/lib/python/temporal
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Dec 3 16:55:31 PST 2013
Author: huhabla
Date: 2013-12-03 16:55:30 -0800 (Tue, 03 Dec 2013)
New Revision: 58381
Added:
grass/trunk/lib/python/temporal/list_stds.py
grass/trunk/lib/python/temporal/open_stds.py
grass/trunk/lib/python/temporal/unittests_register.py
Removed:
grass/trunk/lib/python/temporal/list.py
grass/trunk/lib/python/temporal/open.py
Modified:
grass/trunk/lib/python/temporal/Makefile
grass/trunk/lib/python/temporal/__init__.py
grass/trunk/lib/python/temporal/c_libraries_interface.py
grass/trunk/lib/python/temporal/datetime_math.py
grass/trunk/lib/python/temporal/extract.py
grass/trunk/lib/python/temporal/register.py
grass/trunk/lib/python/temporal/stds_export.py
grass/trunk/lib/python/temporal/univar_statistics.py
Log:
Better Python file naming. New unittests to test the map register function
in register.py. Fixes for map registration bugs with relative time
reported by Vaclav.
Modified: grass/trunk/lib/python/temporal/Makefile
===================================================================
--- grass/trunk/lib/python/temporal/Makefile 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/Makefile 2013-12-04 00:55:30 UTC (rev 58381)
@@ -8,7 +8,7 @@
GDIR = $(PYDIR)/grass
DSTDIR = $(GDIR)/temporal
-MODULES = base core abstract_dataset abstract_map_dataset abstract_space_time_dataset space_time_datasets open factory gui_support list register sampling metadata spatial_extent temporal_extent datetime_math temporal_granularity spatio_temporal_relationships unit_tests aggregation stds_export stds_import extract mapcalc univar_statistics temporal_topology_dataset_connector spatial_topology_dataset_connector c_libraries_interface
+MODULES = base core abstract_dataset abstract_map_dataset abstract_space_time_dataset space_time_datasets open_stds factory gui_support list_stds register sampling metadata spatial_extent temporal_extent datetime_math temporal_granularity spatio_temporal_relationships unit_tests aggregation stds_export stds_import extract mapcalc univar_statistics temporal_topology_dataset_connector spatial_topology_dataset_connector c_libraries_interface
PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)
Modified: grass/trunk/lib/python/temporal/__init__.py
===================================================================
--- grass/trunk/lib/python/temporal/__init__.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/__init__.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -12,10 +12,10 @@
from datetime_math import *
from temporal_granularity import *
from spatio_temporal_relationships import *
-from open import *
+from open_stds import *
from factory import *
from gui_support import *
-from list import *
+from list_stds import *
from register import *
from sampling import *
from aggregation import *
Modified: grass/trunk/lib/python/temporal/c_libraries_interface.py
===================================================================
--- grass/trunk/lib/python/temporal/c_libraries_interface.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/c_libraries_interface.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -549,11 +549,11 @@
unit = "hours"
start = dt1.hour
elif dt1.minute > 0:
- unit = "minutess"
- start = dt1.minutes
- elif dt1.seconds > 0:
+ unit = "minutes"
+ start = dt1.minute
+ elif dt1.second > 0:
unit = "seconds"
- start = dt1.seconds
+ start = dt1.second
if count == 2:
if dt2.year > 0:
end = dt2.year
@@ -564,9 +564,9 @@
elif dt2.hour > 0:
end = dt2.hour
elif dt2.minute > 0:
- end = dt2.minutes
- elif dt2.seconds > 0:
- end = dt2.seconds
+ end = dt2.minute
+ elif dt2.second > 0:
+ end = dt2.second
return (start, end, unit)
###############################################################################
@@ -635,11 +635,11 @@
0
>>> grass.run_command("v.random", output="test", n=10, overwrite=True, quiet=True)
0
- >>> grass.run_command("r.timestamp", map="test", date='12 Mar 1995', overwrite=True, quiet=True)
+ >>> grass.run_command("r.timestamp", map="test", date='12 Mar 1995 10:34:40', overwrite=True, quiet=True)
0
- >>> grass.run_command("r3.timestamp", map="test", date='12 Mar 1995', overwrite=True, quiet=True)
+ >>> grass.run_command("r3.timestamp", map="test", date='12 Mar 1995 10:34:40', overwrite=True, quiet=True)
0
- >>> grass.run_command("v.timestamp", map="test", date='12 Mar 1995', overwrite=True, quiet=True)
+ >>> grass.run_command("v.timestamp", map="test", date='12 Mar 1995 10:34:40', overwrite=True, quiet=True)
0
@@ -658,11 +658,11 @@
... if res[0]:
... print str(res[1][0]), str(res[1][0])
... ciface.remove_raster_timestamp("test", tgis.get_current_mapset())
- 1995-03-12 00:00:00 1995-03-12 00:00:00
+ 1995-03-12 10:34:40 1995-03-12 10:34:40
1
>>> ciface.has_raster_timestamp("test", tgis.get_current_mapset())
False
- >>> ciface.write_raster_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999")
+ >>> ciface.write_raster_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999 14:30:05")
1
>>> ciface.has_raster_timestamp("test", tgis.get_current_mapset())
True
@@ -682,11 +682,11 @@
... if res[0]:
... print str(res[1][0]), str(res[1][0])
... ciface.remove_raster3d_timestamp("test", tgis.get_current_mapset())
- 1995-03-12 00:00:00 1995-03-12 00:00:00
+ 1995-03-12 10:34:40 1995-03-12 10:34:40
1
>>> ciface.has_raster3d_timestamp("test", tgis.get_current_mapset())
False
- >>> ciface.write_raster3d_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999")
+ >>> ciface.write_raster3d_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999 14:30:05")
1
>>> ciface.has_raster3d_timestamp("test", tgis.get_current_mapset())
True
@@ -707,11 +707,11 @@
... if res[0]:
... print str(res[1][0]), str(res[1][0])
... ciface.remove_vector_timestamp("test", tgis.get_current_mapset())
- 1995-03-12 00:00:00 1995-03-12 00:00:00
+ 1995-03-12 10:34:40 1995-03-12 10:34:40
1
>>> ciface.has_vector_timestamp("test", tgis.get_current_mapset())
False
- >>> ciface.write_vector_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999")
+ >>> ciface.write_vector_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999 14:30:05")
1
>>> ciface.has_vector_timestamp("test", tgis.get_current_mapset())
True
Modified: grass/trunk/lib/python/temporal/datetime_math.py
===================================================================
--- grass/trunk/lib/python/temporal/datetime_math.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/datetime_math.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -737,8 +737,13 @@
Time zones are not supported
@param time_string The time string to convert
- @return datetime object or None in case of an error
+ @return datetime object or None in case the string
+ could not be converted
"""
+
+ if not isinstance(time_string, str):
+ return None
+
time_object = check_datetime_string(time_string)
if not isinstance(time_object, datetime):
core.error(time_object)
Modified: grass/trunk/lib/python/temporal/extract.py
===================================================================
--- grass/trunk/lib/python/temporal/extract.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/extract.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -13,7 +13,7 @@
"""
from space_time_datasets import *
-from open import *
+from open_stds import *
from multiprocessing import Process
############################################################################
Deleted: grass/trunk/lib/python/temporal/list.py
===================================================================
--- grass/trunk/lib/python/temporal/list.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/list.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -1,186 +0,0 @@
-"""!@package grass.temporal
-
- at brief GRASS Python scripting module (temporal GIS functions)
-
-Temporal GIS related functions to be used in Python scripts.
-
-Usage:
-
- at code
-import grass.temporal as tgis
-
-tgis.register_maps_in_space_time_dataset(type, name, maps)
-
-...
- at endcode
-
-(C) 2008-2011 by the GRASS Development Team
-This program is free software under the GNU General Public
-License (>=v2). Read the file COPYING that comes with GRASS
-for details.
-
- at author Soeren Gebbert
-"""
-
-from space_time_datasets import *
-from factory import *
-from open import *
-
-###############################################################################
-
-def list_maps_of_stds(type, input, columns, order, where, separator, method, header, gran=None):
- """! List the maps of a space time dataset using diffetent methods
-
- @param type The type of the maps raster, raster3d or vector
- @param input Name of a space time raster dataset
- @param columns A comma separated list of columns to be printed to stdout
- @param order A comma separated list of columns to order the
- space time dataset by category
- @param where A where statement for selected listing without "WHERE"
- e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
- @param separator The field separator character between the columns
- @param method String identifier to select a method out of cols,
- comma,delta or deltagaps
- - "cols" Print preselected columns specified by columns
- - "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 additional 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
- @param gran The user defined granule to be used if method=gran is set, in case gran=None the
- granule of the space time dataset is used
- """
-
- dbif, connected = init_dbif(None)
-
- sp = open_old_space_time_dataset(input, type, dbif)
-
- if separator is None or separator == "":
- separator = "\t"
-
- # This method expects a list of objects for gap detection
- if method == "delta" or method == "deltagaps" or method == "gran":
- if type == "stvds":
- columns = "id,name,layer,mapset,start_time,end_time"
- else:
- columns = "id,name,mapset,start_time,end_time"
- if method == "deltagaps":
- maps = sp.get_registered_maps_as_objects_with_gaps(where=where, dbif=dbif)
- elif method == "delta":
- maps = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif)
- elif method == "gran":
- if gran is not None and gran != "":
- maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran, dbif=dbif)
- else:
- maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
-
- if header:
- string = ""
- string += "%s%s" % ("id", separator)
- string += "%s%s" % ("name", separator)
- if type == "stvds":
- string += "%s%s" % ("layer", separator)
- string += "%s%s" % ("mapset", separator)
- string += "%s%s" % ("start_time", separator)
- string += "%s%s" % ("end_time", separator)
- string += "%s%s" % ("interval_length", separator)
- string += "%s" % ("distance_from_begin")
- print string
-
- if maps and len(maps) > 0:
-
- if isinstance(maps[0], list):
- if len(maps[0]) > 0:
- first_time, dummy = maps[0][0].get_temporal_extent_as_tuple()
- else:
- core.warning(_("Empty map list."))
- return
- else:
- first_time, dummy = maps[0].get_temporal_extent_as_tuple()
-
- for mymap in maps:
-
- if isinstance(mymap, list):
- if len(mymap) > 0:
- map = mymap[0]
- else:
- core.fatal(_("Empty entry in map list, this should not happen."))
- else:
- map = mymap
-
- start, end = map.get_temporal_extent_as_tuple()
- if end:
- delta = end - start
- else:
- delta = None
- delta_first = start - first_time
-
- if map.is_time_absolute():
- if end:
- delta = time_delta_to_relative_time(delta)
- delta_first = time_delta_to_relative_time(delta_first)
-
- string = ""
- string += "%s%s" % (map.get_id(), separator)
- string += "%s%s" % (map.get_name(), separator)
- if type == "stvds":
- string += "%s%s" % (map.get_layer(), separator)
- string += "%s%s" % (map.get_mapset(), separator)
- string += "%s%s" % (start, separator)
- string += "%s%s" % (end, separator)
- string += "%s%s" % (delta, separator)
- string += "%s" % (delta_first)
- print string
-
- else:
- # In comma separated mode only map ids are needed
- if method == "comma":
- columns = "id"
-
- rows = sp.get_registered_maps(columns, where, order, dbif)
-
- if rows:
- if method == "comma":
- string = ""
- count = 0
- for row in rows:
- if count == 0:
- string += row["id"]
- else:
- string += ",%s" % row["id"]
- count += 1
- print string
-
- elif method == "cols":
- # Print the column names if requested
- if header:
- output = ""
- count = 0
-
- collist = columns.split(",")
-
- for key in collist:
- 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 connected:
- dbif.close()
Copied: grass/trunk/lib/python/temporal/list_stds.py (from rev 58380, grass/trunk/lib/python/temporal/list.py)
===================================================================
--- grass/trunk/lib/python/temporal/list_stds.py (rev 0)
+++ grass/trunk/lib/python/temporal/list_stds.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -0,0 +1,186 @@
+"""!@package grass.temporal
+
+ at brief GRASS Python scripting module (temporal GIS functions)
+
+Temporal GIS related functions to be used in Python scripts.
+
+Usage:
+
+ at code
+import grass.temporal as tgis
+
+tgis.register_maps_in_space_time_dataset(type, name, maps)
+
+...
+ at endcode
+
+(C) 2008-2011 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+ at author Soeren Gebbert
+"""
+
+from space_time_datasets import *
+from factory import *
+from open_stds import *
+
+###############################################################################
+
+def list_maps_of_stds(type, input, columns, order, where, separator, method, header, gran=None):
+ """! List the maps of a space time dataset using diffetent methods
+
+ @param type The type of the maps raster, raster3d or vector
+ @param input Name of a space time raster dataset
+ @param columns A comma separated list of columns to be printed to stdout
+ @param order A comma separated list of columns to order the
+ space time dataset by category
+ @param where A where statement for selected listing without "WHERE"
+ e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
+ @param separator The field separator character between the columns
+ @param method String identifier to select a method out of cols,
+ comma,delta or deltagaps
+ - "cols" Print preselected columns specified by columns
+ - "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 additional 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
+ @param gran The user defined granule to be used if method=gran is set, in case gran=None the
+ granule of the space time dataset is used
+ """
+
+ dbif, connected = init_dbif(None)
+
+ sp = open_old_space_time_dataset(input, type, dbif)
+
+ if separator is None or separator == "":
+ separator = "\t"
+
+ # This method expects a list of objects for gap detection
+ if method == "delta" or method == "deltagaps" or method == "gran":
+ if type == "stvds":
+ columns = "id,name,layer,mapset,start_time,end_time"
+ else:
+ columns = "id,name,mapset,start_time,end_time"
+ if method == "deltagaps":
+ maps = sp.get_registered_maps_as_objects_with_gaps(where=where, dbif=dbif)
+ elif method == "delta":
+ maps = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif)
+ elif method == "gran":
+ if gran is not None and gran != "":
+ maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran, dbif=dbif)
+ else:
+ maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
+
+ if header:
+ string = ""
+ string += "%s%s" % ("id", separator)
+ string += "%s%s" % ("name", separator)
+ if type == "stvds":
+ string += "%s%s" % ("layer", separator)
+ string += "%s%s" % ("mapset", separator)
+ string += "%s%s" % ("start_time", separator)
+ string += "%s%s" % ("end_time", separator)
+ string += "%s%s" % ("interval_length", separator)
+ string += "%s" % ("distance_from_begin")
+ print string
+
+ if maps and len(maps) > 0:
+
+ if isinstance(maps[0], list):
+ if len(maps[0]) > 0:
+ first_time, dummy = maps[0][0].get_temporal_extent_as_tuple()
+ else:
+ core.warning(_("Empty map list."))
+ return
+ else:
+ first_time, dummy = maps[0].get_temporal_extent_as_tuple()
+
+ for mymap in maps:
+
+ if isinstance(mymap, list):
+ if len(mymap) > 0:
+ map = mymap[0]
+ else:
+ core.fatal(_("Empty entry in map list, this should not happen."))
+ else:
+ map = mymap
+
+ start, end = map.get_temporal_extent_as_tuple()
+ if end:
+ delta = end - start
+ else:
+ delta = None
+ delta_first = start - first_time
+
+ if map.is_time_absolute():
+ if end:
+ delta = time_delta_to_relative_time(delta)
+ delta_first = time_delta_to_relative_time(delta_first)
+
+ string = ""
+ string += "%s%s" % (map.get_id(), separator)
+ string += "%s%s" % (map.get_name(), separator)
+ if type == "stvds":
+ string += "%s%s" % (map.get_layer(), separator)
+ string += "%s%s" % (map.get_mapset(), separator)
+ string += "%s%s" % (start, separator)
+ string += "%s%s" % (end, separator)
+ string += "%s%s" % (delta, separator)
+ string += "%s" % (delta_first)
+ print string
+
+ else:
+ # In comma separated mode only map ids are needed
+ if method == "comma":
+ columns = "id"
+
+ rows = sp.get_registered_maps(columns, where, order, dbif)
+
+ if rows:
+ if method == "comma":
+ string = ""
+ count = 0
+ for row in rows:
+ if count == 0:
+ string += row["id"]
+ else:
+ string += ",%s" % row["id"]
+ count += 1
+ print string
+
+ elif method == "cols":
+ # Print the column names if requested
+ if header:
+ output = ""
+ count = 0
+
+ collist = columns.split(",")
+
+ for key in collist:
+ 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 connected:
+ dbif.close()
Deleted: grass/trunk/lib/python/temporal/open.py
===================================================================
--- grass/trunk/lib/python/temporal/open.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/open.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -1,249 +0,0 @@
-"""!@package grass.temporal
-
- at brief GRASS Python scripting module (temporal GIS functions)
-
-Temporal GIS related functions to be used in Python scripts.
-
-Usage:
-
- at code
-import grass.temporal as tgis
-
-tgis.register_maps_in_space_time_dataset(type, name, maps)
-
-...
- at endcode
-
-(C) 2008-2011 by the GRASS Development Team
-This program is free software under the GNU General Public
-License (>=v2). Read the file COPYING that comes with GRASS
-for details.
-
- at author Soeren Gebbert
-"""
-
-from space_time_datasets import *
-from factory import *
-
-###############################################################################
-
-def open_old_space_time_dataset(name, type, dbif=None):
- """!This function opens an existing space time dataset and return the
- created and intialized object of the specified type.
-
- This function will raise a ScriptError in case the type is wrong,
- or the space time dataset was not found.
-
- @param name The name of the space time dataset, if the name does not
- contain the mapset (name at mapset) then the current mapset
- will be used to identifiy the space time dataset
- @param type The type of the space time dataset (strd, str3ds, stvds,
- raster, vector, raster3d)
- @param dbif The optional database interface to be used
-
- """
- mapset = get_current_mapset()
-
- # Check if the dataset name contains the mapset as well
- if name.find("@") < 0:
- id = name + "@" + mapset
- else:
- id = name
-
- if type == "strds" or type == "rast" or type == "raster":
- sp = dataset_factory("strds", id)
- elif type == "str3ds" or type == "rast3d" or type == "raster3d":
- sp = dataset_factory("str3ds", id)
- elif type == "stvds" or type == "vect" or type == "vector":
- sp = dataset_factory("stvds", id)
- else:
- core.fatal(_("Unkown type: %s") % (type))
-
- dbif, connected = init_dbif(dbif)
-
- if not sp.is_in_db(dbif):
- dbif.close()
- core.fatal(_("Space time %(sp)s dataset <%(name)s> no found") %
- {'sp': sp.get_new_map_instance(None).get_type(),
- 'name': name})
-
- # Read content from temporal database
- sp.select(dbif)
- if connected:
- dbif.close()
-
- return sp
-
-###############################################################################
-
-def check_new_space_time_dataset(name, type, dbif=None, overwrite=False):
- """!Check if a new space time dataset of a specific type can be created
-
- @param name The name of the new space time dataset
- @param type The type of the new space time dataset (strd, str3ds, stvds,
- raster, vector, raster3d)
- @param dbif The temporal database interface to be used
- @param overwrite Flag to allow overwriting
-
- @return A space time dataset object that must be filled with
- content before insertion in the temporal database
-
- 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 = get_current_mapset()
-
- if name.find("@") < 0:
- id = name + "@" + mapset
- else:
- n, m = name.split("@")
- if mapset != m:
- core.fatal(_("Space time datasets can only be created in the "
- "current mapset"))
- id = name
-
- if type == "strds" or type == "rast" or type == "raster":
- sp = dataset_factory("strds", id)
- elif type == "str3ds" or type == "rast3d" or type == "raster3d":
- sp = dataset_factory("str3ds", id)
- elif type == "stvds" or type == "vect" or type == "vector":
- sp = dataset_factory("stvds", id)
- else:
- core.error(_("Unkown type: %s") % (type))
- return None
-
- dbif, connected = init_dbif(dbif)
-
- if sp.is_in_db(dbif) and overwrite is False:
- core.fatal(_("Space time %(sp)s dataset <%(name)s> is already in the"
- " database. Use the overwrite flag.") % {
- 'sp': sp.get_new_map_instance(None).get_type(),
- 'name': name})
- if connected:
- dbif.close()
-
- return sp
-
-###############################################################################
-
-def open_new_space_time_dataset(name, type, temporaltype, title, descr, semantic,
- dbif=None, overwrite=False):
- """!Create a new space time dataset of a specific type
-
- @param name The name of the new space time dataset
- @param type The type of the new space time dataset (strd, str3ds, stvds,
- raster, vector, raster3d)
- @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
- @param overwrite Flag to allow overwriting
- @param dry Do not create the space time dataset in the temporal database,
- make a dry run with including all checks
-
- @return The new created space time dataset
-
- This function will raise a ScriptError in case of an error.
- """
- dbif, connected = init_dbif(dbif)
- sp = check_new_space_time_dataset(name, type, dbif, overwrite)
-
- if sp.is_in_db(dbif):
- core.warning(_("Overwrite space time %(sp)s dataset <%(name)s> and "
- "unregister all maps.") % {
- 'sp': sp.get_new_map_instance(None).get_type(),
- 'name': name})
- id = sp.get_id()
- sp.delete(dbif)
- sp = sp.get_new_instance(id)
-
- core.verbose(_("Create new 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 connected:
- dbif.close()
-
- return sp
-
-############################################################################
-
-def check_new_map_dataset(name, layer=None, type="raster",
- overwrite=False, dbif=None):
- """!Check if a new map dataset of a specific type can be created in
- the temporal database
-
- @param name The name of the new map dataset
- @param layer The layer of the new map dataset
- @param type The type of the new map dataset (raster, vector, raster3d)
- @param dbif The temporal database interface to be used
- @param overwrite Flag to allow overwriting
-
- @return A map dataset object
-
- This function will raise a ScriptError in case of an error.
- """
- mapset = get_current_mapset()
-
- dbif, connected = init_dbif(dbif)
- map_id = AbstractMapDataset.build_id(name, mapset, layer)
-
- new_map = dataset_factory(type, map_id)
- # Check if new map is in the temporal database
- if new_map.is_in_db(dbif):
- if not overwrite:
- if connected:
- dbif.close()
- core.fatal(_("Map <%s> is already in temporal database,"
- " use overwrite flag to overwrite") % (map_id))
-
- if connected:
- dbif.close()
-
- return new_map
-
-############################################################################
-
-def open_new_map_dataset(name, layer=None, type="raster",
- temporal_extent=None, overwrite=False,
- dbif=None):
- """!Create a new map dataset object of a specific type that can be
- registered in the temporal database
-
- @param name The name of the new map dataset
- @param layer The layer of the new map dataset
- @param type The type of the new map dataset (raster, vector, raster3d)
- @param dbif The temporal database interface to be used
- @param overwrite Flag to allow overwriting
-
- @return A map dataset object
-
- This function will raise a ScriptError in case of an error.
- """
-
- mapset = get_current_mapset()
-
- dbif, connected = init_dbif(dbif)
- new_map = check_new_map_dataset(name, layer, "raster", overwrite, dbif)
-
- # Check if new map is in the temporal database
- if new_map.is_in_db(dbif):
- # Remove the existing temporal database entry
- map_id = new_map.get_id()
- new_map.delete(dbif)
- new_map = new_map.get_new_instance(map_id)
-
- if temporal_extent:
- new_map.set_temporal_extent(temporal_extent)
-
- if connected:
- dbif.close()
-
- return new_map
Copied: grass/trunk/lib/python/temporal/open_stds.py (from rev 58380, grass/trunk/lib/python/temporal/open.py)
===================================================================
--- grass/trunk/lib/python/temporal/open_stds.py (rev 0)
+++ grass/trunk/lib/python/temporal/open_stds.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -0,0 +1,248 @@
+"""!@package grass.temporal
+
+ at brief GRASS Python scripting module (temporal GIS functions)
+
+Temporal GIS related functions to be used in Python scripts.
+
+Usage:
+
+ at code
+import grass.temporal as tgis
+
+tgis.register_maps_in_space_time_dataset(type, name, maps)
+
+...
+ at endcode
+
+(C) 2008-2011 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+ at author Soeren Gebbert
+"""
+
+from factory import *
+
+###############################################################################
+
+def open_old_space_time_dataset(name, type, dbif=None):
+ """!This function opens an existing space time dataset and return the
+ created and intialized object of the specified type.
+
+ This function will raise a ScriptError in case the type is wrong,
+ or the space time dataset was not found.
+
+ @param name The name of the space time dataset, if the name does not
+ contain the mapset (name at mapset) then the current mapset
+ will be used to identifiy the space time dataset
+ @param type The type of the space time dataset (strd, str3ds, stvds,
+ raster, vector, raster3d)
+ @param dbif The optional database interface to be used
+
+ """
+ mapset = get_current_mapset()
+
+ # Check if the dataset name contains the mapset as well
+ if name.find("@") < 0:
+ id = name + "@" + mapset
+ else:
+ id = name
+
+ if type == "strds" or type == "rast" or type == "raster":
+ sp = dataset_factory("strds", id)
+ elif type == "str3ds" or type == "rast3d" or type == "raster3d":
+ sp = dataset_factory("str3ds", id)
+ elif type == "stvds" or type == "vect" or type == "vector":
+ sp = dataset_factory("stvds", id)
+ else:
+ core.fatal(_("Unkown type: %s") % (type))
+
+ dbif, connected = init_dbif(dbif)
+
+ if not sp.is_in_db(dbif):
+ dbif.close()
+ core.fatal(_("Space time %(sp)s dataset <%(name)s> no found") %
+ {'sp': sp.get_new_map_instance(None).get_type(),
+ 'name': name})
+
+ # Read content from temporal database
+ sp.select(dbif)
+ if connected:
+ dbif.close()
+
+ return sp
+
+###############################################################################
+
+def check_new_space_time_dataset(name, type, dbif=None, overwrite=False):
+ """!Check if a new space time dataset of a specific type can be created
+
+ @param name The name of the new space time dataset
+ @param type The type of the new space time dataset (strd, str3ds, stvds,
+ raster, vector, raster3d)
+ @param dbif The temporal database interface to be used
+ @param overwrite Flag to allow overwriting
+
+ @return A space time dataset object that must be filled with
+ content before insertion in the temporal database
+
+ 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 = get_current_mapset()
+
+ if name.find("@") < 0:
+ id = name + "@" + mapset
+ else:
+ n, m = name.split("@")
+ if mapset != m:
+ core.fatal(_("Space time datasets can only be created in the "
+ "current mapset"))
+ id = name
+
+ if type == "strds" or type == "rast" or type == "raster":
+ sp = dataset_factory("strds", id)
+ elif type == "str3ds" or type == "rast3d" or type == "raster3d":
+ sp = dataset_factory("str3ds", id)
+ elif type == "stvds" or type == "vect" or type == "vector":
+ sp = dataset_factory("stvds", id)
+ else:
+ core.error(_("Unkown type: %s") % (type))
+ return None
+
+ dbif, connected = init_dbif(dbif)
+
+ if sp.is_in_db(dbif) and overwrite is False:
+ core.fatal(_("Space time %(sp)s dataset <%(name)s> is already in the"
+ " database. Use the overwrite flag.") % {
+ 'sp': sp.get_new_map_instance(None).get_type(),
+ 'name': name})
+ if connected:
+ dbif.close()
+
+ return sp
+
+###############################################################################
+
+def open_new_space_time_dataset(name, type, temporaltype, title, descr, semantic,
+ dbif=None, overwrite=False):
+ """!Create a new space time dataset of a specific type
+
+ @param name The name of the new space time dataset
+ @param type The type of the new space time dataset (strd, str3ds, stvds,
+ raster, vector, raster3d)
+ @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
+ @param overwrite Flag to allow overwriting
+ @param dry Do not create the space time dataset in the temporal database,
+ make a dry run with including all checks
+
+ @return The new created space time dataset
+
+ This function will raise a ScriptError in case of an error.
+ """
+ dbif, connected = init_dbif(dbif)
+ sp = check_new_space_time_dataset(name, type, dbif, overwrite)
+
+ if sp.is_in_db(dbif):
+ core.warning(_("Overwrite space time %(sp)s dataset <%(name)s> and "
+ "unregister all maps.") % {
+ 'sp': sp.get_new_map_instance(None).get_type(),
+ 'name': name})
+ id = sp.get_id()
+ sp.delete(dbif)
+ sp = sp.get_new_instance(id)
+
+ core.verbose(_("Create new 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 connected:
+ dbif.close()
+
+ return sp
+
+############################################################################
+
+def check_new_map_dataset(name, layer=None, type="raster",
+ overwrite=False, dbif=None):
+ """!Check if a new map dataset of a specific type can be created in
+ the temporal database
+
+ @param name The name of the new map dataset
+ @param layer The layer of the new map dataset
+ @param type The type of the new map dataset (raster, vector, raster3d)
+ @param dbif The temporal database interface to be used
+ @param overwrite Flag to allow overwriting
+
+ @return A map dataset object
+
+ This function will raise a ScriptError in case of an error.
+ """
+ mapset = get_current_mapset()
+
+ dbif, connected = init_dbif(dbif)
+ map_id = AbstractMapDataset.build_id(name, mapset, layer)
+
+ new_map = dataset_factory(type, map_id)
+ # Check if new map is in the temporal database
+ if new_map.is_in_db(dbif):
+ if not overwrite:
+ if connected:
+ dbif.close()
+ core.fatal(_("Map <%s> is already in temporal database,"
+ " use overwrite flag to overwrite") % (map_id))
+
+ if connected:
+ dbif.close()
+
+ return new_map
+
+############################################################################
+
+def open_new_map_dataset(name, layer=None, type="raster",
+ temporal_extent=None, overwrite=False,
+ dbif=None):
+ """!Create a new map dataset object of a specific type that can be
+ registered in the temporal database
+
+ @param name The name of the new map dataset
+ @param layer The layer of the new map dataset
+ @param type The type of the new map dataset (raster, vector, raster3d)
+ @param dbif The temporal database interface to be used
+ @param overwrite Flag to allow overwriting
+
+ @return A map dataset object
+
+ This function will raise a ScriptError in case of an error.
+ """
+
+ mapset = get_current_mapset()
+
+ dbif, connected = init_dbif(dbif)
+ new_map = check_new_map_dataset(name, layer, "raster", overwrite, dbif)
+
+ # Check if new map is in the temporal database
+ if new_map.is_in_db(dbif):
+ # Remove the existing temporal database entry
+ map_id = new_map.get_id()
+ new_map.delete(dbif)
+ new_map = new_map.get_new_instance(map_id)
+
+ if temporal_extent:
+ new_map.set_temporal_extent(temporal_extent)
+
+ if connected:
+ dbif.close()
+
+ return new_map
Modified: grass/trunk/lib/python/temporal/register.py
===================================================================
--- grass/trunk/lib/python/temporal/register.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/register.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -22,9 +22,7 @@
@author Soeren Gebbert
"""
-from space_time_datasets import *
-from factory import *
-from open import *
+from open_stds import *
###############################################################################
@@ -41,29 +39,6 @@
It takes care of the correct update of the space time datasets from all
registered maps.
- @code
-
- >>> import grass.script as grass
- >>> import grass.temporal as tgis
- >>> grass.use_temp_region()
- >>> grass.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0,
- ... t=1.0, b=0.0, res=10.0)
- 0
- >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_1 = 1")
- 0
- >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_2 = 2")
- 0
- >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_3 = 3")
- 0
- >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_4 = 4")
- 0
- >>> tgis.init(True)
- >>> tgis.register_maps_in_space_time_dataset(type="strds", name=None,
- ... maps="register_map_1,register_map_2,register_map_3,register_map_4",
- ... start="2001-01-01", increment="1 day", interval=True)
-
- @endcode
-
@param type The type of the maps rast, rast3d or vect
@param name The name of the space time dataset. Maps will be registered in the
temporal database if the name was set to None
Modified: grass/trunk/lib/python/temporal/stds_export.py
===================================================================
--- grass/trunk/lib/python/temporal/stds_export.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/stds_export.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -35,7 +35,7 @@
from space_time_datasets import *
from factory import *
-from open import *
+from open_stds import *
proj_file_name = "proj.txt"
init_file_name = "init.txt"
Added: grass/trunk/lib/python/temporal/unittests_register.py
===================================================================
--- grass/trunk/lib/python/temporal/unittests_register.py (rev 0)
+++ grass/trunk/lib/python/temporal/unittests_register.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -0,0 +1,204 @@
+"""!Unit test to register raster maps with absolute and relative
+ time using tgis.register_maps_in_space_time_dataset()
+
+(C) 2013 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+ at author Soeren Gebbert
+"""
+
+import grass.script as grass
+import grass.temporal as tgis
+import unittest
+import datetime
+
+class TestRegisterFunctions(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ """!Initiate the temporal GIS and set the region
+ """
+ tgis.init(True)
+ grass.overwrite = True
+ grass.use_temp_region()
+ ret = grass.run_command("g.region", n=80.0, s=0.0, e=120.0,
+ w=0.0, t=1.0, b=0.0, res=10.0)
+
+ def setUp(self):
+ """!Create the test maps
+ """
+ ret = 0
+ ret += grass.run_command("r.mapcalc", overwrite=True, quiet=True,
+ expression="register_map_1 = 1")
+ ret += grass.run_command("r.mapcalc", overwrite=True, quiet=True,
+ expression="register_map_2 = 2")
+ self.assertEqual(ret, 0)
+
+ def test_absolute_time_strds(self):
+ """!Test the registration of maps with absolute time in a
+ space time raster dataset
+ """
+ ret = grass.run_command("t.create", output="register_test",
+ title="Test strds", description="Test strds",
+ temporaltype="absolute")
+ self.assertEqual(ret, 0)
+
+ tgis.register_maps_in_space_time_dataset(type="rast", name="register_test",
+ maps="register_map_1,register_map_2",
+ start="2001-01-01", increment="1 day", interval=True)
+
+ map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+ map.select()
+ start, end, tz = map.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 1, 2))
+
+ map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+ map.select()
+ start, end, tz = map.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 2))
+ self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+ strds = tgis.SpaceTimeRasterDataset("register_test@" + tgis.get_current_mapset())
+ strds.select()
+ start, end, tz = strds.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+ ret = grass.run_command("t.remove", input="register_test")
+ self.assertEqual(ret, 0)
+
+ def test_absolute_time_1(self):
+ """!Test the registration of maps with absolute time
+ """
+ tgis.register_maps_in_space_time_dataset(type="rast", name=None,
+ maps="register_map_1,register_map_2",
+ start="2001-01-01", increment="1 day", interval=True)
+
+ map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+ map.select()
+ start, end, tz = map.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 1, 2))
+
+ map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+ map.select()
+ start, end, tz = map.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 2))
+ self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+ def test_absolute_time_2(self):
+ """!Test the registration of maps with absolute time
+ """
+ tgis.register_maps_in_space_time_dataset(type="rast", name=None,
+ maps="register_map_1,register_map_2",
+ start="2001-01-01 10:30:01", increment="8 hours", interval=False)
+
+ map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+ map.select()
+ start, end, tz = map.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1))
+
+ map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+ map.select()
+ start, end, tz = map.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
+
+ def test_relative_time_strds(self):
+ """!Test the registration of maps with relative time in a
+ space time raster dataset
+ """
+ ret = grass.run_command("t.create", output="register_test",
+ title="Test strds", description="Test strds",
+ temporaltype="relative")
+ self.assertEqual(ret, 0)
+
+
+ tgis.register_maps_in_space_time_dataset(type="rast", name="register_test",
+ maps="register_map_1,register_map_2",
+ start=0, increment=1, unit="day", interval=True)
+
+ map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+ map.select()
+ start, end, unit = map.get_relative_time()
+ self.assertEqual(start, 0)
+ self.assertEqual(end, 1)
+ self.assertEqual(unit, "day")
+
+ map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+ map.select()
+ start, end, unit = map.get_relative_time()
+ self.assertEqual(start, 1)
+ self.assertEqual(end, 2)
+ self.assertEqual(unit, "day")
+
+ strds = tgis.SpaceTimeRasterDataset("register_test@" + tgis.get_current_mapset())
+ strds.select()
+ start, end, unit = strds.get_relative_time()
+ self.assertEqual(start, 0)
+ self.assertEqual(end, 2)
+ self.assertEqual(unit, "day")
+
+ ret = grass.run_command("t.remove", input="register_test")
+ self.assertEqual(ret, 0)
+
+ def test_relative_time_1(self):
+ """!Test the registration of maps with relative time
+ """
+ tgis.register_maps_in_space_time_dataset(type="rast", name=None,
+ maps="register_map_1,register_map_2",
+ start=0, increment=1, unit="day", interval=True)
+
+ map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+ map.select()
+ start, end, unit = map.get_relative_time()
+ self.assertEqual(start, 0)
+ self.assertEqual(end, 1)
+ self.assertEqual(unit, "day")
+
+ map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+ map.select()
+ start, end, unit = map.get_relative_time()
+ self.assertEqual(start, 1)
+ self.assertEqual(end, 2)
+ self.assertEqual(unit, "day")
+
+ def test_relative_time_2(self):
+ """!Test the registration of maps with relative time
+ """
+ tgis.register_maps_in_space_time_dataset(type="rast", name=None,
+ maps="register_map_1,register_map_2",
+ start=1000000, increment=500000, unit="second", interval=True)
+
+ map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+ map.select()
+ start, end, unit = map.get_relative_time()
+ self.assertEqual(start, 1000000)
+ self.assertEqual(end, 1500000)
+ self.assertEqual(unit, "second")
+
+ map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+ map.select()
+ start, end, unit = map.get_relative_time()
+ self.assertEqual(start, 1500000)
+ self.assertEqual(end, 2000000)
+ self.assertEqual(unit, "second")
+
+ def tearDown(self):
+ """!Remove maps from temporal database
+ """
+ ret = grass.run_command("t.unregister", maps="register_map_1,register_map_2")
+ self.assertEqual(ret, 0)
+
+ @classmethod
+ def tearDownClass(cls):
+ """!Remove the temporary region
+ """
+ grass.del_temp_region()
+
+if __name__ == '__main__':
+ unittest.main()
+
+
Modified: grass/trunk/lib/python/temporal/univar_statistics.py
===================================================================
--- grass/trunk/lib/python/temporal/univar_statistics.py 2013-12-03 21:34:42 UTC (rev 58380)
+++ grass/trunk/lib/python/temporal/univar_statistics.py 2013-12-04 00:55:30 UTC (rev 58381)
@@ -25,7 +25,7 @@
from space_time_datasets import *
from factory import *
-from open import *
+from open_stds import *
###############################################################################
More information about the grass-commit
mailing list