1# -*- coding: utf-8 2import numpy as np 3import unittest 4 5from grass.gunittest.case import TestCase 6from grass.gunittest.main import test 7 8from grass.pygrass.raster import raster2numpy_img 9from grass.pygrass.gis.region import Region 10from grass.script.core import tempfile 11 12has_PyQt4 = False 13try: 14 from PyQt4.QtCore import * 15 from PyQt4.QtGui import * 16 has_PyQt4 = True 17except: 18 pass 19 20 21class RasterRowImgTestCase(TestCase): 22 23 name = "RasterRowImgTestCase_map" 24 25 @classmethod 26 def setUpClass(cls): 27 """Create test raster map and region""" 28 cls.use_temp_region() 29 cls.runModule("g.region", n=60, s=0, e=40, w=0, res=0.1) 30 cls.runModule("r.mapcalc", 31 expression="%s = if(row() >= 10 && row() <= 60, null(), row() + (10.0 * col()))" % (cls.name), 32 overwrite=True) 33 cls.runModule("r.colors", map=cls.name, color="elevation") 34 35 @classmethod 36 def tearDownClass(cls): 37 """Remove the generated vector map, if exist""" 38 cls.runModule("g.remove", flags='f', type='raster', 39 name=cls.name) 40 cls.del_temp_region() 41 42 @unittest.skipIf(has_PyQt4 is False, "Require PyQt4") 43 def test_resampling_to_QImg_1(self): 44 45 region = Region() 46 region.from_rast(self.name) 47 region.cols = 320 48 region.rows = 240 49 region.adjust() 50 51 tmpfile = tempfile(False) 52 tmpfile = tmpfile + ".png" 53 54 a = raster2numpy_img(self.name, region) 55 56 image = QImage(a.data, region.cols, region.rows, 57 QImage.Format_ARGB32) 58 # image.save("data/a.png") 59 image.save(tmpfile) 60 self.assertFilesEqualMd5(tmpfile, "data/a.png") 61 62 @unittest.skipIf(has_PyQt4 is False, "Require PyQt4") 63 def test_resampling_to_QImg_2(self): 64 65 region = Region() 66 region.from_rast(self.name) 67 region.cols = 640 68 region.rows = 480 69 region.adjust() 70 71 tmpfile = tempfile(False) 72 tmpfile = tmpfile + ".png" 73 74 # With array as argument 75 array = np.ndarray((region.rows * region.cols * 4), np.uint8) 76 77 raster2numpy_img(rastname=self.name, region=region, 78 color="ARGB", array=array) 79 80 image = QImage(array.data, 81 region.cols, region.rows, QImage.Format_ARGB32) 82 # image.save("data/b.png") 83 image.save(tmpfile) 84 self.assertFilesEqualMd5(tmpfile, "data/b.png") 85 86 @unittest.skipIf(has_PyQt4 is False, "Require PyQt4") 87 def test_resampling_to_QImg_large(self): 88 89 region = Region() 90 region.from_rast(self.name) 91 region.cols = 4000 92 region.rows = 3000 93 region.adjust() 94 95 tmpfile = tempfile(False) 96 tmpfile = tmpfile + ".png" 97 98 # With array as argument 99 array = np.ndarray((region.rows * region.cols * 4), np.uint8) 100 101 raster2numpy_img(rastname=self.name, region=region, 102 color="ARGB", array=array) 103 104 image = QImage(array.data, 105 region.cols, region.rows, QImage.Format_ARGB32) 106 # image.save("data/c.png") 107 image.save(tmpfile) 108 self.assertFilesEqualMd5(tmpfile, "data/c.png") 109 110 @unittest.skipIf(has_PyQt4 is False, "Require PyQt4") 111 def test_resampling_to_QImg_3(self): 112 113 region = Region() 114 region.from_rast(self.name) 115 region.cols = 400 116 region.rows = 300 117 region.adjust() 118 119 tmpfile = tempfile(False) 120 tmpfile = tmpfile + ".png" 121 122 # With array as argument 123 array = np.ndarray((region.rows * region.cols * 4), np.uint8) 124 125 raster2numpy_img(rastname=self.name, region=region, 126 color="RGB", array=array) 127 128 image = QImage(array.data, 129 region.cols, region.rows, QImage.Format_RGB32) 130 # image.save("data/d.png") 131 image.save(tmpfile) 132 self.assertFilesEqualMd5(tmpfile, "data/d.png") 133 134 @unittest.skipIf(has_PyQt4 is False, "Require PyQt4") 135 def test_resampling_to_QImg_4(self): 136 137 region = Region() 138 region.from_rast(self.name) 139 region.cols = 400 140 region.rows = 300 141 region.adjust() 142 143 tmpfile = tempfile(False) 144 tmpfile = tmpfile + ".png" 145 146 array = raster2numpy_img(rastname=self.name, region=region, 147 color="RGB") 148 149 image = QImage(array.data, 150 region.cols, region.rows, QImage.Format_RGB32) 151 # image.save("data/e.png") 152 image.save(tmpfile) 153 self.assertFilesEqualMd5(tmpfile, "data/e.png") 154 155 def test_resampling_to_numpy_img_1(self): 156 157 region = Region() 158 region.ewres = 10 159 region.nsres = 10 160 region.adjust(rows=True, cols=True) 161 162 a = raster2numpy_img(self.name, region) 163 164 self.assertEqual(len(a), region.rows * region.cols * 4) 165 166 def test_resampling_to_numpy_img_2(self): 167 168 region = Region() 169 region.ewres = 1 170 region.nsres = 1 171 region.adjust(rows=True, cols=True) 172 173 a = raster2numpy_img(self.name, region) 174 175 self.assertEqual(len(a), region.rows * region.cols * 4) 176 177 def test_resampling_to_numpy_img_3(self): 178 179 region = Region() 180 region.ewres = 0.4 181 region.nsres = 0.4 182 region.adjust(rows=True, cols=True) 183 184 a = raster2numpy_img(self.name, region, color="GRAY1") 185 186 self.assertEqual(len(a), region.rows * region.cols * 1) 187 188 def test_resampling_to_numpy_img_4(self): 189 190 region = Region() 191 region.ewres = 0.1 192 region.nsres = 0.1 193 region.adjust(rows=True, cols=True) 194 195 a = raster2numpy_img(self.name, region, color="GRAY2") 196 197 self.assertEqual(len(a), region.rows * region.cols * 1) 198 199if __name__ == '__main__': 200 test() 201