test_r_slope_aspect.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. from grass.gunittest.case import TestCase
  2. from grass.gunittest.main import test
  3. from grass.gunittest.gmodules import call_module
  4. SMALL_MAP = """\
  5. north: 15
  6. south: 10
  7. east: 25
  8. west: 20
  9. rows: 5
  10. cols: 5
  11. 100.0 150.0 150.0 100.0 100.0
  12. 100.0 150.0 150.0 100.0 100.0
  13. 100.0 150.0 150.0 150.0 150.0
  14. 100.0 150.0 150.0 100.0 100.0
  15. 100.0 150.0 150.0 100.0 100.0
  16. """
  17. class TestSlopeAspect(TestCase):
  18. slope = 'limits_slope'
  19. aspect = 'limits_aspect'
  20. def setUp(self):
  21. self.use_temp_region()
  22. call_module('g.region', raster='elevation')
  23. def tearDown(self):
  24. self.del_temp_region()
  25. call_module('g.remove', flags='f', type_='raster',
  26. name=[self.slope, self.aspect])
  27. def test_limits(self):
  28. self.assertModule('r.slope.aspect', elevation='elevation',
  29. slope=self.slope, aspect=self.aspect)
  30. self.assertRasterMinMax(map=self.slope, refmin=0, refmax=90,
  31. msg="Slope in degrees must be between 0 and 90")
  32. self.assertRasterMinMax(map=self.aspect, refmin=0, refmax=360,
  33. msg="Aspect in degrees must be between 0 and 360")
  34. def test_limits_precent(self):
  35. """Assumes NC elevation and allows slope up to 100% (45deg)"""
  36. self.assertModule('r.slope.aspect', elevation='elevation',
  37. slope=self.slope, aspect=self.aspect, format='percent')
  38. self.assertRasterMinMax(map=self.slope, refmin=0, refmax=100,
  39. msg="Slope in percent must be between 0 and 100")
  40. self.assertRasterMinMax(map=self.aspect, refmin=0, refmax=360,
  41. msg="Aspect in degrees must be between 0 and 360")
  42. class TestSlopeAspectAgainstReference(TestCase):
  43. """
  44. Data created using::
  45. g.region n=20 s=10 e=25 w=15 res=1
  46. r.surf.fractal output=fractal_surf
  47. r.out.ascii input=fractal_surf output=data/fractal_surf.ascii
  48. gdaldem slope .../fractal_surf.ascii .../gdal_slope.grd -of GSAG
  49. gdaldem aspect .../fractal_surf.ascii .../gdal_aspect.grd -of GSAG -trigonometric
  50. GDAL version 1.11.0 was used. Note: GDAL-slope/aspect implementation is originally based on
  51. GRASS GIS 4.1.
  52. """
  53. # precision for comparisons
  54. precision = 0.0001
  55. ref_aspect = 'reference_aspect'
  56. aspect = 'fractal_aspect'
  57. ref_slope = 'reference_slope'
  58. slope = 'fractal_slope'
  59. @classmethod
  60. def setUpClass(cls):
  61. cls.use_temp_region()
  62. call_module('g.region', n=20, s=10, e=25, w=15, res=1)
  63. cls.elevation = 'fractal_surf'
  64. cls.runModule('r.in.ascii', input='data/fractal_surf.ascii',
  65. output=cls.elevation)
  66. @classmethod
  67. def tearDownClass(cls):
  68. cls.del_temp_region()
  69. cls.runModule('g.remove', flags='f', type='raster',
  70. name=[cls.elevation, cls.slope, cls.aspect, cls.ref_aspect, cls.ref_slope])
  71. def test_slope(self):
  72. # TODO: using gdal instead of ascii because of cannot seek error
  73. self.runModule('r.in.gdal', flags='o',
  74. input='data/gdal_slope.grd', output=self.ref_slope)
  75. self.assertModule('r.slope.aspect', elevation=self.elevation,
  76. slope=self.slope)
  77. # check we have expected values
  78. self.assertRasterMinMax(map=self.slope, refmin=0, refmax=90,
  79. msg="Slope in degrees must be between 0 and 90")
  80. # check against reference data
  81. self.assertRastersNoDifference(actual=self.slope, reference=self.ref_slope,
  82. precision=self.precision)
  83. def test_aspect(self):
  84. # TODO: using gdal instead of ascii because of cannot seek error
  85. self.runModule('r.in.gdal', flags='o',
  86. input='data/gdal_aspect.grd', output=self.ref_aspect)
  87. self.assertModule('r.slope.aspect', elevation=self.elevation,
  88. aspect=self.aspect)
  89. # check we have expected values
  90. self.assertRasterMinMax(map=self.aspect, refmin=0, refmax=360,
  91. msg="Aspect in degrees must be between 0 and 360")
  92. # check against reference data
  93. self.assertRastersNoDifference(actual=self.aspect, reference=self.ref_aspect,
  94. precision=self.precision)
  95. class TestSlopeAspectAgainstItself(TestCase):
  96. precision = 0.0000001
  97. elevation = 'elevation'
  98. t_aspect = 'sa_together_aspect'
  99. t_slope = 'sa_together_slope'
  100. s_aspect = 'sa_separately_aspect'
  101. s_slope = 'sa_separately_slope'
  102. @classmethod
  103. def setUpClass(cls):
  104. cls.use_temp_region()
  105. call_module('g.region', raster='elevation')
  106. @classmethod
  107. def tearDownClass(cls):
  108. cls.del_temp_region()
  109. call_module('g.remove', flags='f', type_='raster',
  110. name=[cls.t_aspect, cls.t_slope, cls.s_slope, cls.s_aspect])
  111. def test_slope_aspect_together(self):
  112. """Slope and aspect computed separately and together should be the same
  113. """
  114. self.assertModule('r.slope.aspect', elevation=self.elevation,
  115. aspect=self.s_aspect)
  116. self.assertModule('r.slope.aspect', elevation=self.elevation,
  117. slope=self.s_slope)
  118. self.assertModule('r.slope.aspect', elevation=self.elevation,
  119. slope=self.t_slope, aspect=self.t_aspect)
  120. self.assertRastersNoDifference(actual=self.t_aspect, reference=self.s_aspect,
  121. precision=self.precision)
  122. self.assertRastersNoDifference(actual=self.t_slope, reference=self.s_slope,
  123. precision=self.precision)
  124. # TODO: implement this class
  125. class TestExtremes(TestCase):
  126. slope = 'small_slope'
  127. aspect = 'small_aspect'
  128. elevation = 'small_elevation'
  129. def setUp(self):
  130. self.use_temp_region()
  131. def tearDown(self):
  132. self.del_temp_region()
  133. call_module('g.remove', flags='f', type_='raster',
  134. name=[self.slope, self.aspect, self.elevation])
  135. def test_small(self):
  136. self.runModule('r.in.ascii', input='-', output=self.elevation,
  137. stdin_=SMALL_MAP)
  138. call_module('g.region', raster=self.elevation)
  139. self.assertModule('r.slope.aspect', elevation=self.elevation,
  140. slope=self.slope, aspect=self.aspect)
  141. self.assertRasterMinMax(map=self.slope, refmin=0, refmax=90,
  142. msg="Slope in degrees must be between 0 and 90")
  143. self.assertRasterMinMax(map=self.aspect, refmin=0, refmax=360,
  144. msg="Aspect in degrees must be between 0 and 360")
  145. if __name__ == '__main__':
  146. test()