diff --git a/tests/gis_tests/gdal_tests/test_raster.py b/tests/gis_tests/gdal_tests/test_raster.py index 040cd84e3a..5c9df59e11 100644 --- a/tests/gis_tests/gdal_tests/test_raster.py +++ b/tests/gis_tests/gdal_tests/test_raster.py @@ -1,4 +1,5 @@ import os +import shutil import struct import tempfile @@ -541,54 +542,54 @@ class GDALRasterTests(SimpleTestCase): class GDALBandTests(SimpleTestCase): - def setUp(self): - self.rs_path = os.path.join(os.path.dirname(__file__), '../data/rasters/raster.tif') - rs = GDALRaster(self.rs_path) - self.band = rs.bands[0] + rs_path = os.path.join(os.path.dirname(__file__), '../data/rasters/raster.tif') def test_band_data(self): - pam_file = self.rs_path + '.aux.xml' - self.assertEqual(self.band.width, 163) - self.assertEqual(self.band.height, 174) - self.assertEqual(self.band.description, '') - self.assertEqual(self.band.datatype(), 1) - self.assertEqual(self.band.datatype(as_string=True), 'GDT_Byte') - self.assertEqual(self.band.color_interp(), 1) - self.assertEqual(self.band.color_interp(as_string=True), 'GCI_GrayIndex') - self.assertEqual(self.band.nodata_value, 15) + rs = GDALRaster(self.rs_path) + band = rs.bands[0] + self.assertEqual(band.width, 163) + self.assertEqual(band.height, 174) + self.assertEqual(band.description, '') + self.assertEqual(band.datatype(), 1) + self.assertEqual(band.datatype(as_string=True), 'GDT_Byte') + self.assertEqual(band.color_interp(), 1) + self.assertEqual(band.color_interp(as_string=True), 'GCI_GrayIndex') + self.assertEqual(band.nodata_value, 15) if numpy: - data = self.band.data() + data = band.data() assert_array = numpy.loadtxt( os.path.join(os.path.dirname(__file__), '../data/rasters/raster.numpy.txt') ) numpy.testing.assert_equal(data, assert_array) - self.assertEqual(data.shape, (self.band.height, self.band.width)) - try: - smin, smax, smean, sstd = self.band.statistics(approximate=True) + self.assertEqual(data.shape, (band.height, band.width)) + + def test_band_statistics(self): + with tempfile.TemporaryDirectory() as tmp_dir: + rs_path = os.path.join(tmp_dir, 'raster.tif') + shutil.copyfile(self.rs_path, rs_path) + rs = GDALRaster(rs_path) + band = rs.bands[0] + pam_file = rs_path + '.aux.xml' + smin, smax, smean, sstd = band.statistics(approximate=True) self.assertEqual(smin, 0) self.assertEqual(smax, 9) self.assertAlmostEqual(smean, 2.842331288343558) self.assertAlmostEqual(sstd, 2.3965567248965356) - smin, smax, smean, sstd = self.band.statistics(approximate=False, refresh=True) + smin, smax, smean, sstd = band.statistics(approximate=False, refresh=True) self.assertEqual(smin, 0) self.assertEqual(smax, 9) self.assertAlmostEqual(smean, 2.828326634228898) self.assertAlmostEqual(sstd, 2.4260526986669095) - self.assertEqual(self.band.min, 0) - self.assertEqual(self.band.max, 9) - self.assertAlmostEqual(self.band.mean, 2.828326634228898) - self.assertAlmostEqual(self.band.std, 2.4260526986669095) + self.assertEqual(band.min, 0) + self.assertEqual(band.max, 9) + self.assertAlmostEqual(band.mean, 2.828326634228898) + self.assertAlmostEqual(band.std, 2.4260526986669095) # Statistics are persisted into PAM file on band close - self.band = None + rs = band = None self.assertTrue(os.path.isfile(pam_file)) - finally: - # Close band and remove file if created - self.band = None - if os.path.isfile(pam_file): - os.remove(pam_file) def test_read_mode_error(self): # Open raster in read mode