[GRASS-SVN] r66077 - in grass/trunk/lib/python/pygrass/raster: . testsuite testsuite/data

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Sep 1 06:26:08 PDT 2015


Author: huhabla
Date: 2015-09-01 06:26:08 -0700 (Tue, 01 Sep 2015)
New Revision: 66077

Added:
   grass/trunk/lib/python/pygrass/raster/testsuite/data/d.png
   grass/trunk/lib/python/pygrass/raster/testsuite/data/e.png
Modified:
   grass/trunk/lib/python/pygrass/raster/__init__.py
   grass/trunk/lib/python/pygrass/raster/testsuite/test_raster_img.py
Log:
pygrass raster: Added 32Bit RGB image generation and tests


Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py	2015-09-01 13:22:50 UTC (rev 66076)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py	2015-09-01 13:26:08 UTC (rev 66077)
@@ -99,8 +99,8 @@
          ('P', 44, None)]
 
         >>> elev.cats.labels() # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
-        ['A', 'B', 'C', 'D', 'E', 
-         'F', 'G', 'H', 'I', 'J', 
+        ['A', 'B', 'C', 'D', 'E',
+         'F', 'G', 'H', 'I', 'J',
          'K', 'L', 'M', 'n', 'O', 'P']
         >>> elev.cats[0]
         ('A', 11, None)
@@ -273,7 +273,7 @@
             Buffer([12, 22, 32, 42], dtype=int32)
             Buffer([13, 23, 33, 43], dtype=int32)
             Buffer([14, 24, 34, 44], dtype=int32)
-        
+
             >>> elev.close()
 
         """
@@ -373,7 +373,7 @@
             Buffer([12, 22, 32, 42], dtype=int32)
             Buffer([13, 23, 33, 43], dtype=int32)
             Buffer([14, 24, 34, 44], dtype=int32)
-        
+
             >>> elev.close()
 
 
@@ -616,34 +616,37 @@
 
 def raster2numpy_img(rastname, region, color="ARGB", array=None):
     """Convert a raster map layer into a string with
-       32Bit ARGB or 8Bit Gray little endian encoding.
-    
+       32Bit ARGB, 24Bit RGB or 8Bit Gray little endian encoding.
+
         Return a numpy array from a raster map of type uint8
-        that contains the colored map data as 32 bit ARGB or 8 bit image
+        that contains the colored map data as 32 bit ARGB, 32Bit RGB
+        or 8 bit image
 
        :param rastname: The name of raster map
        :type rastname: string
-       
+
        :param region: The region to be used for raster map reading
        :type region: grass.pygrass.gis.region.Region
-       
-       :param color: "ARGB", "GRAY1", "GRAY2"
-                     ARGB  -> 32Bit RGB with alpha channel
+
+       :param color: "ARGB", "RGB", "GRAY1", "GRAY2"
+                     ARGB  -> 32Bit RGB with alpha channel (0xAARRGGBB)
+                     RGB   -> 32Bit RGB (0xffRRGGBB)
                      GRAY1 -> grey scale formular: .33R+ .5G+ .17B
                      GRAY2 -> grey scale formular: .30R+ .59G+ .11B
        :type color: String
 
-       :param array: A numpy array (optional) to store the image, 
+       :param array: A numpy array (optional) to store the image,
                      the array needs to setup as follows:
-                     
+
                      array = np.ndarray((region.rows*region.cols*scale), np.uint8)
-                     
-                     scale is 4 in case of ARGB or 1 in case of Gray scale
+
+                     scale = 4 in case of ARGB and RGB or scale = 1
+                     in case of Gray scale
        :type array: numpy.ndarray
-       
-       :return: A numpy array of size rows*cols*4 in case of ARGB and
+
+       :return: A numpy array of size rows*cols*4 in case of ARGB, RGB and
                 rows*cols*1 in case of gray scale
-    
+
        Attention: This function will change the computational raster region
        of the current process while running.
     """
@@ -651,27 +654,30 @@
     region_orig = deepcopy(region)
     # Set the raster region
     region.set_raster_region()
-    
+
     scale = 1
     color_mode = 1
     if color.upper() == "ARGB":
         scale = 4
         color_mode = 1
+    elif color.upper() == "RGB":
+        scale = 4
+        color_mode = 2
     elif color.upper() == "GRAY1":
         scale = 1
-        color_mode = 2
+        color_mode = 3
     elif color.upper() == "GRAY2":
         scale = 1
-        color_mode = 3
-        
+        color_mode = 4
+
     if array is None:
         array = np.ndarray((region.rows*region.cols*scale), np.uint8)
 
-    libraster.Rast_map_to_img_str(rastname, color_mode, 
+    libraster.Rast_map_to_img_str(rastname, color_mode,
                                   array.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)))
     # Restore the raster region
     region_orig.set_raster_region()
-    
+
     return array
 
 
@@ -730,8 +736,8 @@
     mset = utils.get_mapset_raster(test_raster_name, mapset='')
     if mset:
         Module("g.remove", flags='f', type='raster', name=test_raster_name)
-    mset = utils.get_mapset_raster(test_raster_name + "_segment", 
+    mset = utils.get_mapset_raster(test_raster_name + "_segment",
                                    mapset='')
     if mset:
-        Module("g.remove", flags='f', type='raster', 
+        Module("g.remove", flags='f', type='raster',
                name=test_raster_name + "_segment")

Added: grass/trunk/lib/python/pygrass/raster/testsuite/data/d.png
===================================================================
(Binary files differ)


Property changes on: grass/trunk/lib/python/pygrass/raster/testsuite/data/d.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: grass/trunk/lib/python/pygrass/raster/testsuite/data/e.png
===================================================================
(Binary files differ)


Property changes on: grass/trunk/lib/python/pygrass/raster/testsuite/data/e.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Modified: grass/trunk/lib/python/pygrass/raster/testsuite/test_raster_img.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/testsuite/test_raster_img.py	2015-09-01 13:22:50 UTC (rev 66076)
+++ grass/trunk/lib/python/pygrass/raster/testsuite/test_raster_img.py	2015-09-01 13:26:08 UTC (rev 66077)
@@ -36,120 +36,165 @@
     @classmethod
     def tearDownClass(cls):
         """Remove the generated vector map, if exist"""
-        cls.runModule("g.remove", flags='f', type='raster', 
+        cls.runModule("g.remove", flags='f', type='raster',
                       name=cls.name)
         cls.del_temp_region()
 
     @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
     def test_resampling_to_QImg_1(self):
-        
+
         region = Region()
         region.from_rast(self.name)
         region.cols = 320
         region.rows = 240
         region.adjust()
-        
+
         tmpfile = tempfile(False)
         tmpfile = tmpfile + ".png"
 
         a = raster2numpy_img(self.name, region)
-        
-        image = QImage(a.data, region.cols, region.rows, 
+
+        image = QImage(a.data, region.cols, region.rows,
                        QImage.Format_ARGB32)
         #image.save("data/a.png")
         image.save(tmpfile)
         self.assertFilesEqualMd5(tmpfile, "data/a.png")
-        
+
     @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
     def test_resampling_to_QImg_2(self):
-        
+
         region = Region()
         region.from_rast(self.name)
         region.cols = 640
         region.rows = 480
         region.adjust()
-        
+
         tmpfile = tempfile(False)
         tmpfile = tmpfile + ".png"
 
         # With array as argument
         array = np.ndarray((region.rows*region.cols*4), np.uint8)
 
-        raster2numpy_img(rastname=self.name, region=region, 
+        raster2numpy_img(rastname=self.name, region=region,
                          color="ARGB", array=array)
-        
-        image = QImage(array.data, 
+
+        image = QImage(array.data,
                        region.cols, region.rows, QImage.Format_ARGB32)
         #image.save("data/b.png")
         image.save(tmpfile)
         self.assertFilesEqualMd5(tmpfile, "data/b.png")
-        
+
     @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
     def test_resampling_to_QImg_large(self):
-        
+
         region = Region()
         region.from_rast(self.name)
         region.cols = 4000
         region.rows = 3000
         region.adjust()
-        
+
         tmpfile = tempfile(False)
         tmpfile = tmpfile + ".png"
 
         # With array as argument
         array = np.ndarray((region.rows*region.cols*4), np.uint8)
 
-        raster2numpy_img(rastname=self.name, region=region, 
+        raster2numpy_img(rastname=self.name, region=region,
                          color="ARGB", array=array)
-        
-        image = QImage(array.data, 
+
+        image = QImage(array.data,
                        region.cols, region.rows, QImage.Format_ARGB32)
         #image.save("data/c.png")
         image.save(tmpfile)
         self.assertFilesEqualMd5(tmpfile, "data/c.png")
-        
+
+    @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
+    def test_resampling_to_QImg_3(self):
+
+        region = Region()
+        region.from_rast(self.name)
+        region.cols = 400
+        region.rows = 300
+        region.adjust()
+
+        tmpfile = tempfile(False)
+        tmpfile = tmpfile + ".png"
+
+        # With array as argument
+        array = np.ndarray((region.rows*region.cols*4), np.uint8)
+
+        raster2numpy_img(rastname=self.name, region=region,
+                         color="RGB", array=array)
+
+        image = QImage(array.data,
+                       region.cols, region.rows, QImage.Format_RGB32)
+        #image.save("data/d.png")
+        image.save(tmpfile)
+        self.assertFilesEqualMd5(tmpfile, "data/d.png")
+
+    @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
+    def test_resampling_to_QImg_4(self):
+
+        region = Region()
+        region.from_rast(self.name)
+        region.cols = 400
+        region.rows = 300
+        region.adjust()
+
+        tmpfile = tempfile(False)
+        tmpfile = tmpfile + ".png"
+
+        array = raster2numpy_img(rastname=self.name, region=region,
+                                 color="RGB")
+
+        image = QImage(array.data,
+                       region.cols, region.rows, QImage.Format_RGB32)
+        #image.save("data/e.png")
+        image.save(tmpfile)
+        self.assertFilesEqualMd5(tmpfile, "data/e.png")
+
     def test_resampling_to_numpy_img_1(self):
-                
+
         region = Region()
         region.ewres = 10
         region.nsres = 10
         region.adjust(rows=True, cols=True)
-        
+
         a = raster2numpy_img(self.name, region)
-        
+
         self.assertEqual(len(a), region.rows * region.cols*4)
 
     def test_resampling_to_numpy_img_2(self):
-                
+
         region = Region()
         region.ewres = 1
         region.nsres = 1
         region.adjust(rows=True, cols=True)
-        
+
         a = raster2numpy_img(self.name, region)
-        
+
         self.assertEqual(len(a), region.rows * region.cols*4)
 
     def test_resampling_to_numpy_img_3(self):
-                
+
         region = Region()
         region.ewres = 0.4
         region.nsres = 0.4
         region.adjust(rows=True, cols=True)
-        
+
         a = raster2numpy_img(self.name, region, color="GRAY1")
-        
+
         self.assertEqual(len(a), region.rows * region.cols*1)
 
     def test_resampling_to_numpy_img_4(self):
-                
+
         region = Region()
         region.ewres = 0.1
         region.nsres = 0.1
         region.adjust(rows=True, cols=True)
-        
+
         a = raster2numpy_img(self.name, region, color="GRAY2")
-        
+
         self.assertEqual(len(a), region.rows * region.cols*1)
 
 if __name__ == '__main__':



More information about the grass-commit mailing list