test_raster_img.py 5.5 KB

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