test_small_data.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: test_small_data
  5. # AUTHOR: Vaclav Petras
  6. # PURPOSE: Fast test using a small example
  7. # COPYRIGHT: (C) 2016 by Vaclav Petras and the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. #
  13. #############################################################################
  14. from grass.gunittest.case import TestCase
  15. from grass.gunittest.main import test
  16. from grass.script import list_strings
  17. # generated by
  18. # g.region n=12 s=9 e=21 w=18 t=8 b=4 res=1 res3=1 -p3
  19. # r3.mapcalc "x = rand(0,10)" seed=100 && r3.out.ascii x prec=0
  20. INPUT_A = """\
  21. version: grass7
  22. order: nsbt
  23. north: 12
  24. south: 9
  25. east: 21
  26. west: 18
  27. top: 8
  28. bottom: 4
  29. rows: 3
  30. cols: 3
  31. levels: 4
  32. 6 5 1
  33. 0 7 5
  34. 1 7 1
  35. 8 2 1
  36. 3 4 2
  37. 8 5 6
  38. 1 2 8
  39. 1 5 5
  40. 1 1 3
  41. 1 8 3
  42. 6 5 1
  43. 5 1 7
  44. """
  45. # created from the above and template from
  46. # r.mapcalc "x = rand(0,10)" seed=100 && r.out.ascii x prec=0
  47. OUTPUTS_A = [
  48. """\
  49. north: 12
  50. south: 9
  51. east: 21
  52. west: 18
  53. rows: 3
  54. cols: 3
  55. 6 5 1
  56. 0 7 5
  57. 1 7 1
  58. """,
  59. """\
  60. north: 12
  61. south: 9
  62. east: 21
  63. west: 18
  64. rows: 3
  65. cols: 3
  66. 8 2 1
  67. 3 4 2
  68. 8 5 6
  69. """,
  70. """\
  71. north: 12
  72. south: 9
  73. east: 21
  74. west: 18
  75. rows: 3
  76. cols: 3
  77. 1 2 8
  78. 1 5 5
  79. 1 1 3
  80. """,
  81. """\
  82. north: 12
  83. south: 9
  84. east: 21
  85. west: 18
  86. rows: 3
  87. cols: 3
  88. 1 8 3
  89. 6 5 1
  90. 5 1 7
  91. """,
  92. ]
  93. class TestR3ToRast(TestCase):
  94. # TODO: replace by unified handing of maps
  95. # mixing class and object attributes
  96. to_remove_3d = []
  97. to_remove_2d = []
  98. rast3d = "r3_to_rast_test"
  99. rast2d = "r3_to_rast_test"
  100. rast2d_ref = "r3_to_rast_test_ref"
  101. rast2d_refs = []
  102. def setUp(self):
  103. self.use_temp_region()
  104. self.runModule("r3.in.ascii", input="-", stdin_=INPUT_A, output=self.rast3d)
  105. self.to_remove_3d.append(self.rast3d)
  106. self.runModule("g.region", raster_3d=self.rast3d)
  107. for i, data in enumerate(OUTPUTS_A):
  108. rast = "%s_%d" % (self.rast2d_ref, i)
  109. self.runModule("r.in.ascii", input="-", stdin_=data, output=rast)
  110. self.to_remove_2d.append(rast)
  111. self.rast2d_refs.append(rast)
  112. def tearDown(self):
  113. if self.to_remove_3d:
  114. self.runModule(
  115. "g.remove",
  116. flags="f",
  117. type="raster_3d",
  118. name=",".join(self.to_remove_3d),
  119. verbose=True,
  120. )
  121. if self.to_remove_2d:
  122. self.runModule(
  123. "g.remove",
  124. flags="f",
  125. type="raster",
  126. name=",".join(self.to_remove_2d),
  127. verbose=True,
  128. )
  129. self.del_temp_region()
  130. def test_a(self):
  131. self.assertModule("r3.to.rast", input=self.rast3d, output=self.rast2d)
  132. rasts = list_strings(
  133. "raster",
  134. mapset=".",
  135. pattern="%s_*" % self.rast2d,
  136. exclude="%s_*" % self.rast2d_ref,
  137. )
  138. self.assertEquals(
  139. len(rasts), 4, msg="Wrong number of 2D rasters present" " in the mapset"
  140. )
  141. ref_info = dict(cells=9)
  142. ref_univar = dict(cells=9, null_cells=0)
  143. for rast in rasts:
  144. self.assertRasterExists(rast)
  145. # the following doesn't make much sense because we just listed them
  146. self.to_remove_2d.append(rast)
  147. self.assertRasterFitsInfo(raster=rast, reference=ref_info, precision=0)
  148. self.assertRasterFitsUnivar(raster=rast, reference=ref_univar, precision=0)
  149. # check the actual values
  150. for rast_ref, rast in zip(self.rast2d_refs, rasts):
  151. self.assertRastersNoDifference(
  152. actual=rast, reference=rast_ref, precision=0.1
  153. )
  154. if __name__ == "__main__":
  155. test()