[GRASS-SVN] r62631 - in grass/trunk/lib/python/temporal: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Nov 5 16:06:45 PST 2014
Author: huhabla
Date: 2014-11-05 16:06:45 -0800 (Wed, 05 Nov 2014)
New Revision: 62631
Modified:
grass/trunk/lib/python/temporal/open_stds.py
grass/trunk/lib/python/temporal/temporal_algebra.py
grass/trunk/lib/python/temporal/temporal_granularity.py
grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py
Log:
temporal framework: New temporal algebra approach implemented, that uses the common granularity of all STDS in an expression to perform map algebra. Some minor fixes and a new function to compute the common granularity for relative time.
Modified: grass/trunk/lib/python/temporal/open_stds.py
===================================================================
--- grass/trunk/lib/python/temporal/open_stds.py 2014-11-05 22:36:33 UTC (rev 62630)
+++ grass/trunk/lib/python/temporal/open_stds.py 2014-11-06 00:06:45 UTC (rev 62631)
@@ -37,6 +37,8 @@
:param type: The type of the space time dataset (strd, str3ds, stvds,
raster, vector, raster3d)
:param dbif: The optional database interface to be used
+
+ :return: New stds object
"""
mapset = get_current_mapset()
Modified: grass/trunk/lib/python/temporal/temporal_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_algebra.py 2014-11-05 22:36:33 UTC (rev 62630)
+++ grass/trunk/lib/python/temporal/temporal_algebra.py 2014-11-06 00:06:45 UTC (rev 62631)
@@ -683,11 +683,73 @@
self.m_mremove = pymod.Module('g.remove')
self.m_copy = pymod.Module('g.copy')
self.nprocs = nprocs
+ self.use_granularity = False
def __del__(self):
if self.dbif.connected:
self.dbif.close()
+
+ def setup_common_granularity(self, expression, stdstype = 'strds'):
+ """Configure the temporal algebra to use the common granularity of all
+ space time datasets in the expression to generate the map lists.
+
+ This function will analyze the expression to detect space time datasets
+ and computes are common granularity from all granularities.
+
+ This granularity is then be used to generate the map lists. Hence, all
+ maps from all STDS will have equidistant temporal extents. The only meaningful
+ temporal relation is "equal".
+
+ :param expression: The algebra expression to analyze
+
+ :return: True if successful, False otherwise
+ """
+
+ # Split the expression to ignore the left part
+ expression = expression.split("=")[1]
+
+ # detect all STDS
+ l = TemporalAlgebraLexer()
+ l.build()
+ l.lexer.input(expression)
+
+ self.name_list = []
+ ignore = False
+
+ while True:
+ tok = l.lexer.token()
+ if not tok: break
+
+ if tok.type == "NAME" and ignore == False:
+ self.name_list.append(tok.value)
+
+ grans = []
+ ttypes = {}
+ dbif, connected = init_dbif(self.dbif)
+
+ for name in self.name_list:
+ stds = open_old_stds(name, stdstype, dbif)
+ # We need valid temporal topology
+ if stds.check_temporal_topology() is False:
+ return False
+ grans.append(stds.get_granularity())
+ ttypes[stds.get_temporal_type()] = stds.get_temporal_type()
+
+ # Only one temporal type is allowed
+ if len(ttypes) > 1:
+ return False
+
+ # Compute the common granularity
+ if "absolute" in ttypes.keys():
+ self.granularity = compute_common_absolute_time_granularity(grans)
+ else:
+ self.granularity = compute_common_relative_time_granularity(grans)
+
+ self.use_granularity = True
+
+ return True
+
def parse(self, expression, stdstype = 'strds', maptype = 'rast', mapclass = RasterDataset, basename = None, overwrite=False):
self.lexer = TemporalAlgebraLexer()
self.lexer.build()
@@ -903,7 +965,17 @@
else:
# Select temporal dataset entry from database.
stds.select(dbif=self.dbif)
- maplist = stds.get_registered_maps_as_objects(dbif=self.dbif)
+ if self.use_granularity:
+ # We create the maplist out of the map array from none-gap objects
+ maplist = []
+ map_array = stds.get_registered_maps_as_objects_by_granularity(gran=self.granularity, dbif=self.dbif)
+ for entry in map_array:
+ # 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.
for map_i in maplist:
if "map_value" not in dir(map_i):
Modified: grass/trunk/lib/python/temporal/temporal_granularity.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_granularity.py 2014-11-05 22:36:33 UTC (rev 62630)
+++ grass/trunk/lib/python/temporal/temporal_granularity.py 2014-11-06 00:06:45 UTC (rev 62631)
@@ -495,9 +495,30 @@
###############################################################################
+def compute_common_relative_time_granularity(gran_list):
+ """Compute the greatest common granule from a list of relative time granules
+
+ .. code-block:: python
+
+ >>> import grass.temporal as tgis
+ >>> tgis.init()
+ >>> grans = [1,2,30]
+ >>> tgis.compute_common_relative_time_granularity(grans)
+ 1
+
+ >>> import grass.temporal as tgis
+ >>> tgis.init()
+ >>> grans = [10,20,30]
+ >>> tgis.compute_common_relative_time_granularity(grans)
+ 10
+ """
+ return gcd_list(gran_list)
+
+###############################################################################
+
def compute_common_absolute_time_granularity(gran_list):
"""Compute the greatest common granule from a list of absolute time granules
-
+
.. code-block:: python
>>> import grass.temporal as tgis
Modified: grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py 2014-11-05 22:36:33 UTC (rev 62630)
+++ grass/trunk/lib/python/temporal/testsuite/unittests_temporal_algebra.py 2014-11-06 00:06:45 UTC (rev 62631)
@@ -348,5 +348,38 @@
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()
More information about the grass-commit
mailing list