test_rpatch_artificial.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. """
  2. TEST: r.patch
  3. AUTHOR(S): Vaclav Petras
  4. PURPOSE: Test r.patch using artificial and small data
  5. COPYRIGHT: (C) 2015 Vaclav Petras, and by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. """
  10. from grass.gunittest.case import TestCase
  11. from grass.gunittest.main import test
  12. cell_1 = """\
  13. north: 20
  14. south: 10
  15. east: 25
  16. west: 15
  17. rows: 4
  18. cols: 4
  19. 505 501 500 520
  20. 506 501 550 520
  21. 504 401 400 520
  22. 502 520 540 520
  23. """
  24. cell_2 = """\
  25. north: 30
  26. south: 20
  27. east: 25
  28. west: 15
  29. rows: 4
  30. cols: 4
  31. 505 505 500 520
  32. 406 409 550 520
  33. 304 405 400 560
  34. 302 520 540 520
  35. """
  36. cell_patched_ref = """\
  37. north: 30
  38. south: 10
  39. east: 25
  40. west: 15
  41. rows: 8
  42. cols: 4
  43. 505 505 500 520
  44. 406 409 550 520
  45. 304 405 400 560
  46. 302 520 540 520
  47. 505 501 500 520
  48. 506 501 550 520
  49. 504 401 400 520
  50. 502 520 540 520
  51. """
  52. # the following comes from the manual
  53. cell_overlap_a = """\
  54. north: 35
  55. south: 20
  56. east: 25
  57. west: 15
  58. rows: 6
  59. cols: 8
  60. 1 1 1 0 2 2 0 0
  61. 1 1 0 2 2 2 0 0
  62. 3 3 3 3 2 2 0 0
  63. 3 3 3 3 0 0 0 0
  64. 3 3 3 0 0 0 0 0
  65. 0 0 0 0 0 0 0 0
  66. """
  67. cell_overlap_b = """\
  68. north: 35
  69. south: 20
  70. east: 25
  71. west: 15
  72. rows: 6
  73. cols: 8
  74. 0 0 1 1 0 0 0 0
  75. 0 0 1 1 0 0 0 0
  76. 0 0 0 0 0 0 0 0
  77. 4 4 4 4 4 4 4 4
  78. 4 4 4 4 4 4 4 4
  79. 4 4 4 4 4 4 4 4
  80. """
  81. cell_overlap_ab = """\
  82. north: 35
  83. south: 20
  84. east: 25
  85. west: 15
  86. rows: 6
  87. cols: 8
  88. 1 1 1 1 2 2 0 0
  89. 1 1 1 2 2 2 0 0
  90. 3 3 3 3 2 2 0 0
  91. 3 3 3 3 4 4 4 4
  92. 3 3 3 4 4 4 4 4
  93. 4 4 4 4 4 4 4 4
  94. """
  95. cell_overlap_ba = """\
  96. north: 35
  97. south: 20
  98. east: 25
  99. west: 15
  100. rows: 6
  101. cols: 8
  102. 1 1 1 1 2 2 0 0
  103. 1 1 1 1 2 2 0 0
  104. 3 3 3 3 2 2 0 0
  105. 4 4 4 4 4 4 4 4
  106. 4 4 4 4 4 4 4 4
  107. 4 4 4 4 4 4 4 4
  108. """
  109. class TestSmallDataNoOverlap(TestCase):
  110. # TODO: replace by unified handing of maps
  111. to_remove = []
  112. cell_1 = "rpatch_small_test_cell_1"
  113. cell_2 = "rpatch_small_test_cell_2"
  114. cell_patched = "rpatch_small_test_cell_patched"
  115. cell_patched_threaded = "rpatch_small_test_cell_patched_threaded"
  116. cell_patched_ref = "rpatch_small_test_cell_patched_ref"
  117. @classmethod
  118. def setUpClass(cls):
  119. cls.use_temp_region()
  120. # 10 / 4 == 2.5 (size of raster / number of cells)
  121. cls.runModule("g.region", n=30, s=10, e=25, w=15, res=2.5)
  122. @classmethod
  123. def tearDownClass(cls):
  124. cls.del_temp_region()
  125. if cls.to_remove:
  126. cls.runModule(
  127. "g.remove",
  128. flags="f",
  129. type="raster",
  130. name=",".join(cls.to_remove),
  131. verbose=True,
  132. )
  133. def test_patching_cell(self):
  134. """Test patching two neighboring CELL raster maps"""
  135. self.runModule("r.in.ascii", input="-", stdin=cell_1, output=self.cell_1)
  136. self.to_remove.append(self.cell_1)
  137. self.runModule("r.in.ascii", input="-", stdin=cell_2, output=self.cell_2)
  138. self.to_remove.append(self.cell_2)
  139. self.assertModule(
  140. "r.patch", input=(self.cell_1, self.cell_2), output=self.cell_patched
  141. )
  142. self.assertModule(
  143. "r.patch",
  144. input=(self.cell_1, self.cell_2),
  145. nprocs=8,
  146. output=self.cell_patched_threaded,
  147. )
  148. self.to_remove.append(self.cell_patched)
  149. self.to_remove.append(self.cell_patched_threaded)
  150. self.runModule(
  151. "r.in.ascii",
  152. input="-",
  153. stdin=cell_patched_ref,
  154. output=self.cell_patched_ref,
  155. )
  156. self.to_remove.append(self.cell_patched_ref)
  157. self.assertRastersNoDifference(
  158. self.cell_patched, self.cell_patched_ref, precision=0
  159. )
  160. self.assertRastersNoDifference(
  161. self.cell_patched_threaded, self.cell_patched_ref, precision=0
  162. )
  163. class TestSmallDataOverlap(TestCase):
  164. # TODO: replace by unified handing of maps
  165. to_remove = []
  166. cell_a = "rpatch_small_test_cell_a"
  167. cell_b = "rpatch_small_test_cell_b"
  168. cell_ab = "rpatch_small_test_cell_ab_reference"
  169. cell_ba = "rpatch_small_test_cell_ba_reference"
  170. cell_ab_result = "rpatch_small_test_cell_ab_result"
  171. cell_ab_result_threaded = "rpatch_small_test_cell_ab_result_threaded"
  172. cell_ba_result = "rpatch_small_test_cell_ba_result"
  173. cell_ba_result_threaded = "rpatch_small_test_cell_ba_result_threaded"
  174. @classmethod
  175. def setUpClass(cls):
  176. cls.use_temp_region()
  177. # 15 / 6 == 2.5 ((n-s) / number of cells)
  178. # 10 / 8 == 1.25 ((e-w) / number of cells)
  179. cls.runModule("g.region", n=35, s=20, e=25, w=15, nsres=2.5, ewres=1.25)
  180. cls.runModule("r.in.ascii", input="-", stdin=cell_overlap_a, output=cls.cell_a)
  181. cls.to_remove.append(cls.cell_a)
  182. cls.runModule("r.in.ascii", input="-", stdin=cell_overlap_b, output=cls.cell_b)
  183. cls.to_remove.append(cls.cell_b)
  184. cls.runModule(
  185. "r.in.ascii", input="-", stdin=cell_overlap_ab, output=cls.cell_ab
  186. )
  187. cls.to_remove.append(cls.cell_ab)
  188. cls.runModule(
  189. "r.in.ascii", input="-", stdin=cell_overlap_ba, output=cls.cell_ba
  190. )
  191. cls.to_remove.append(cls.cell_ba)
  192. @classmethod
  193. def tearDownClass(cls):
  194. cls.del_temp_region()
  195. if cls.to_remove:
  196. cls.runModule(
  197. "g.remove",
  198. flags="f",
  199. type="raster",
  200. name=",".join(cls.to_remove),
  201. verbose=True,
  202. )
  203. def test_patch_oder_ab_cell(self):
  204. """Test patching two overlapping CELL raster maps (watching order)"""
  205. self.assertModule(
  206. "r.patch",
  207. input=(self.cell_a, self.cell_b),
  208. output=self.cell_ab_result,
  209. flags="z",
  210. )
  211. self.assertModule(
  212. "r.patch",
  213. input=(self.cell_a, self.cell_b),
  214. output=self.cell_ab_result_threaded,
  215. flags="z",
  216. nprocs=8,
  217. )
  218. self.assertRasterExists(self.cell_ab_result)
  219. self.assertRasterExists(self.cell_ab_result_threaded)
  220. self.to_remove.append(self.cell_ab_result)
  221. self.to_remove.append(self.cell_ab_result_threaded)
  222. self.assertRastersNoDifference(self.cell_ab_result, self.cell_ab, precision=0)
  223. self.assertRastersNoDifference(
  224. self.cell_ab_result_threaded, self.cell_ab, precision=0
  225. )
  226. def test_patch_oder_ba_cell(self):
  227. """Test patching two overlapping CELL raster maps (watching order)"""
  228. self.assertModule(
  229. "r.patch",
  230. input=(self.cell_b, self.cell_a),
  231. output=self.cell_ba_result,
  232. flags="z",
  233. )
  234. self.assertModule(
  235. "r.patch",
  236. input=(self.cell_b, self.cell_a),
  237. flags="z",
  238. output=self.cell_ba_result_threaded,
  239. nprocs=8,
  240. )
  241. self.assertRasterExists(self.cell_ba_result)
  242. self.assertRasterExists(self.cell_ba_result_threaded)
  243. self.to_remove.append(self.cell_ba_result)
  244. self.to_remove.append(self.cell_ba_result_threaded)
  245. self.assertRastersNoDifference(self.cell_ba_result, self.cell_ba, precision=0)
  246. self.assertRastersNoDifference(
  247. self.cell_ba_result_threaded, self.cell_ba, precision=0
  248. )
  249. if __name__ == "__main__":
  250. test()