[GRASS-SVN] r70727 - in grass/trunk/lib/python/temporal: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Mar 4 23:35:59 PST 2017


Author: huhabla
Date: 2017-03-04 23:35:58 -0800 (Sat, 04 Mar 2017)
New Revision: 70727

Modified:
   grass/trunk/lib/python/temporal/register.py
   grass/trunk/lib/python/temporal/testsuite/test_register_function.py
Log:
temporal framework: Fixed empty map removal in register object function


Modified: grass/trunk/lib/python/temporal/register.py
===================================================================
--- grass/trunk/lib/python/temporal/register.py	2017-03-05 04:26:39 UTC (rev 70726)
+++ grass/trunk/lib/python/temporal/register.py	2017-03-05 07:35:58 UTC (rev 70727)
@@ -32,7 +32,7 @@
 def register_maps_in_space_time_dataset(
     type, name, maps=None, file=None, start=None,
     end=None, unit=None, increment=None, dbif=None,
-        interval=False, fs="|", update_cmd_list=True):
+    interval=False, fs="|", update_cmd_list=True):
     """Use this method to register maps in space time datasets.
 
        Additionally a start time string and an increment string can be
@@ -483,10 +483,15 @@
         # In case of a empty map continue, do not register empty maps
 
         if delete_empty:
-            if map_layer.metadata.get_min() is None and \
-               map_layer.metadata.get_max() is None:
-                empty_maps.append(map_layer)
-                continue
+            if type in ["raster", "raster_3d"]:
+                if map_layer.metadata.get_min() is None and \
+                   map_layer.metadata.get_max() is None:
+                    empty_maps.append(map_layer)
+                    continue
+            if type == "vector":
+                if map_layer.metadata.get_number_of_primitives() == 0:
+                    empty_maps.append(map_layer)
+                    continue
 
         start,  end = map_layer.get_temporal_extent_as_tuple()
         id = map_layer.get_id()
@@ -507,11 +512,9 @@
     g_remove = pymod.Module("g.remove", flags='f', quiet=True,
                             run_=False, finish_=True)
 
-    # Remove empty maps
+    # Remove empty maps and unregister them from the temporal database
     if len(empty_maps) > 0:
         for map in empty_maps:
-            if map.is_in_db(dbif):
-                map.delete(dbif)
             mod = copy.deepcopy(g_remove)
             if map.get_name():
                 if map.get_type() == "raster":
@@ -521,6 +524,8 @@
                 if map.get_type() == "vector":
                     mod(type='vector', name=map.get_name())
                 mod.run()
+            if map.is_in_db(dbif):
+                map.delete(dbif)
 
     if connected:
         dbif.close()

Modified: grass/trunk/lib/python/temporal/testsuite/test_register_function.py
===================================================================
--- grass/trunk/lib/python/temporal/testsuite/test_register_function.py	2017-03-05 04:26:39 UTC (rev 70726)
+++ grass/trunk/lib/python/temporal/testsuite/test_register_function.py	2017-03-05 07:35:58 UTC (rev 70727)
@@ -15,7 +15,7 @@
 import datetime
 import os
 
-class TestRegisterFunctions(TestCase):
+class TestRasterRegisterFunctions(TestCase):
 
     @classmethod
     def setUpClass(cls):
@@ -42,6 +42,8 @@
                        expression="register_map_1 = 1")
         self.runModule("r.mapcalc", overwrite=True, quiet=True,
                        expression="register_map_2 = 2")
+        self.runModule("r.mapcalc", overwrite=True, quiet=True,
+                       expression="register_map_null = null()")
 
         self.strds_abs = tgis.open_new_stds(name="register_test_abs", type="strds", temporaltype="absolute",
                                             title="Test strds", descr="Test strds", semantic="field",
@@ -53,8 +55,10 @@
     def tearDown(self):
         """Remove maps from temporal database
         """
-        self.runModule("t.unregister", maps="register_map_1,register_map_2", quiet=True)
-        self.runModule("g.remove", flags='f', type="raster", name="register_map_1,register_map_2", quiet=True)
+        self.runModule("t.unregister", type="raster", maps="register_map_1,register_map_2",
+                       quiet=True)
+        self.runModule("g.remove", flags='f', type="raster", name="register_map_1,register_map_2",
+                       quiet=True)
         self.strds_abs.delete()
         self.strds_rel.delete()
 
@@ -148,6 +152,7 @@
         """
 
         ciface = tgis.get_tgis_c_library_interface()
+        # Set the timestamp as relative time
         ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1 day")
 
         tgis.register_maps_in_space_time_dataset(type="raster", name=self.strds_abs.get_name(),
@@ -168,40 +173,70 @@
 
     def test_absolute_time_1(self):
         """Test the registration of maps with absolute time
+        using register_maps_in_space_time_dataset() and register_map_object_list()
         """
         tgis.register_maps_in_space_time_dataset(type="raster", 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 = map.get_absolute_time()
+        map_1 = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+        map_1.select()
+        start, end = map_1.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 = map.get_absolute_time()
+        map_2 = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+        map_2.select()
+        start, end = map_2.get_absolute_time()
         self.assertEqual(start, datetime.datetime(2001, 1, 2))
         self.assertEqual(end, datetime.datetime(2001, 1, 3))
 
+        map_list = [map_1, map_2]
+
+        tgis.register_map_object_list(type="raster", map_list=map_list,
+                                      output_stds=self.strds_abs,
+                                      delete_empty=False)
+        self.strds_abs.select()
+        start, end = self.strds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
     def test_absolute_time_2(self):
         """Test the registration of maps with absolute time
+        using register_maps_in_space_time_dataset() and register_map_object_list() with empty map deletion
         """
         tgis.register_maps_in_space_time_dataset(type="raster", name=None,
-                 maps="register_map_1,register_map_2",
+                 maps="register_map_1,register_map_2,register_map_null",
                  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 = map.get_absolute_time()
+        map_1 = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+        map_1.select()
+        start, end = map_1.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 = map.get_absolute_time()
+        map_2 = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+        map_2.select()
+        start, end = map_2.get_absolute_time()
         self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
 
+        map_3 = tgis.RasterDataset("register_map_null@" + tgis.get_current_mapset())
+        map_3.select()
+        start, end = map_3.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2, 02, 30, 1))
+
+        map_list = [map_1, map_2, map_3]
+
+        tgis.register_map_object_list(type="raster", map_list=map_list,
+                                      output_stds=self.strds_abs,
+                                      delete_empty=True)
+        self.strds_abs.select()
+        start, end = self.strds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 1, 18, 30, 1))
+
+        map_3 = tgis.VectorDataset("register_map_null@" + tgis.get_current_mapset())
+        self.assertEqual(map_3.map_exists(), False)
+
     def test_absolute_time_3(self):
         """Test the registration of maps with absolute time.
            The timestamps are set using the C-Interface beforehand, so that the register function needs
@@ -329,7 +364,7 @@
         self.assertEqual(unit, "seconds")
 
     def test_relative_time_3(self):
-        """Test the registration of maps with relative time. The timetsamps are set beforhand using
+        """Test the registration of maps with relative time. The timetsamps are set beforehand using
            the C-interface.
         """
         ciface = tgis.get_tgis_c_library_interface()
@@ -354,6 +389,209 @@
         self.assertEqual(unit, "seconds")
 
 
+class TestVectorRegisterFunctions(TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        os.putenv("GRASS_OVERWRITE", "1")
+        # Use always the current mapset as temporal database
+        cls.runModule("g.gisenv", set="TGIS_USE_CURRENT_MAPSET=1")
+        tgis.init()
+        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)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the temporary region
+        """
+        cls.del_temp_region()
+
+    def setUp(self):
+        """Create the test maps and the space time raster datasets
+        """
+        self.runModule("v.random", overwrite=True, quiet=True,
+                       output="register_map_1", npoints=5, seed=1)
+        self.runModule("v.random", overwrite=True, quiet=True,
+                       output="register_map_2", npoints=5, seed=1)
+        self.runModule("r.mapcalc", overwrite=True, quiet=True,
+                       expression="register_map_null = null()")
+        self.runModule("r.to.vect", overwrite=True, quiet=True,
+                       input="register_map_null", type="point",
+                       output="register_map_empty")
+
+        self.stvds_abs = tgis.open_new_stds(name="register_test_abs", type="stvds", temporaltype="absolute",
+                                            title="Test stvds", descr="Test stvds", semantic="field",
+                                            overwrite=True)
+        self.stvds_rel = tgis.open_new_stds(name="register_test_rel", type="stvds", temporaltype="relative",
+                                            title="Test stvds", descr="Test stvds", semantic="field",
+                                            overwrite=True)
+
+    def tearDown(self):
+        """Remove maps from temporal database
+        """
+        self.runModule("t.unregister", type="vector", maps="register_map_1,register_map_2", quiet=True)
+        self.runModule("g.remove", flags='f', type="vector", name="register_map_1,register_map_2", quiet=True)
+        self.stvds_abs.delete()
+        self.stvds_rel.delete()
+
+    def test_absolute_time_stvds_1(self):
+        """Test the registration of maps with absolute time in a
+           space time raster dataset
+        """
+        tgis.register_maps_in_space_time_dataset(type="vector", name=self.stvds_abs.get_name(),
+                 maps="register_map_1,register_map_2",
+                 start="2001-01-01", increment="1 day", interval=True)
+
+        map = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 2))
+
+        map = tgis.VectorDataset("register_map_2@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+        self.stvds_abs.select()
+        start, end = self.stvds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+    def test_absolute_time_stvds_2(self):
+        """Test the registration of maps with absolute time in a
+           space time raster dataset.
+           The timestamps are set using the C-Interface beforehand, so that the register function needs
+           to read the timetsamp from the map metadata.
+        """
+
+        ciface = tgis.get_tgis_c_library_interface()
+        ciface.write_vector_timestamp("register_map_1", tgis.get_current_mapset(), "1 Jan 2001/2 Jan 2001")
+        ciface.write_vector_timestamp("register_map_2", tgis.get_current_mapset(), "2 Jan 2001/3 Jan 2001")
+
+        tgis.register_maps_in_space_time_dataset(type="vector", name=self.stvds_abs.get_name(),
+                                                 maps="register_map_1,register_map_2")
+
+        map = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 2))
+
+        map = tgis.VectorDataset("register_map_2@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+        self.stvds_abs.select()
+        start, end = self.stvds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+    def test_absolute_time_stvds_3(self):
+        """Test the registration of maps with absolute time in a
+           space time raster dataset. The timestamps are set via method arguments and with the
+           c-interface. The timestamps of the method arguments should overwrite the
+           time stamps set via the C-interface.
+        """
+
+        ciface = tgis.get_tgis_c_library_interface()
+        ciface.write_vector_timestamp("register_map_1", tgis.get_current_mapset(), "1 Jan 2001/2 Jan 2001")
+
+        tgis.register_maps_in_space_time_dataset(type="vector", name=self.stvds_abs.get_name(),
+                                                 maps="register_map_1",
+                                                 start="2001-02-01", increment="1 day",
+                                                 interval=True)
+
+        map = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 2, 1))
+        self.assertEqual(end, datetime.datetime(2001, 2, 2))
+
+        self.stvds_abs.select()
+        start, end = self.stvds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 2, 1))
+        self.assertEqual(end, datetime.datetime(2001, 2, 2))
+
+    def test_absolute_time_1(self):
+        """Register vector maps in the temporal database and in addition in a stvds using the object method
+
+        :return:
+        """
+        tgis.register_maps_in_space_time_dataset(type="vector", name=None,
+                 maps="register_map_1,register_map_2",
+                 start="2001-01-01", increment="1 day", interval=True)
+
+        map_1 = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset())
+        map_1.select()
+        start, end = map_1.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 2))
+
+        map_2 = tgis.VectorDataset("register_map_2@" + tgis.get_current_mapset())
+        map_2.select()
+        start, end = map_2.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+        map_list = [map_1, map_2]
+
+        tgis.register_map_object_list(type="vector", map_list=map_list,
+                                      output_stds=self.stvds_abs,
+                                      delete_empty=False)
+        self.stvds_abs.select()
+        start, end = self.stvds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+    def test_absolute_time_2(self):
+        """Register vector maps in the temporal database and in addition
+        in a stvds using the object method deleting empty maps
+
+        :return:
+        """
+        tgis.register_maps_in_space_time_dataset(type="vector", name=None,
+                 maps="register_map_1,register_map_2,register_map_empty",
+                 start="2001-01-01", increment="1 day", interval=True)
+
+        map_1 = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset())
+        map_1.select()
+        start, end = map_1.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 2))
+
+        map_2 = tgis.VectorDataset("register_map_2@" + tgis.get_current_mapset())
+        map_2.select()
+        start, end = map_2.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+        map_3 = tgis.VectorDataset("register_map_empty@" + tgis.get_current_mapset())
+        map_3.select()
+        start, end = map_3.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 3))
+        self.assertEqual(end, datetime.datetime(2001, 1, 4))
+
+        map_list = [map_1, map_2, map_3]
+
+        tgis.register_map_object_list(type="vector", map_list=map_list,
+                                      output_stds=self.stvds_abs,
+                                      delete_empty=True)
+        self.stvds_abs.select()
+        start, end = self.stvds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+        map_3 = tgis.VectorDataset("register_map_empty@" + tgis.get_current_mapset())
+        self.assertEqual(map_3.map_exists(), False)
+
+
 class TestRegisterFails(TestCase):
 
     def test_error_handling_1(self):



More information about the grass-commit mailing list