| 1 | import grass.script as gs |
| 2 | from grass.gunittest.case import TestCase |
| 3 | from grass.gunittest.main import test |
| 4 | |
| 5 | |
| 6 | class TestIOrthoElev(TestCase): |
| 7 | """Regression testsuite for i.ortho.elev module""" |
| 8 | |
| 9 | test_group = "test_ortho_elev_group" |
| 10 | tmp_raster = "temp_raster" |
| 11 | elev_map = "elevation_map" |
| 12 | scaled_elev_map = "scaled_elevation_map" |
| 13 | tmp_rasters = [] |
| 14 | |
| 15 | @classmethod |
| 16 | def setUpClass(cls): |
| 17 | """Set up input region and rasters for test environment.""" |
| 18 | cls.use_temp_region() |
| 19 | cls.runModule("g.region", n=10, s=0, e=10, w=0, rows=10, cols=10) |
| 20 | cls.runModule( |
| 21 | "r.mapcalc", expression=f"{cls.tmp_raster} = row()", overwrite=True |
| 22 | ) |
| 23 | cls.runModule( |
| 24 | "r.mapcalc", expression=f"{cls.elev_map} = 100 + row()", overwrite=True |
| 25 | ) |
| 26 | cls.runModule( |
| 27 | "r.mapcalc", |
| 28 | expression=f"{cls.scaled_elev_map} = int(row()/5)", |
| 29 | overwrite=True, |
| 30 | ) |
| 31 | cls.tmp_rasters.extend([cls.tmp_raster, cls.elev_map, cls.scaled_elev_map]) |
| 32 | |
| 33 | cls.runModule( |
| 34 | "i.group", |
| 35 | group=cls.test_group, |
| 36 | input=f"{cls.tmp_raster},{cls.elev_map},{cls.scaled_elev_map}", |
| 37 | ) |
| 38 | |
| 39 | def setUp(self): |
| 40 | """Set the target location and mapset for the test imagery group.""" |
| 41 | self.runModule( |
| 42 | "i.target", |
| 43 | group=self.test_group, |
| 44 | location=gs.gisenv()["LOCATION_NAME"], |
| 45 | mapset=gs.gisenv()["MAPSET"], |
| 46 | ) |
| 47 | |
| 48 | @classmethod |
| 49 | def tearDownClass(cls): |
| 50 | """Remove temporary raster maps and imagery group, and reset region.""" |
| 51 | gs.run_command("g.remove", flags="f", type="raster", name=cls.tmp_rasters) |
| 52 | gs.run_command("g.remove", flags="f", type="group", name=cls.test_group) |
| 53 | cls.del_temp_region() |
| 54 | |
| 55 | def test_set_basic_elevation(self): |
| 56 | """Assigns an elevation map to the ortho group and verifies metadata output""" |
| 57 | self.assertModule( |
| 58 | "i.ortho.elev", |
| 59 | group=self.test_group, |
| 60 | elevation=self.elev_map, |
| 61 | ) |
| 62 | output = gs.read_command("i.ortho.elev", group=self.test_group, flags="p") |
| 63 | self.assertIn(f"map:\t\t\t{self.elev_map}", output) |
| 64 | |
| 65 | def test_all_metadata(self): |
| 66 | """Assigns an elevation map with math expression, unit, and null value and checks all metadata fields""" |
| 67 | self.assertModule( |
| 68 | "i.ortho.elev", |
| 69 | group=self.test_group, |
| 70 | elevation=self.scaled_elev_map, |
| 71 | math_expression="x*10+50", |
| 72 | units="meters", |
| 73 | null_value="-1", |
| 74 | ) |
| 75 | |
| 76 | output = gs.read_command("i.ortho.elev", group=self.test_group, flags="p") |
| 77 | |
| 78 | self.assertIn(f"map:\t\t\t{self.scaled_elev_map}", output) |
| 79 | self.assertIn("math expression:\tx*10+50", output) |
| 80 | self.assertIn("units:\t\t\tmeters", output) |
| 81 | self.assertIn("nodata value:\t\t-1", output) |
| 82 | |
| 83 | def test_parameter_persistence(self): |
| 84 | """Ensure previously set parameters persist after updating the elevation map""" |
| 85 | self.runModule( |
| 86 | "i.ortho.elev", |
| 87 | group=self.test_group, |
| 88 | elevation=self.elev_map, |
| 89 | units="meters", |
| 90 | null_value="-1", |
| 91 | ) |
| 92 | initial_output = gs.read_command( |
| 93 | "i.ortho.elev", group=self.test_group, flags="p" |
| 94 | ) |
| 95 | self.assertIn(f"map:\t\t\t{self.elev_map}", initial_output) |
| 96 | self.assertIn("units:\t\t\tmeters", initial_output) |
| 97 | |
| 98 | self.runModule( |
| 99 | "i.ortho.elev", |
| 100 | group=self.test_group, |
| 101 | elevation=self.scaled_elev_map, |
| 102 | units="meters", |
| 103 | null_value="-1", |
| 104 | ) |
| 105 | updated_output = gs.read_command( |
| 106 | "i.ortho.elev", group=self.test_group, flags="p" |
| 107 | ) |
| 108 | self.assertIn(f"map:\t\t\t{self.scaled_elev_map}", updated_output) |
| 109 | self.assertIn("units:\t\t\tmeters", updated_output) |
| 110 | |
| 111 | |
| 112 | if __name__ == "__main__": |
| 113 | test() |