test_r_slope_aspect.py 6.1 KB

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