Debian Codegraph @

raster/r.horizon/testsuite/test_r_horizon.py

raw
1 """
2 TEST: test_r_horizon.py
3
4 AUTHOR(S): Anna Petrasova <kratochanna at gmail>
5
6 PURPOSE: Test r.horizon
7
8 COPYRIGHT: (C) 2015-2024 Anna Petrasova
9
10 This program is free software under the GNU General Public
11 License (>=v2). Read the file COPYING that comes with GRASS
12 for details.
13 """
14
15 import json
16
17 from grass.gunittest.case import TestCase
18 from grass.gunittest.main import test
19 from grass.gunittest.gmodules import SimpleModule
20 from grass.script import raster_what
21
22
23 ref1 = """azimuth,horizon_height
24 180.000000,0.023101
25 """
26
27 ref2 = """azimuth,horizon_height
28 180.000000,0.023101
29 200.000000,0.034850
30 220.000000,0.050549
31 240.000000,0.048211
32 260.000000,0.053101
33 280.000000,0.039774
34 300.000000,0.032360
35 320.000000,0.014804
36 340.000000,0.000000
37 360.000000,0.004724
38 20.000000,0.012612
39 40.000000,0.015207
40 60.000000,0.014344
41 80.000000,0.011044
42 100.000000,0.012192
43 120.000000,0.007462
44 140.000000,0.004071
45 160.000000,0.015356
46 """
47
48 ref3 = """azimuth,horizon_height
49 180.000000,0.023101
50 200.000000,0.034850
51 220.000000,0.050549
52 240.000000,0.048211
53 260.000000,0.053101
54 280.000000,0.039774
55 300.000000,0.032360
56 320.000000,0.014804
57 340.000000,0.000000
58 360.000000,0.004724
59 20.000000,0.012612
60 40.000000,0.015207
61 60.000000,0.014344
62 80.000000,0.011044
63 100.000000,0.012192
64 120.000000,0.007462
65 140.000000,0.004071
66 160.000000,0.015356
67 """
68
69 ref4 = """azimuth,horizon_height
70 0.000000,0.197017
71 20.000000,0.196832
72 40.000000,0.196875
73 60.000000,0.196689
74 80.000000,0.196847
75 100.000000,0.196645
76 120.000000,0.196969
77 140.000000,0.196778
78 160.000000,0.196863
79 180.000000,0.197017
80 200.000000,0.196832
81 220.000000,0.196875
82 240.000000,0.196689
83 260.000000,0.196847
84 280.000000,0.196645
85 300.000000,0.196969
86 320.000000,0.196778
87 340.000000,0.196863
88 """
89
90 ref5 = """azimuth,horizon_height,horizon_distance
91 0.000000,0.197017,5000.040000
92 20.000000,0.196832,5004.837660
93 40.000000,0.196875,5003.728610
94 60.000000,0.196689,5008.552685
95 80.000000,0.196847,5004.448022
96 100.000000,0.196645,5009.690609
97 120.000000,0.196969,5001.279836
98 140.000000,0.196778,5006.246099
99 160.000000,0.196863,5004.018385
100 180.000000,0.197017,5000.040000
101 200.000000,0.196832,5004.837660
102 220.000000,0.196875,5003.728610
103 240.000000,0.196689,5008.552685
104 260.000000,0.196847,5004.448022
105 280.000000,0.196645,5009.690609
106 300.000000,0.196969,5001.279836
107 320.000000,0.196778,5006.246099
108 340.000000,0.196863,5004.018385
109 """
110
111 ref6 = """azimuth,horizon_height,horizon_distance
112 180.000000,0.023101,420.000000
113 200.000000,0.034850,436.577599
114 220.000000,0.050549,184.390889
115 240.000000,0.048211,197.230829
116 260.000000,0.053101,162.788206
117 280.000000,0.039774,253.179778
118 300.000000,0.032360,277.848880
119 320.000000,0.014804,262.488095
120 340.000000,0.000000,0.000000
121 360.000000,0.004724,2780.017986
122 20.000000,0.012612,1148.259553
123 40.000000,0.015207,1334.166406
124 60.000000,0.014344,1867.966809
125 80.000000,0.011044,2964.203097
126 100.000000,0.012192,1828.223181
127 120.000000,0.007462,4270.667395
128 140.000000,0.004071,5659.231397
129 160.000000,0.015356,1666.883319
130 """
131
132
133 class TestHorizon(TestCase):
134 circle = "circle"
135 horizon = "test_horizon_from_elevation"
136 horizon_output = "test_horizon_output_from_elevation"
137
138 @classmethod
139 def setUpClass(cls):
140 cls.use_temp_region()
141 cls.runModule("g.region", raster="elevation")
142 cls.runModule(
143 "r.circle",
144 flags="b",
145 output=cls.circle,
146 coordinates=(637505, 221755),
147 min=5000,
148 multiplier=1000,
149 )
150 cls.runModule("r.null", map=cls.circle, null=0)
151
152 @classmethod
153 def tearDownClass(cls):
154 cls.runModule("g.remove", flags="f", type="raster", name=cls.circle)
155 cls.del_temp_region()
156
157 def setUp(self):
158 self.runModule("g.region", raster="elevation")
159
160 def tearDown(self):
161 """Remove horizon map after each test method"""
162 self.runModule("g.remove", flags="f", type="raster", name=self.horizon)
163 self.runModule(
164 "g.remove", flags="f", type="raster", pattern=self.horizon_output + "*"
165 )
166
167 def test_point_mode_one_direction(self):
168 """Test mode with 1 point and 1 direction"""
169 module = SimpleModule(
170 "r.horizon",
171 elevation="elevation",
172 coordinates=(634720, 216180),
173 output=self.horizon,
174 direction=180,
175 step=0,
176 )
177 self.assertModule(module)
178 stdout = module.outputs.stdout
179 self.assertMultiLineEqual(first=ref1, second=stdout)
180
181 def test_point_mode_multiple_direction(self):
182 """Test mode with 1 point and multiple directions"""
183 module = SimpleModule(
184 "r.horizon",
185 elevation="elevation",
186 coordinates=(634720, 216180),
187 output=self.horizon,
188 direction=180,
189 step=20,
190 )
191 self.assertModule(module)
192 stdout = module.outputs.stdout
193 self.assertMultiLineEqual(first=ref2, second=stdout)
194
195 # include nulls along the edge
196 self.runModule("g.region", raster="elevation", w="w-100")
197 self.assertModule(module)
198 stdout = module.outputs.stdout
199 self.assertMultiLineEqual(first=ref2, second=stdout)
200
201 def test_point_mode_multiple_points_and_directions(self):
202 """Test mode with 2 identical points and multiple directions"""
203 module = SimpleModule(
204 "r.horizon",
205 elevation="elevation",
206 coordinates=(634720, 216180, 634720, 216180),
207 output=self.horizon,
208 direction=180,
209 step=20,
210 )
211 self.assertModule(module)
212 stdout = module.outputs.stdout
213 self.assertMultiLineEqual(first=ref2 + ref2, second=stdout)
214
215 def test_point_mode_multiple_direction_json(self):
216 """Test mode with 1 point and multiple directions with JSON"""
217 module = SimpleModule(
218 "r.horizon",
219 elevation="elevation",
220 coordinates=(634720, 216180),
221 output=self.horizon,
222 direction=180,
223 step=20,
224 format="json",
225 )
226 self.assertModule(module)
227 stdout = json.loads(module.outputs.stdout)
228 horizons = []
229 reference = {}
230 for line in ref6.splitlines()[1:]:
231 azimuth, horizon, distance = line.split(",")
232 horizons.append(
233 {
234 "azimuth": float(azimuth),
235 "angle": float(horizon),
236 "distance": float(distance),
237 }
238 )
239 reference["x"] = 634720.0
240 reference["y"] = 216180.0
241 reference["horizons"] = horizons
242
243 self.assertListEqual([reference], stdout)
244
245 def test_point_mode_multiple_points_and_directions_json(self):
246 """Test mode with 2 identical points and multiple directions with JSON"""
247 module = SimpleModule(
248 "r.horizon",
249 elevation="elevation",
250 coordinates=(634720, 216180, 634720, 216180),
251 output=self.horizon,
252 direction=180,
253 step=20,
254 format="json",
255 )
256 self.assertModule(module)
257 stdout = json.loads(module.outputs.stdout)
258 horizons = []
259 reference = {}
260 for line in ref6.splitlines()[1:]:
261 azimuth, horizon, distance = line.split(",")
262 horizons.append(
263 {
264 "azimuth": float(azimuth),
265 "angle": float(horizon),
266 "distance": float(distance),
267 }
268 )
269 reference["x"] = 634720.0
270 reference["y"] = 216180.0
271 reference["horizons"] = horizons
272
273 self.assertListEqual([reference, reference], stdout)
274
275 def test_point_mode_multiple_direction_artificial(self):
276 """Test mode with 1 point and multiple directions with artificial surface"""
277 module = SimpleModule(
278 "r.horizon",
279 elevation=self.circle,
280 coordinates=(637505, 221755),
281 output=self.horizon,
282 direction=0,
283 step=20,
284 )
285 self.assertModule(module)
286 stdout = module.outputs.stdout
287 self.assertMultiLineEqual(first=ref4, second=stdout)
288
289 def test_point_mode_multiple_direction_artificial_distance(self):
290 """With 1 point, more directions on artificial surface, distance in output"""
291 module = SimpleModule(
292 "r.horizon",
293 elevation=self.circle,
294 coordinates=(637505, 221755),
295 output=self.horizon,
296 direction=0,
297 step=20,
298 flags="l",
299 )
300 self.assertModule(module)
301 stdout = module.outputs.stdout
302 self.assertMultiLineEqual(first=ref5, second=stdout)
303
304 module = SimpleModule(
305 "r.horizon",
306 elevation=self.circle,
307 coordinates=(637505, 221755),
308 output=self.horizon,
309 direction=0,
310 step=20,
311 flags="l",
312 format="json",
313 )
314 self.assertModule(module)
315 stdout = json.loads(module.outputs.stdout)
316 horizons = []
317 reference = {}
318 for line in ref5.splitlines()[1:]:
319 azimuth, horizon, distance = line.split(",")
320 horizons.append(
321 {
322 "azimuth": float(azimuth),
323 "angle": float(horizon),
324 "distance": float(distance),
325 }
326 )
327 reference["x"] = 637505.0
328 reference["y"] = 221755.0
329 reference["horizons"] = horizons
330
331 self.assertListEqual([reference], stdout)
332
333 def test_raster_mode_one_direction(self):
334 """Test mode with one direction and against point mode"""
335 module = SimpleModule(
336 "r.horizon", elevation="elevation", output=self.horizon_output, direction=50
337 )
338 self.assertModule(module)
339 ref = {
340 "min": 0,
341 "max": 0.70678365230560,
342 "stddev": 0.0360724286360789,
343 }
344 output = "test_horizon_output_from_elevation_050"
345 self.assertRasterFitsUnivar(
346 raster=output,
347 reference=ref,
348 precision=1e6,
349 )
350
351 # test if point mode matches raster mode
352 coordinates = [
353 (634725, 216185),
354 (633315, 217595),
355 (633555, 223405),
356 (639955, 220605),
357 (637505, 219705),
358 (641105, 222225),
359 ]
360 for coordinate in coordinates:
361 module = SimpleModule(
362 "r.horizon",
363 elevation="elevation",
364 coordinates=coordinate,
365 output=self.horizon,
366 direction=50,
367 step=0,
368 )
369 self.assertModule(module)
370 stdout = module.outputs.stdout
371 first = float(stdout.splitlines()[-1].split(",")[-1])
372 what = raster_what(output, coord=coordinate)
373 second = float(what[0][output]["value"])
374 self.assertAlmostEqual(first=first, second=second, delta=0.000001)
375
376 def test_raster_mode_multiple_direction(self):
377 self.runModule("g.region", raster="elevation", res=100)
378 module = SimpleModule(
379 "r.horizon",
380 elevation="elevation",
381 output=self.horizon_output,
382 start=10,
383 end=50,
384 step=15.512,
385 )
386 self.assertModule(module)
387 module_list = SimpleModule(
388 "g.list", type="raster", pattern=self.horizon_output + "*"
389 )
390 self.runModule(module_list)
391 stdout = module_list.outputs.stdout.strip()
392 self.assertMultiLineEqual(
393 first=(
394 "test_horizon_output_from_elevation_010_000\n"
395 "test_horizon_output_from_elevation_025_512\n"
396 "test_horizon_output_from_elevation_041_024"
397 ),
398 second=stdout,
399 )
400
401 def test_raster_mode_multiple_direction_offset(self):
402 self.runModule("g.region", raster="elevation", res=100)
403 module = SimpleModule(
404 "r.horizon",
405 elevation="elevation",
406 output=self.horizon_output,
407 start=10,
408 end=50,
409 step=15.512,
410 direction=80,
411 )
412 self.assertModule(module)
413 module_list = SimpleModule(
414 "g.list", type="raster", pattern=self.horizon_output + "*"
415 )
416 self.runModule(module_list)
417 stdout = module_list.outputs.stdout.strip()
418 self.assertMultiLineEqual(
419 first=(
420 "test_horizon_output_from_elevation_090_000\n"
421 "test_horizon_output_from_elevation_105_512\n"
422 "test_horizon_output_from_elevation_121_024"
423 ),
424 second=stdout,
425 )
426
427 def test_raster_mode_bufferzone(self):
428 """Test buffer 100 m and 109 m with resolution 10 gives the same result"""
429 self.runModule(
430 "g.region",
431 raster="elevation",
432 n="n-5000",
433 s="s+5000",
434 e="e-5000",
435 w="w+5000",
436 )
437 # raises ValueError from pygrass parameter check
438 self.assertRaises(
439 ValueError,
440 SimpleModule,
441 "r.horizon",
442 elevation="elevation",
443 output=self.horizon_output,
444 direction=50,
445 bufferzone=-100,
446 )
447 self.assertRaises(
448 ValueError,
449 SimpleModule,
450 "r.horizon",
451 elevation="elevation",
452 output=self.horizon_output,
453 direction=50,
454 e_buff=100,
455 n_buff=0,
456 s_buff=-100,
457 w_buff=-100,
458 )
459 module = SimpleModule(
460 "r.horizon",
461 elevation="elevation",
462 output=self.horizon_output,
463 direction=50,
464 bufferzone=100,
465 )
466 self.assertModule(module)
467 ref = {
468 "mean": 0.0344791,
469 }
470 output = "test_horizon_output_from_elevation_050"
471 self.assertRasterFitsUnivar(
472 raster=output,
473 reference=ref,
474 precision=1e-6,
475 )
476 module = SimpleModule(
477 "r.horizon",
478 elevation="elevation",
479 output=self.horizon_output,
480 direction=50,
481 bufferzone=103,
482 )
483 self.assertModule(module)
484 self.assertRasterFitsUnivar(
485 raster=output,
486 reference=ref,
487 precision=1e-6,
488 )
489 module = SimpleModule(
490 "r.horizon",
491 elevation="elevation",
492 output=self.horizon_output,
493 direction=50,
494 bufferzone=95,
495 )
496 self.assertModule(module)
497 ref = {
498 "mean": 0.0344624,
499 }
500 self.assertRasterFitsUnivar(
501 raster=output,
502 reference=ref,
503 precision=1e-6,
504 )
505 self.runModule("g.region", raster="elevation")
506
507
508 if __name__ == "__main__":
509 test()