test_raster_img.py 5.7 KB

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