source: grass/trunk/raster/r.slope.aspect/testsuite/test_r_slope_aspect.py@ 68557

Last change on this file since 68557 was 68557, checked in by wenzeslaus, 8 years ago

r.slope.aspect: test is guaranteed to work only for the NC sample data

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