test_r_slope_aspect.py 5.9 KB

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