test_raster_img.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(
  24. "r.mapcalc",
  25. expression="%s = if(row() >= 10 && row() <= 60, null(), row() + (10.0 * col()))"
  26. % (cls.name),
  27. overwrite=True,
  28. )
  29. cls.runModule("r.colors", map=cls.name, color="elevation")
  30. @classmethod
  31. def tearDownClass(cls):
  32. """Remove the generated vector map, if exist"""
  33. cls.runModule("g.remove", flags="f", type="raster", 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, QImage.Format_ARGB32)
  46. # image.save("data/a.png")
  47. image.save(tmpfile)
  48. self.assertFilesEqualMd5(tmpfile, "data/a.png")
  49. @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
  50. def test_resampling_to_QImg_2(self):
  51. region = Region()
  52. region.from_rast(self.name)
  53. region.cols = 640
  54. region.rows = 480
  55. region.adjust()
  56. tmpfile = tempfile(False)
  57. tmpfile = tmpfile + ".png"
  58. # With array as argument
  59. array = np.ndarray((region.rows * region.cols * 4), np.uint8)
  60. raster2numpy_img(rastname=self.name, region=region, color="ARGB", array=array)
  61. image = QImage(array.data, region.cols, region.rows, QImage.Format_ARGB32)
  62. # image.save("data/b.png")
  63. image.save(tmpfile)
  64. self.assertFilesEqualMd5(tmpfile, "data/b.png")
  65. @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
  66. def test_resampling_to_QImg_large(self):
  67. region = Region()
  68. region.from_rast(self.name)
  69. region.cols = 4000
  70. region.rows = 3000
  71. region.adjust()
  72. tmpfile = tempfile(False)
  73. tmpfile = tmpfile + ".png"
  74. # With array as argument
  75. array = np.ndarray((region.rows * region.cols * 4), np.uint8)
  76. raster2numpy_img(rastname=self.name, region=region, color="ARGB", array=array)
  77. image = QImage(array.data, region.cols, region.rows, QImage.Format_ARGB32)
  78. # image.save("data/c.png")
  79. image.save(tmpfile)
  80. self.assertFilesEqualMd5(tmpfile, "data/c.png")
  81. @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
  82. def test_resampling_to_QImg_3(self):
  83. region = Region()
  84. region.from_rast(self.name)
  85. region.cols = 400
  86. region.rows = 300
  87. region.adjust()
  88. tmpfile = tempfile(False)
  89. tmpfile = tmpfile + ".png"
  90. # With array as argument
  91. array = np.ndarray((region.rows * region.cols * 4), np.uint8)
  92. raster2numpy_img(rastname=self.name, region=region, color="RGB", array=array)
  93. image = QImage(array.data, region.cols, region.rows, QImage.Format_RGB32)
  94. # image.save("data/d.png")
  95. image.save(tmpfile)
  96. self.assertFilesEqualMd5(tmpfile, "data/d.png")
  97. @unittest.skipIf(has_PyQt4 is False, "Require PyQt4")
  98. def test_resampling_to_QImg_4(self):
  99. region = Region()
  100. region.from_rast(self.name)
  101. region.cols = 400
  102. region.rows = 300
  103. region.adjust()
  104. tmpfile = tempfile(False)
  105. tmpfile = tmpfile + ".png"
  106. array = raster2numpy_img(rastname=self.name, region=region, color="RGB")
  107. image = QImage(array.data, region.cols, region.rows, QImage.Format_RGB32)
  108. # image.save("data/e.png")
  109. image.save(tmpfile)
  110. self.assertFilesEqualMd5(tmpfile, "data/e.png")
  111. def test_resampling_to_numpy_img_1(self):
  112. region = Region()
  113. region.ewres = 10
  114. region.nsres = 10
  115. region.adjust(rows=True, cols=True)
  116. a = raster2numpy_img(self.name, region)
  117. self.assertEqual(len(a), region.rows * region.cols * 4)
  118. def test_resampling_to_numpy_img_2(self):
  119. region = Region()
  120. region.ewres = 1
  121. region.nsres = 1
  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_3(self):
  126. region = Region()
  127. region.ewres = 0.4
  128. region.nsres = 0.4
  129. region.adjust(rows=True, cols=True)
  130. a = raster2numpy_img(self.name, region, color="GRAY1")
  131. self.assertEqual(len(a), region.rows * region.cols * 1)
  132. def test_resampling_to_numpy_img_4(self):
  133. region = Region()
  134. region.ewres = 0.1
  135. region.nsres = 0.1
  136. region.adjust(rows=True, cols=True)
  137. a = raster2numpy_img(self.name, region, color="GRAY2")
  138. self.assertEqual(len(a), region.rows * region.cols * 1)
  139. if __name__ == "__main__":
  140. test()