test_raster_img.py 5.7 KB

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