From 26332a827ada28a3292287dcc45cbc1750231ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20de=20Sousa?= Date: Wed, 19 Jun 2024 12:49:02 +0200 Subject: [PATCH 1/2] r.out.gdal: adds basic unit tests --- .../r.out.gdal/testsuite/test_r_out_gdal.py | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 raster/r.out.gdal/testsuite/test_r_out_gdal.py diff --git a/raster/r.out.gdal/testsuite/test_r_out_gdal.py b/raster/r.out.gdal/testsuite/test_r_out_gdal.py new file mode 100644 index 00000000000..854149d21c9 --- /dev/null +++ b/raster/r.out.gdal/testsuite/test_r_out_gdal.py @@ -0,0 +1,109 @@ +"""Test of r.out.gdal +@author Luís Moreira de Sousa +""" + +from pathlib import Path +from grass.gunittest.case import TestCase + + +class TestOgrExport(TestCase): + + # Vector map in NC test dataset + test_map = "boundary_county_500m" + + # Result of import tests + temp_import = "test_gdal_import_map" + + # Output of r.univar + univar_string = """n=60020100 +null_cells=0 +cells=60020100 +min=37 +max=183 +range=146 +mean=120.311390684121 +mean_of_abs=120.311390684121 +stddev=52.7291612829331 +variance=2780.36444960157 +coeff_var=43.8272394518107 +sum=7221101700 +""" + + @classmethod + def setUpClass(cls): + """Use temporary region settings""" + cls.use_temp_region() + + @classmethod + def tearDownClass(cls): + """!Remove the temporary region""" + cls.del_temp_region() + + def tearDown(self): + self.runModule( + "g.remove", type="raster", flags="f", pattern=f"{self.temp_import}*" + ) + for p in Path(".").glob(f"{self.test_map}*"): + p.unlink() + + def test_gpkg_format(self): + """Tests output to GeoPackage format""" + + self.assertModule( + "r.out.gdal", + "Export to GeoPackage Format", + input=self.test_map, + output=f"{self.test_map}.gpkg", + format="GPKG", + type="Int16", + flags="f", + ) + + # Import back to verify + self.runModule( + "r.in.gdal", + input=f"{self.test_map}.gpkg", + output=self.temp_import, + ) + + self.runModule("g.region", raster=self.temp_import) + + self.assertRasterFitsUnivar( + raster=self.temp_import, + reference=self.univar_string, + precision=1e-8, + ) + + def test_gtiff_format(self): + """Tests output to GeoTiff format""" + + self.assertModule( + "r.out.gdal", + "Export to GeoTiff Format", + input=self.test_map, + output=f"{self.test_map}.gtiff", + format="GTiff", + type="Int16", + flags="fc", + ) + + # Import back to verify + self.runModule( + "r.in.gdal", + input=f"{self.test_map}.gtiff", + output=self.temp_import, + ) + + self.runModule("g.region", raster=self.temp_import) + + self.assertRasterFitsUnivar( + raster=self.temp_import, + reference=self.univar_string, + precision=1e-8, + ) + + +if __name__ == "__main__": + from grass.gunittest.main import test + + test() From 00eb27f9b72cf6c0a2388b8c9028ba05aa3a19f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20de=20Sousa?= Date: Mon, 29 Jul 2024 16:37:29 +0100 Subject: [PATCH 2/2] Update required by Ruff Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- raster/r.out.gdal/testsuite/test_r_out_gdal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raster/r.out.gdal/testsuite/test_r_out_gdal.py b/raster/r.out.gdal/testsuite/test_r_out_gdal.py index 854149d21c9..68312789033 100644 --- a/raster/r.out.gdal/testsuite/test_r_out_gdal.py +++ b/raster/r.out.gdal/testsuite/test_r_out_gdal.py @@ -43,7 +43,7 @@ def tearDown(self): self.runModule( "g.remove", type="raster", flags="f", pattern=f"{self.temp_import}*" ) - for p in Path(".").glob(f"{self.test_map}*"): + for p in Path().glob(f"{self.test_map}*"): p.unlink() def test_gpkg_format(self):