decimation_test.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """
  2. Name: decimation_test
  3. Purpose: v.in.lidar decimation test
  4. Author: Vaclav Petras
  5. Copyright: (C) 2015 by Vaclav Petras and the GRASS Development Team
  6. Licence: 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. import os
  11. from grass.gunittest.case import TestCase
  12. from grass.gunittest.main import test
  13. class TestCountBasedDecimation(TestCase):
  14. """Test case for watershed module
  15. This tests expects v.random and v.out.lidar to work properly.
  16. """
  17. # Setup variables to be used for outputs
  18. vector_points = "vinlidar_decimation_original"
  19. imported_points = "vinlidar_decimation_imported"
  20. las_file = "vinlidar_decimation_points.las"
  21. npoints = 300 # the values works well for 300 without rounding
  22. @classmethod
  23. def setUpClass(cls):
  24. """Ensures expected computational region and generated data"""
  25. cls.use_temp_region()
  26. cls.runModule("g.region", n=20, s=10, e=25, w=15, res=1)
  27. cls.runModule(
  28. "v.random",
  29. flags="zb",
  30. output=cls.vector_points,
  31. npoints=cls.npoints,
  32. zmin=200,
  33. zmax=500,
  34. seed=100,
  35. )
  36. cls.runModule("v.out.lidar", input=cls.vector_points, output=cls.las_file)
  37. @classmethod
  38. def tearDownClass(cls):
  39. """Remove the temporary region and generated data"""
  40. cls.runModule("g.remove", flags="f", type="vector", name=cls.vector_points)
  41. if os.path.isfile(cls.las_file):
  42. os.remove(cls.las_file)
  43. cls.del_temp_region()
  44. def tearDown(self):
  45. """Remove the outputs created by the import
  46. This is executed after each test run.
  47. """
  48. self.runModule("g.remove", flags="f", type="vector", name=self.imported_points)
  49. def test_identical(self):
  50. """Test to see if the standard outputs are created"""
  51. self.assertModule(
  52. "v.in.lidar", input=self.las_file, output=self.imported_points, flags="bt"
  53. )
  54. self.assertVectorExists(self.imported_points)
  55. self.assertVectorFitsTopoInfo(
  56. vector=self.imported_points, reference=dict(points=self.npoints)
  57. )
  58. def skip_number(self, number, expect):
  59. """Test to see if the outputs are created"""
  60. self.assertModule(
  61. "v.in.lidar",
  62. input=self.las_file,
  63. output=self.imported_points,
  64. flags="bt",
  65. skip=number,
  66. )
  67. self.assertVectorExists(self.imported_points)
  68. self.assertVectorFitsTopoInfo(
  69. vector=self.imported_points, reference=dict(points=expect)
  70. )
  71. def preserve_number(self, number, expect):
  72. """Test to see if the outputs are created"""
  73. self.assertModule(
  74. "v.in.lidar",
  75. input=self.las_file,
  76. output=self.imported_points,
  77. flags="bt",
  78. preserve=number,
  79. )
  80. self.assertVectorExists(self.imported_points)
  81. self.assertVectorFitsTopoInfo(
  82. vector=self.imported_points, reference=dict(points=expect)
  83. )
  84. def offset_number(self, number, expect):
  85. """Test to see if the outputs are created"""
  86. self.assertModule(
  87. "v.in.lidar",
  88. input=self.las_file,
  89. output=self.imported_points,
  90. flags="bt",
  91. offset=number,
  92. )
  93. self.assertVectorExists(self.imported_points)
  94. self.assertVectorFitsTopoInfo(
  95. vector=self.imported_points, reference=dict(points=expect)
  96. )
  97. def limit_number(self, number, expect):
  98. """Test to see if the outputs are created"""
  99. self.assertModule(
  100. "v.in.lidar",
  101. input=self.las_file,
  102. output=self.imported_points,
  103. flags="bt",
  104. limit=number,
  105. )
  106. self.assertVectorExists(self.imported_points)
  107. self.assertVectorFitsTopoInfo(
  108. vector=self.imported_points, reference=dict(points=expect)
  109. )
  110. def test_decimated_skip_2(self):
  111. """Test to see if the outputs are created"""
  112. self.skip_number(number=2, expect=self.npoints / 2)
  113. def test_decimated_skip_4(self):
  114. """Test to see if the outputs are created"""
  115. self.skip_number(number=4, expect=0.75 * self.npoints)
  116. def test_decimated_skip_10(self):
  117. """Test to see if the outputs are created"""
  118. self.skip_number(number=10, expect=0.9 * self.npoints)
  119. def test_decimated_preserve_2(self):
  120. """Test to see if the outputs are created"""
  121. self.preserve_number(number=2, expect=self.npoints / 2)
  122. def test_decimated_preserve_10(self):
  123. """Test to see if the outputs are created"""
  124. self.preserve_number(number=10, expect=self.npoints / 10)
  125. def test_decimated_offset_105(self):
  126. """Test to see if the outputs are created"""
  127. self.offset_number(number=105, expect=self.npoints - 105)
  128. def test_decimated_limit_105(self):
  129. """Test to see if the outputs are created"""
  130. self.limit_number(number=105, expect=105)
  131. def test_offset_preserve(self):
  132. """Test to see if the outputs are created"""
  133. self.assertModule(
  134. "v.in.lidar",
  135. input=self.las_file,
  136. output=self.imported_points,
  137. flags="bt",
  138. offset=105,
  139. preserve=10,
  140. )
  141. self.assertVectorExists(self.imported_points)
  142. self.assertVectorFitsTopoInfo(
  143. vector=self.imported_points,
  144. reference=dict(points=int((self.npoints - 105) / 10)),
  145. )
  146. def test_limit_skip(self):
  147. """Test to see if the outputs are created"""
  148. self.assertModule(
  149. "v.in.lidar",
  150. input=self.las_file,
  151. output=self.imported_points,
  152. flags="bt",
  153. limit=105,
  154. skip=10,
  155. )
  156. self.assertVectorExists(self.imported_points)
  157. self.assertVectorFitsTopoInfo(
  158. vector=self.imported_points, reference=dict(points=105)
  159. )
  160. def test_offset_limit_skip(self):
  161. """Test to see if the outputs are created"""
  162. self.assertModule(
  163. "v.in.lidar",
  164. input=self.las_file,
  165. output=self.imported_points,
  166. flags="bt",
  167. offset=50,
  168. skip=5,
  169. limit=self.npoints - 1,
  170. )
  171. self.assertVectorExists(self.imported_points)
  172. self.assertVectorFitsTopoInfo(
  173. vector=self.imported_points,
  174. reference=dict(points=0.8 * (self.npoints - 50)),
  175. )
  176. if __name__ == "__main__":
  177. test()