Skip to content

Commit

Permalink
autotest test_gdal_pansharpen.py: make tests independent, use tmp_path
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jul 18, 2023
1 parent fb77cbb commit 48e208c
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions autotest/pyscripts/test_gdal_pansharpen.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,48 @@ def script_path():
return test_py_scripts.get_py_script("gdal_pansharpen")


@pytest.fixture(scope="module")
def small_world_pan_tif(tmp_path_factory):

small_world_pan_tif = str(tmp_path_factory.mktemp("tmp") / "small_world_pan.tif")

with gdal.Open(
test_py_scripts.get_data_path("gdrivers") + "small_world.tif"
) as src_ds:
src_data = src_ds.GetRasterBand(1).ReadRaster()
gt = src_ds.GetGeoTransform()
wkt = src_ds.GetProjectionRef()

with gdal.GetDriverByName("GTiff").Create(small_world_pan_tif, 800, 400) as pan_ds:
gt = [gt[i] for i in range(len(gt))]
gt[1] *= 0.5
gt[5] *= 0.5
pan_ds.SetGeoTransform(gt)
pan_ds.SetProjection(wkt)
pan_ds.GetRasterBand(1).WriteRaster(0, 0, 800, 400, src_data, 400, 200)

return small_world_pan_tif


###############################################################################
# Simple test


def test_gdal_pansharpen_1(script_path):
def test_gdal_pansharpen_1(script_path, tmp_path, small_world_pan_tif):

src_ds = gdal.Open(test_py_scripts.get_data_path("gdrivers") + "small_world.tif")
src_data = src_ds.GetRasterBand(1).ReadRaster()
gt = src_ds.GetGeoTransform()
wkt = src_ds.GetProjectionRef()
src_ds = None
pan_ds = gdal.GetDriverByName("GTiff").Create("tmp/small_world_pan.tif", 800, 400)
gt = [gt[i] for i in range(len(gt))]
gt[1] *= 0.5
gt[5] *= 0.5
pan_ds.SetGeoTransform(gt)
pan_ds.SetProjection(wkt)
pan_ds.GetRasterBand(1).WriteRaster(0, 0, 800, 400, src_data, 400, 200)
pan_ds = None
out_tif = str(tmp_path / "out.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_pansharpen",
" tmp/small_world_pan.tif "
f" {small_world_pan_tif} "
+ test_py_scripts.get_data_path("gdrivers")
+ "small_world.tif tmp/out.tif",
+ "small_world.tif "
+ out_tif,
)

ds = gdal.Open("tmp/out.tif")
cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)]
ds = None
gdal.GetDriverByName("GTiff").Delete("tmp/out.tif")
with gdal.Open(out_tif) as ds:
cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)]

assert cs in ([4735, 10000, 9742], [4731, 9991, 9734]) # s390x or graviton2

Expand All @@ -86,32 +96,26 @@ def test_gdal_pansharpen_1(script_path):
# Full options


def test_gdal_pansharpen_2(script_path):
def test_gdal_pansharpen_2(script_path, tmp_path, small_world_pan_tif):

out_vrt = str(tmp_path / "out.vrt")

test_py_scripts.run_py_script(
script_path,
"gdal_pansharpen",
" -q -b 3 -b 1 -bitdepth 8 -threads ALL_CPUS -spat_adjust union -w 0.33333333333333333 -w 0.33333333333333333 -w 0.33333333333333333 -of VRT -r cubic tmp/small_world_pan.tif "
" -q -b 3 -b 1 -bitdepth 8 -threads ALL_CPUS -spat_adjust union -w 0.33333333333333333 -w 0.33333333333333333 -w 0.33333333333333333 -of VRT -r cubic "
+ small_world_pan_tif
+ " "
+ test_py_scripts.get_data_path("gdrivers")
+ "small_world.tif,band=1 "
+ test_py_scripts.get_data_path("gdrivers")
+ "small_world.tif,band=2 "
+ test_py_scripts.get_data_path("gdrivers")
+ "small_world.tif,band=3 tmp/out.vrt",
+ "small_world.tif,band=3 "
+ out_vrt,
)

ds = gdal.Open("tmp/out.vrt")
cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)]
ds = None
gdal.GetDriverByName("VRT").Delete("tmp/out.vrt")
with gdal.Open(out_vrt) as ds:
cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)]

assert cs in ([9742, 4735], [9734, 4731]) # s390x or graviton2


###############################################################################
# Cleanup


def test_gdal_pansharpen_cleanup():

gdal.GetDriverByName("GTiff").Delete("tmp/small_world_pan.tif")

0 comments on commit 48e208c

Please sign in to comment.