[GRASS-SVN] r66551 - in grass/trunk/raster/r.univar: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 21 02:50:59 PDT 2015


Author: huhabla
Date: 2015-10-21 02:50:59 -0700 (Wed, 21 Oct 2015)
New Revision: 66551

Added:
   grass/trunk/raster/r.univar/testsuite/
   grass/trunk/raster/r.univar/testsuite/test_r_univar.py
Modified:
   grass/trunk/raster/r.univar/globals.h
   grass/trunk/raster/r.univar/r.univar_main.c
Log:
Added raster region flag "-r" to use the native region 
and resolution of a raster map, independent from the 
current region settings. Added tests.


Modified: grass/trunk/raster/r.univar/globals.h
===================================================================
--- grass/trunk/raster/r.univar/globals.h	2015-10-20 22:14:56 UTC (rev 66550)
+++ grass/trunk/raster/r.univar/globals.h	2015-10-21 09:50:59 UTC (rev 66551)
@@ -56,7 +56,7 @@
 typedef struct
 {
     struct Option *inputfile, *zonefile, *percentile, *output_file, *separator;
-    struct Flag *shell_style, *extended, *table;
+    struct Flag *shell_style, *extended, *table, *use_rast_region;
 } param_type;
 
 extern param_type param;

Modified: grass/trunk/raster/r.univar/r.univar_main.c
===================================================================
--- grass/trunk/raster/r.univar/r.univar_main.c	2015-10-20 22:14:56 UTC (rev 66550)
+++ grass/trunk/raster/r.univar/r.univar_main.c	2015-10-21 09:50:59 UTC (rev 66551)
@@ -71,6 +71,10 @@
     param.table->description = _("Table output format instead of standard output format");
     param.table->guisection = _("Formatting");
 
+    param.use_rast_region = G_define_flag();
+    param.use_rast_region->key = 'r';
+    param.use_rast_region->description = _("Use the native resolution and extent of the raster map, instead of the current region");
+
     return;
 }
 
@@ -115,14 +119,16 @@
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+    if (param.zonefile->answer && param.use_rast_region->answer) {
+    	G_fatal_error(_("zones option and region flag -r are mutually exclusive"));
+    }
+
     name = param.output_file->answer;
     if (name != NULL && strcmp(name, "-") != 0) {
 	if (NULL == freopen(name, "w", stdout)) {
 	    G_fatal_error(_("Unable to open file <%s> for writing"), name);
 	}
     }
-
-    G_get_window(&region);
     
     /* table field separator */
     zone_info.sep = G_option_to_separator(param.separator);
@@ -166,6 +172,18 @@
 	     : 0);
 
     for (p = param.inputfile->answers; *p; p++) {
+
+	/* Check if the native extent and resolution
+	   of the input map should be used */
+	if(param.use_rast_region->answer) {
+    	    mapset = G_find_raster2(*p, "");
+	    Rast_get_cellhd(*p, mapset, &region);
+        } else {
+    	    G_get_window(&region);
+        }
+	/* Set the computational region */
+	Rast_set_window(&region);
+
 	fd = open_raster(*p);
 
 	if (map_type != -1) {
@@ -255,7 +273,7 @@
     const size_t value_sz = Rast_cell_size(map_type);
     unsigned int row;
     void *raster_row;
-    CELL *zoneraster_row;
+    CELL *zoneraster_row = NULL;
     int n_zones = zone_info.n_zones;
     
     raster_row = Rast_allocate_buf(map_type);
@@ -264,7 +282,7 @@
 
     for (row = 0; row < rows; row++) {
 	void *ptr;
-	CELL *zptr;
+	CELL *zptr = NULL;
 	unsigned int col;
 
 	Rast_get_row(fd, raster_row, row, map_type);

Added: grass/trunk/raster/r.univar/testsuite/test_r_univar.py
===================================================================
--- grass/trunk/raster/r.univar/testsuite/test_r_univar.py	                        (rev 0)
+++ grass/trunk/raster/r.univar/testsuite/test_r_univar.py	2015-10-21 09:50:59 UTC (rev 66551)
@@ -0,0 +1,142 @@
+"""Test of r.univar
+
+ at author Soeren Gebbert
+"""
+from grass.gunittest.case import TestCase
+
+class TestRasterUnivar(TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        """Use temporary region settings"""
+        cls.use_temp_region()
+
+    @classmethod
+    def tearDownClass(cls):
+        """!Remove the temporary region
+        """
+        cls.del_temp_region()
+
+    def tearDown(self):
+        self.runModule("g.remove", type="raster", name="map_a")
+        self.runModule("g.remove", type="raster", name="map_b")
+
+    def setUp(self):
+        """Create input data
+        """
+        self.runModule("g.region", res=1, n=90, s=0, w=0, e=90)
+        self.runModule("r.mapcalc", expression="map_a = 100 + row() + col()",
+                       overwrite=True)
+        self.runModule("r.mapcalc", expression="map_b = 200 + row() + col()",
+                       overwrite=True)
+
+    def test_1(self):
+        # Output of r.univar
+        univar_string="""n=8100
+        null_cells=0
+        cells=8100
+        min=102
+        max=280
+        range=178
+        mean=191
+        mean_of_abs=191
+        sum=1547100"""
+
+        self.assertRasterFitsUnivar(raster="map_a",  reference=univar_string,
+                                    precision=3)
+
+    def test_2(self):
+        # Output of r.univar
+        univar_string="""n=81
+        null_cells=0
+        cells=81
+        min=112
+        max=272
+        range=160
+        mean=192
+        mean_of_abs=192
+        sum=15552"""
+
+        self.runModule("g.region", res=10)
+        self.assertRasterFitsUnivar(raster="map_a",  reference=univar_string,
+                                    precision=3)
+
+
+    def test_3(self):
+        """
+        Check the -r flag
+        :return:
+        """
+
+        univar_string="""n=8100
+        null_cells=0
+        cells=8100
+        min=102
+        max=280
+        range=178
+        mean=191
+        mean_of_abs=191
+        sum=1547100"""
+
+        self.runModule("g.region", res=10)
+        self.assertModuleKeyValue(module="r.univar", map="map_a", flags="rg",
+                                  reference=univar_string, precision=3, sep='=')
+
+    def test_multiple_1(self):
+        # Output of r.univar
+        univar_string="""n=16200
+        null_cells=0
+        cells=16200
+        min=102
+        max=380
+        range=278
+        mean=241
+        mean_of_abs=241
+        sum=3904200"""
+
+        self.assertModuleKeyValue(module="r.univar", map=["map_a","map_b"], flags="rg",
+                                  reference=univar_string, precision=3, sep='=')
+
+    def test_multiple_2(self):
+        # Output of r.univar
+        univar_string="""n=162
+        null_cells=0
+        cells=162
+        min=112
+        max=372
+        range=260
+        mean=241
+        mean_of_abs=241
+        sum=39204"""
+
+        self.runModule("g.region", res=10)
+        self.assertModuleKeyValue(module="r.univar", map=["map_a","map_b"], flags="g",
+                                  reference=univar_string, precision=3, sep='=')
+
+
+    def test_multiple_3(self):
+        """
+        Check the -r flag
+        :return:
+        """
+
+        # Output of r.univar
+        univar_string="""n=16200
+        null_cells=0
+        cells=16200
+        min=102
+        max=380
+        range=278
+        mean=241
+        mean_of_abs=241
+        sum=3904200"""
+
+        self.runModule("g.region", res=10)
+        self.assertModuleKeyValue(module="r.univar", map=["map_a","map_b"], flags="rg",
+                                  reference=univar_string, precision=3, sep='=')
+
+if __name__ == '__main__':
+    from grass.gunittest.main import test
+    test()
+
+



More information about the grass-commit mailing list