[GRASS-SVN] r62635 - in grass/trunk/lib/python/temporal: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Nov 6 05:53:41 PST 2014
Author: huhabla
Date: 2014-11-06 05:53:41 -0800 (Thu, 06 Nov 2014)
New Revision: 62635
Added:
grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_grs.py
grass/trunk/lib/python/temporal/testsuite/unittests_temporal_raster_algebra_grs.py
Modified:
grass/trunk/lib/python/temporal/register.py
grass/trunk/lib/python/temporal/temporal_algebra.py
grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py
grass/trunk/lib/python/temporal/temporal_vector_algebra.py
grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py
Log:
temporal framework: New temporal algebra functionality implemented to support granularity sampling instead of topological sampling.
Modified: grass/trunk/lib/python/temporal/register.py
===================================================================
--- grass/trunk/lib/python/temporal/register.py 2014-11-06 09:05:25 UTC (rev 62634)
+++ grass/trunk/lib/python/temporal/register.py 2014-11-06 13:53:41 UTC (rev 62635)
@@ -377,7 +377,6 @@
if end:
end_time = string_to_datetime(end)
if end_time is None:
- dbif.close()
msgr.fatal(_("Unable to convert string \"%s\"into a "
"datetime object") % (end))
Modified: grass/trunk/lib/python/temporal/temporal_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_algebra.py 2014-11-06 09:05:25 UTC (rev 62634)
+++ grass/trunk/lib/python/temporal/temporal_algebra.py 2014-11-06 13:53:41 UTC (rev 62635)
@@ -689,7 +689,7 @@
if self.dbif.connected:
self.dbif.close()
- def setup_common_granularity(self, expression, stdstype = 'strds'):
+ def setup_common_granularity(self, expression, stdstype = 'strds', lexer = None):
"""Configure the temporal algebra to use the common granularity of all
space time datasets in the expression to generate the map lists.
@@ -702,32 +702,49 @@
:param expression: The algebra expression to analyze
+ :param lexer: The temporal algebra lexer (select, raster, voxel, vector) that should be used to
+ parse the expression, default is TemporalAlgebraLexer
+
:return: True if successful, False otherwise
+
+ :TODO: Check for tmap and map functions in the expression to avoid
+ problems with map names that are handled as STDS
"""
-
+ l = lexer
# Split the expression to ignore the left part
- expression = expression.split("=")[1]
-
+ expressions = expression.split("=")[1:]
+ expression = " ".join(expressions)
+
# detect all STDS
- l = TemporalAlgebraLexer()
+ if l is None:
+ l = TemporalAlgebraLexer()
l.build()
l.lexer.input(expression)
- self.name_list = []
- ignore = False
+ name_list = []
+ tokens = []
+ count = 0
while True:
tok = l.lexer.token()
if not tok: break
+ # Ignore map layer
+ tokens.append(tok.type)
+ ignore = False
+ if count > 1:
+ if tokens[count - 2] == "MAP" or tokens[count - 2] == "TMAP":
+ ignore = True
+
if tok.type == "NAME" and ignore == False:
- self.name_list.append(tok.value)
+ name_list.append(tok.value)
+ count += 1
grans = []
ttypes = {}
dbif, connected = init_dbif(self.dbif)
- for name in self.name_list:
+ for name in name_list:
stds = open_old_stds(name, stdstype, dbif)
# We need valid temporal topology
if stds.check_temporal_topology() is False:
@@ -803,6 +820,8 @@
if not rename:
name = base_map.get_id()
map_new.set_id(name)
+ # Make sure to set the uid that is used in several dictionaries
+ map_new.uid = name
return map_new
def overlay_map_extent(self, mapA, mapB, bool_op = None, temp_op = 'l',
@@ -973,7 +992,6 @@
# Ignore gap objects
if entry[0].get_id() is not None:
maplist.append(entry[0])
- print maplist
else:
maplist = stds.get_registered_maps_as_objects(dbif=self.dbif)
# Create map_value as empty list item.
@@ -1013,6 +1031,14 @@
map_i.condition_value = []
else:
self.msgr.fatal(_("Wrong type of input"))
+
+ # We generate a unique map id that will be used
+ # in the topology analysis, since the maplist can
+ # contain maps with equal map ids
+ for map in maplist:
+ map.uid = self.generate_map_name()
+ if self.debug:
+ print map.get_name(), map.uid, map.get_temporal_extent_as_tuple()
return(maplist)
@@ -1049,8 +1075,10 @@
>>> for i in range(10):
... idA = "a%i at B"%(i)
... mapA = tgis.RasterDataset(idA)
+ ... mapA.uid = idA
... idB = "b%i at B"%(i)
... mapB = tgis.RasterDataset(idB)
+ ... mapB.uid = idB
... check = mapA.set_relative_time(i, i + 1, "months")
... check = mapB.set_relative_time(i, i + 1, "months")
... mapsA.append(mapA)
@@ -1080,8 +1108,10 @@
>>> for i in range(10):
... idA = "a%i at B"%(i)
... mapA = tgis.RasterDataset(idA)
+ ... mapA.uid = idA
... idB = "b%i at B"%(i)
... mapB = tgis.RasterDataset(idB)
+ ... mapB.uid = idB
... check = mapA.set_relative_time(i, i + 1, "months")
... check = mapB.set_relative_time(i, i + 2, "months")
... mapsA.append(mapA)
@@ -1123,8 +1153,10 @@
>>> for i in range(10):
... idA = "a%i at B"%(i)
... mapA = tgis.RasterDataset(idA)
+ ... mapA.uid = idA
... idB = "b%i at B"%(i)
... mapB = tgis.RasterDataset(idB)
+ ... mapB.uid = idB
... check = mapA.set_relative_time(i, i + 1, "months")
... check = mapB.set_relative_time(i, i + 1, "months")
... mapB.map_value = True
@@ -1136,9 +1168,11 @@
>>> for i in range(10):
... idA = "a%i at B"%(i)
... mapA = tgis.RasterDataset(idA)
+ ... mapA.uid = idA
... mapA.map_value = True
... idB = "b%i at B"%(i)
... mapB = tgis.RasterDataset(idB)
+ ... mapB.uid = idB
... mapB.map_value = False
... check = mapA.set_absolute_time(datetime(2000,1,i+1),
... datetime(2000,1,i + 2))
@@ -1201,7 +1235,8 @@
map_i.map_value.append(gvar)
else:
map_i.map_value = gvar
- resultdict[map_i.get_id()] = map_i
+ # Use unique identifier, since map names may be equal
+ resultdict[map_i.uid] = map_i
resultlist = resultdict.values()
# Sort list of maps chronological.
@@ -1335,8 +1370,10 @@
>>> for i in range(10):
... idA = "a%i at B"%(i)
... mapA = tgis.RasterDataset(idA)
+ ... mapA.uid = idA
... idB = "b%i at B"%(i)
... mapB = tgis.RasterDataset(idB)
+ ... mapB.uid = idB
... check = mapA.set_relative_time(i, i + 1, "months")
... check = mapB.set_relative_time(i + 5, i + 6, "months")
... mapsA.append(mapA)
@@ -1407,8 +1444,10 @@
>>> for i in range(10):
... idA = "a%i at B"%(i)
... mapA = tgis.RasterDataset(idA)
+ ... mapA.uid = idA
... idB = "b%i at B"%(i)
... mapB = tgis.RasterDataset(idB)
+ ... mapB.uid = idB
... check = mapA.set_relative_time(i, i + 1, "months")
... check = mapB.set_relative_time(i*2, i*2 + 2, "months")
... mapsA.append(mapA)
@@ -1519,6 +1558,7 @@
>>> for i in range(1):
... idA = "a%i at B"%(i)
... mapA = tgis.RasterDataset(idA)
+ ... mapA.uid = idA
... check = mapA.set_absolute_time(datetime.datetime(2000,1,1),
... datetime.datetime(2000,10,1))
... tfuncdict = l.get_temporal_func_dict(mapA)
Modified: grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py 2014-11-06 09:05:25 UTC (rev 62634)
+++ grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py 2014-11-06 13:53:41 UTC (rev 62635)
@@ -231,7 +231,8 @@
map_i.map_value.append(gvar)
else:
map_i.map_value = gvar
- resultdict[map_i.get_id()] = map_i
+ # Use unique identifier, since map names may be equal
+ resultdict[map_i.uid] = map_i
resultlist = resultdict.values()
# Sort list of maps chronological.
@@ -422,7 +423,7 @@
# Conditional append of module command.
map_new.cmd_list = cmdstring
# Write map object to result dictionary.
- resultdict[map_new.get_id()] = map_new
+ resultdict[map_new.uid] = map_new
if returncode == 0:
break
# Append map to result map list.
Modified: grass/trunk/lib/python/temporal/temporal_vector_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_vector_algebra.py 2014-11-06 09:05:25 UTC (rev 62634)
+++ grass/trunk/lib/python/temporal/temporal_vector_algebra.py 2014-11-06 13:53:41 UTC (rev 62635)
@@ -232,7 +232,8 @@
map_i.map_value.append(gvar)
else:
map_i.map_value = gvar
- resultdict[map_i.get_id()] = map_i
+ # Use unique identifier, since map names may be equal
+ resultdict[map_i.uid] = map_i
resultlist = resultdict.values()
# Sort list of maps chronological.
Modified: grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py 2014-11-06 09:05:25 UTC (rev 62634)
+++ grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py 2014-11-06 13:53:41 UTC (rev 62635)
@@ -348,38 +348,5 @@
self.assertEqual( D.check_temporal_topology(), True)
self.assertEqual(D.get_granularity(), u'1 day')
- def test_common_granularity(self):
- """Testing the common granularity function. """
- ta = tgis.TemporalAlgebraParser(run = True, debug = True)
- ret = ta.setup_common_granularity(expression='R = A : B')
-
- self.assertEqual(ret, True)
- self.assertEqual(ta.granularity, "1 days")
-
- ta.count = 0
- ta.stdstype = "strds"
- ta.maptype = "rast"
- ta.mapclass = tgis.RasterDataset
-
- maplist = ta.check_stds("A")
- self.assertEqual(len(maplist), 4)
- maplist = ta.check_stds("B")
- self.assertEqual(len(maplist), 4)
-
- ta.parse(expression='R = A : B', basename="r", overwrite=True)
-
- D = tgis.open_old_stds("R", type="strds")
- D.select()
- self.assertEqual(D.metadata.get_number_of_maps(), 4)
- self.assertEqual(D.metadata.get_min_min(), 1)
- self.assertEqual(D.metadata.get_max_max(), 4)
- start, end = D.get_absolute_time()
- self.assertEqual(start, datetime.datetime(2001, 1, 1))
- self.assertEqual(end, datetime.datetime(2001, 1, 5))
- self.assertEqual( D.check_temporal_topology(), True)
- self.assertEqual(D.get_granularity(), u'1 day')
-
-
-
if __name__ == '__main__':
grass.gunittest.test()
Added: grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_grs.py
===================================================================
--- grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_grs.py (rev 0)
+++ grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra_grs.py 2014-11-06 13:53:41 UTC (rev 62635)
@@ -0,0 +1,228 @@
+"""
+(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.
+
+:authors: Soeren Gebbert and Thomas Leppelt
+"""
+
+import grass.script
+import grass.temporal as tgis
+import grass.gunittest
+import datetime
+import os
+
+class TestTemporalAlgebraGranularity(grass.gunittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ """Initiate the temporal GIS and set the region
+ """
+ tgis.init(True) # Raise on error instead of exit(1)
+ cls.use_temp_region()
+ cls.runModule("g.region", n=80.0, s=0.0, e=120.0,
+ w=0.0, t=1.0, b=0.0, res=10.0)
+
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 1")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 2")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a3 = 3")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a4 = 4")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a5 = 5")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a6 = 6")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b1 = 7")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b2 = 8")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="c1 = 9")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d1 = 10")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d2 = 11")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d3 = 12")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="singletmap = 99")
+
+
+ tgis.open_new_stds(name="A", type="strds", temporaltype="absolute",
+ title="A", descr="A", semantic="field", overwrite=True)
+ tgis.open_new_stds(name="B", type="strds", temporaltype="absolute",
+ title="B", descr="B", semantic="field", overwrite=True)
+ tgis.open_new_stds(name="C", type="strds", temporaltype="absolute",
+ title="C", descr="C", semantic="field", overwrite=True)
+ tgis.open_new_stds(name="D", type="strds", temporaltype="absolute",
+ title="D", descr="D", semantic="field", overwrite=True)
+
+ tgis.register_maps_in_space_time_dataset(type="rast", name="A", maps="a1,a2,a3,a4,a5,a6",
+ start="2001-01-01", increment="1 month", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="B", maps="b1,b2",
+ start="2001-01-01", increment="3 months", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="C", maps="c1",
+ start="2001-01-01", increment="1 year", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="D", maps="d1",
+ start="2001-01-01", increment="31 days", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="D", maps="d2",
+ start="2001-03-01", increment="31 days", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="D", maps="d3",
+ start="2001-05-01", increment="31 days", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name=None, maps="singletmap",
+ start="2001-01-03", end="2001-01-04", interval=True)
+
+ def tearDown(self):
+ pass
+ #self.runModule("t.remove", inputs="R", quiet=True)
+
+ @classmethod
+ def tearDownClass(cls):
+ """Remove the temporary region
+ """
+ #cls.runModule("t.remove", flags="rf", inputs="A,B,C,D", quiet=True)
+ cls.del_temp_region()
+
+ def test_common_granularity_1(self):
+ """Testing the common granularity function. """
+ ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+ expr = 'R = A : B'
+ ret = ta.setup_common_granularity(expression=expr)
+
+ self.assertEqual(ret, True)
+ self.assertEqual(ta.granularity, "1 months")
+
+ ta.count = 0
+ ta.stdstype = "strds"
+ ta.maptype = "rast"
+ ta.mapclass = tgis.RasterDataset
+
+ maplist = ta.check_stds("A")
+ self.assertEqual(len(maplist), 6)
+ maplist = ta.check_stds("B")
+ self.assertEqual(len(maplist), 6)
+
+ ta.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 6)
+ self.assertEqual(D.metadata.get_min_min(), 1)
+ self.assertEqual(D.metadata.get_max_max(), 6)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+ def test_common_granularity_2(self):
+ """Testing the common granularity function year to month samping. """
+ ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+ expr = 'R = A : C'
+ ret = ta.setup_common_granularity(expression=expr)
+
+ ta.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 6)
+ self.assertEqual(D.metadata.get_min_min(), 1)
+ self.assertEqual(D.metadata.get_max_max(), 6)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+
+ def test_common_granularity_3(self):
+ """Testing the common granularity function with gaps. """
+ ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+ expr = 'R = A : D'
+ ret = ta.setup_common_granularity(expression=expr)
+
+ ta.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 3)
+ self.assertEqual(D.metadata.get_min_min(), 1)
+ self.assertEqual(D.metadata.get_max_max(), 5)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 6, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+
+ def test_common_granularity_4(self):
+ """Testing the common granularity function year to month with gaps. """
+ ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+ expr = 'R = C : D'
+ ret = ta.setup_common_granularity(expression=expr)
+
+ ta.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 3)
+ self.assertEqual(D.metadata.get_min_min(), 9)
+ self.assertEqual(D.metadata.get_max_max(), 9)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 6, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+ def test_common_granularity_4(self):
+ """Testing the common granularity function year to month with gaps. """
+ ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+ expr = 'R = C : D'
+ ret = ta.setup_common_granularity(expression=expr)
+
+ ta.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 3)
+ self.assertEqual(D.metadata.get_min_min(), 9)
+ self.assertEqual(D.metadata.get_max_max(), 9)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 6, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+ def test_common_granularity_5(self):
+ """Testing the common granularity function year to month with gaps. """
+ ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+ expr = 'R = A : C : D'
+ ret = ta.setup_common_granularity(expression=expr)
+
+ ta.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 3)
+ self.assertEqual(D.metadata.get_min_min(), 1)
+ self.assertEqual(D.metadata.get_max_max(), 5)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 6, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+ def test_common_granularity_6(self):
+ """Testing the common granularity function year to month with gaps. """
+ ta = tgis.TemporalAlgebraParser(run = True, debug = True)
+ expr = 'R = if(start_month(A) > 2, A : C : D)'
+ ret = ta.setup_common_granularity(expression=expr)
+
+ ta.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 2)
+ self.assertEqual(D.metadata.get_min_min(), 3)
+ self.assertEqual(D.metadata.get_max_max(), 5)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 3, 1))
+ self.assertEqual(end, datetime.datetime(2001, 6, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+
+
+if __name__ == '__main__':
+ grass.gunittest.test()
Added: grass/trunk/lib/python/temporal/testsuite/unittests_temporal_raster_algebra_grs.py
===================================================================
--- grass/trunk/lib/python/temporal/testsuite/unittests_temporal_raster_algebra_grs.py (rev 0)
+++ grass/trunk/lib/python/temporal/testsuite/unittests_temporal_raster_algebra_grs.py 2014-11-06 13:53:41 UTC (rev 62635)
@@ -0,0 +1,264 @@
+"""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 datetime
+import os
+import grass.script
+import grass.temporal as tgis
+import grass.gunittest as gunittest
+
+class TestTemporalRasterAlgebra(gunittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ """Initiate the temporal GIS and set the region
+ """
+ tgis.init(True) # Raise on error instead of exit(1)
+ cls.use_temp_region()
+ cls.runModule("g.region", n=80.0, s=0.0, e=120.0,
+ w=0.0, t=1.0, b=0.0, res=10.0)
+
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 1")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 2")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a3 = 3")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a4 = 4")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a5 = 5")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a6 = 6")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b1 = 7")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b2 = 8")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="c1 = 9")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d1 = 10")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d2 = 11")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d3 = 12")
+ cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="singletmap = 99")
+
+ tgis.open_new_stds(name="A", type="strds", temporaltype="absolute",
+ title="A", descr="A", semantic="field", overwrite=True)
+ tgis.open_new_stds(name="B", type="strds", temporaltype="absolute",
+ title="B", descr="B", semantic="field", overwrite=True)
+ tgis.open_new_stds(name="C", type="strds", temporaltype="absolute",
+ title="C", descr="C", semantic="field", overwrite=True)
+ tgis.open_new_stds(name="D", type="strds", temporaltype="absolute",
+ title="D", descr="D", semantic="field", overwrite=True)
+
+ tgis.register_maps_in_space_time_dataset(type="rast", name="A", maps="a1,a2,a3,a4,a5,a6",
+ start="2001-01-01", increment="1 month", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="B", maps="b1,b2",
+ start="2001-01-01", increment="3 months", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="C", maps="c1",
+ start="2001-01-01", increment="1 year", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="D", maps="d1",
+ start="2001-01-01", increment="5 days", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="D", maps="d2",
+ start="2001-03-01", increment="5 days", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name="D", maps="d3",
+ start="2001-05-01", increment="5 days", interval=True)
+ tgis.register_maps_in_space_time_dataset(type="rast", name=None, maps="singletmap",
+ start="2001-03-01", end="2001-04-01", interval=True)
+
+ def tearDown(self):
+ self.runModule("t.remove", flags="rf", inputs="R", quiet=True)
+
+ @classmethod
+ def tearDownClass(cls):
+ """Remove the temporary region
+ """
+ cls.runModule("t.remove", flags="rf", inputs="A,B,C,D", quiet=True)
+ cls.runModule("t.unregister", maps="singletmap", quiet=True)
+ cls.del_temp_region()
+
+ def test_1(self):
+ """Simple arithmetik test"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = "R = if(C == 9, A - 1)"
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 6)
+ self.assertEqual(D.metadata.get_min_min(), 0) # 1 - 1
+ self.assertEqual(D.metadata.get_max_max(), 5) # 6 - 1
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+ def test_2(self):
+ """Simple arithmetik test"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = "R = if(D == 11, A - 1, A + 1)"
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 15)
+ self.assertEqual(D.metadata.get_min_min(), 2) # 1 - 1
+ self.assertEqual(D.metadata.get_max_max(), 6) # 5 + 1
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 5, 6))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 day')
+
+ def test_simple_arith_hash_1(self):
+ """Simple arithmetic test including the hash operator"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ tra.parse(expression='R = A + A # A', basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 6)
+ self.assertEqual(D.metadata.get_min_min(), 2)
+ self.assertEqual(D.metadata.get_max_max(), 7)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+
+
+ def test_simple_arith_td_1(self):
+ """Simple arithmetic test"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = 'R = A + td(A:D)'
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 15)
+ self.assertEqual(D.metadata.get_min_min(), 2) # 1 - 1
+ self.assertEqual(D.metadata.get_max_max(), 6) # 5 + 1
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 5, 6))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 day')
+
+ def test_simple_arith_if_1(self):
+ """Simple arithmetic test with if condition"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = 'R = if(start_date(A) >= "2001-02-01", A + A)'
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 5)
+ self.assertEqual(D.metadata.get_min_min(), 4)
+ self.assertEqual(D.metadata.get_max_max(), 12)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 2, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+
+ def test_simple_arith_if_2(self):
+ """Simple arithmetic test with if condition"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = 'R = if(A#A == 1, A - A)'
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 6)
+ self.assertEqual(D.metadata.get_min_min(), 0)
+ self.assertEqual(D.metadata.get_max_max(), 0)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+
+ def test_complex_arith_if_1(self):
+ """Complex arithmetic test with if condition"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = 'R = if(start_date(A) < "2001-03-01" && A#A == 1, A+C, A-C)'
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 6)
+ self.assertEqual(D.metadata.get_min_min(), -6) # 3 - 9
+ self.assertEqual(D.metadata.get_max_max(), 11) # 2 + 2
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+
+ def test_temporal_neighbors(self):
+ """Simple temporal neighborhood computation test"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr ='R = (A[0,0,-1] : D) + (A[0,0,1] : D)'
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 14)
+ self.assertEqual(D.metadata.get_min_min(), 2) # 1 + 1
+ self.assertEqual(D.metadata.get_max_max(), 10) # 5 + 5
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 2))
+ self.assertEqual(end, datetime.datetime(2001, 5, 6))
+
+ def test_map(self):
+ """Test STDS + single map without timestamp"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = "R = A + map(singletmap)"
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(), 6)
+ self.assertEqual(D.metadata.get_min_min(), 100) # 1 + 99
+ self.assertEqual(D.metadata.get_max_max(), 105) # 6 + 99
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 1, 1))
+ self.assertEqual(end, datetime.datetime(2001, 7, 1))
+
+ def test_tmap_map(self):
+ """Test STDS + single map with and without timestamp"""
+ tra = tgis.TemporalRasterAlgebraParser(run = True, debug = True)
+ expr = "R = tmap(singletmap) + A + map(singletmap)"
+ ret = tra.setup_common_granularity(expression=expr, lexer = tgis.TemporalRasterAlgebraLexer())
+ self.assertEqual(ret, True)
+
+ tra.parse(expression=expr, basename="r", overwrite=True)
+
+ D = tgis.open_old_stds("R", type="strds")
+
+ self.assertEqual(D.metadata.get_number_of_maps(),1)
+ self.assertEqual(D.metadata.get_min_min(), 201)
+ self.assertEqual(D.metadata.get_max_max(), 201)
+ start, end = D.get_absolute_time()
+ self.assertEqual(start, datetime.datetime(2001, 3, 1))
+ self.assertEqual(end, datetime.datetime(2001, 4, 1))
+ self.assertEqual( D.check_temporal_topology(), True)
+ self.assertEqual(D.get_granularity(), u'1 month')
+
+if __name__ == '__main__':
+ gunittest.test()
More information about the grass-commit
mailing list