| 1 | import grass.script as gs |
| 2 | from grass.gunittest.case import TestCase |
| 3 | from grass.gunittest.main import test |
| 4 | |
| 5 | |
| 6 | class TestIOrthoInit(TestCase): |
| 7 | """Regression tests for the i.ortho.init module""" |
| 8 | |
| 9 | test_group = "test_ortho_init_group" |
| 10 | test_raster = "test_ortho_init_raster" |
| 11 | |
| 12 | test_xc = 500000.123 |
| 13 | test_yc = 4500000.456 |
| 14 | test_zc = 1500.789 |
| 15 | test_omega = 2.5 |
| 16 | test_phi = -1.8 |
| 17 | test_kappa = 45.2 |
| 18 | |
| 19 | test_xc_sd = 10.0 |
| 20 | test_yc_sd = 10.0 |
| 21 | test_zc_sd = 5.0 |
| 22 | test_omega_sd = 2.0 |
| 23 | test_phi_sd = 2.0 |
| 24 | test_kappa_sd = 2.0 |
| 25 | |
| 26 | @classmethod |
| 27 | def setUpClass(cls): |
| 28 | """Set up a temporary region and create test raster and group""" |
| 29 | cls.use_temp_region() |
| 30 | cls.runModule("g.region", n=10, s=0, e=10, w=0, rows=10, cols=10) |
| 31 | cls.runModule( |
| 32 | "r.mapcalc", expression=f"{cls.test_raster} = row()", overwrite=True |
| 33 | ) |
| 34 | cls.runModule("i.group", group=cls.test_group, input=cls.test_raster) |
| 35 | |
| 36 | @classmethod |
| 37 | def tearDownClass(cls): |
| 38 | """Remove all temporary data and region""" |
| 39 | gs.run_command("g.remove", flags="f", type="raster", name=cls.test_raster) |
| 40 | gs.run_command("g.remove", flags="f", type="group", name=cls.test_group) |
| 41 | cls.del_temp_region() |
| 42 | |
| 43 | def _run_init_module(self, flags="", **overrides): |
| 44 | """Helper to run i.ortho.init with default parameters and optional overrides""" |
| 45 | params = { |
| 46 | "group": self.test_group, |
| 47 | "xc": self.test_xc, |
| 48 | "yc": self.test_yc, |
| 49 | "zc": self.test_zc, |
| 50 | "xc_sd": self.test_xc_sd, |
| 51 | "yc_sd": self.test_yc_sd, |
| 52 | "zc_sd": self.test_zc_sd, |
| 53 | "omega": self.test_omega, |
| 54 | "phi": self.test_phi, |
| 55 | "kappa": self.test_kappa, |
| 56 | "omega_sd": self.test_omega_sd, |
| 57 | "phi_sd": self.test_phi_sd, |
| 58 | "kappa_sd": self.test_kappa_sd, |
| 59 | } |
| 60 | params.update(overrides) |
| 61 | self.assertModule("i.ortho.init", flags=flags, **params) |
| 62 | |
| 63 | def _assert_camera_params_equal(self, output_dict, expected_dict): |
| 64 | """Helper to assert all expected camera parameters with 3 decimal tolerance""" |
| 65 | for key, expected in expected_dict.items(): |
| 66 | actual = float(output_dict[key]) |
| 67 | self.assertAlmostEqual( |
| 68 | actual, |
| 69 | expected, |
| 70 | places=3, |
| 71 | msg=f"{key} mismatch: got {actual}, expected {expected}", |
| 72 | ) |
| 73 | |
| 74 | def test_create_camera_model_with_use_flag(self): |
| 75 | """Test full camera model creation with use flag (-r)""" |
| 76 | self._run_init_module() |
| 77 | output = gs.parse_command("i.ortho.init", group=self.test_group, flags="p") |
| 78 | |
| 79 | expected_values = { |
| 80 | "xc": self.test_xc, |
| 81 | "yc": self.test_yc, |
| 82 | "zc": self.test_zc, |
| 83 | "omega": self.test_omega, |
| 84 | "phi": self.test_phi, |
| 85 | "kappa": self.test_kappa, |
| 86 | "xc_sd": self.test_xc_sd, |
| 87 | "yc_sd": self.test_yc_sd, |
| 88 | "zc_sd": self.test_zc_sd, |
| 89 | "omega_sd": self.test_omega_sd, |
| 90 | "phi_sd": self.test_phi_sd, |
| 91 | "kappa_sd": self.test_kappa_sd, |
| 92 | } |
| 93 | |
| 94 | self._assert_camera_params_equal(output, expected_values) |
| 95 | |
| 96 | def test_partial_update_of_parameters(self): |
| 97 | """Test that selected camera parameters can be updated after initial creation""" |
| 98 | self._run_init_module() |
| 99 | |
| 100 | updated_values = {"omega": 5.7, "kappa": -30.5, "zc_sd": 15.0} |
| 101 | |
| 102 | self.assertModule("i.ortho.init", group=self.test_group, **updated_values) |
| 103 | output = gs.parse_command("i.ortho.init", group=self.test_group, flags="p") |
| 104 | |
| 105 | expected_values = { |
| 106 | "omega": updated_values["omega"], |
| 107 | "kappa": updated_values["kappa"], |
| 108 | "zc_sd": updated_values["zc_sd"], |
| 109 | "xc": self.test_xc, |
| 110 | "phi": self.test_phi, |
| 111 | } |
| 112 | |
| 113 | self._assert_camera_params_equal(output, expected_values) |
| 114 | |
| 115 | |
| 116 | if __name__ == "__main__": |
| 117 | test() |