Skip to content

Commit

Permalink
Merge pull request #8097 from dbaston/pytest-independent
Browse files Browse the repository at this point in the history
autotest: make utilities, pyscript tests run independently
  • Loading branch information
rouault authored Jul 20, 2023
2 parents 33693f2 + 2afea0a commit bacb9fd
Show file tree
Hide file tree
Showing 21 changed files with 1,061 additions and 1,473 deletions.
10 changes: 8 additions & 2 deletions autotest/pymod/gdaltest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,10 @@ def runexternal(
encoding="latin1",
):
# pylint: disable=unused-argument
command = shlex.split(cmd)
if sys.platform == "win32":
command = cmd
else:
command = shlex.split(cmd)
if strin is None:
p = subprocess.Popen(command, stdout=subprocess.PIPE)
else:
Expand Down Expand Up @@ -1949,7 +1952,10 @@ def _read_in_thread(f, q):

def runexternal_out_and_err(cmd, check_memleak=True, encoding="ascii"):
# pylint: disable=unused-argument
command = shlex.split(cmd)
if sys.platform == "win32":
command = cmd
else:
command = shlex.split(cmd)
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if p.stdout is not None:
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def test_gdal_edit_py_3(script_path):
shutil.copy(test_py_scripts.get_data_path("gcore") + "byte.tif", filename)

try:
test_py_scripts.run_py_script(script_path, "gdal_edit", filename + " -a_srs ''")
test_py_scripts.run_py_script(
script_path, "gdal_edit", filename + " -a_srs None"
)

ds = gdal.Open(filename)
wkt = ds.GetProjectionRef()
Expand Down
178 changes: 83 additions & 95 deletions autotest/pyscripts/test_gdal_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,57 @@ def script_path():
return test_py_scripts.get_py_script("gdal_merge")


@pytest.fixture(scope="module")
def sample_tifs(tmp_path_factory):
tmpdir = tmp_path_factory.mktemp("tmp")

drv = gdal.GetDriverByName("GTiff")
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS("WGS84")
wkt = srs.ExportToWkt()

sample1_tif = str(tmpdir / "in1.tif")
with drv.Create(sample1_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(0)

sample2_tif = str(tmpdir / "in2.tif")
with drv.Create(sample2_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(63)

sample3_tif = str(tmpdir / "in3.tif")
with drv.Create(sample3_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(127)

sample4_tif = str(tmpdir / "in4.tif")
with drv.Create(sample4_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(255)

yield (sample1_tif, sample2_tif, sample3_tif, sample4_tif)


###############################################################################
# Basic test


def test_gdal_merge_1(script_path):
def test_gdal_merge_1(script_path, tmp_path):

output_tif = str(tmp_path / "test_gdal_merge_1.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-o tmp/test_gdal_merge_1.tif "
+ test_py_scripts.get_data_path("gcore")
+ "byte.tif",
f"-o {output_tif} " + test_py_scripts.get_data_path("gcore") + "byte.tif",
)

ds = gdal.Open("tmp/test_gdal_merge_1.tif")
ds = gdal.Open(output_tif)
assert ds.GetRasterBand(1).Checksum() == 4672
ds = None

Expand All @@ -70,44 +106,17 @@ def test_gdal_merge_1(script_path):
# Merge 4 tiles


def test_gdal_merge_2(script_path):

drv = gdal.GetDriverByName("GTiff")
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS("WGS84")
wkt = srs.ExportToWkt()

ds = drv.Create("tmp/in1.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(0)
ds = None

ds = drv.Create("tmp/in2.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(63)
ds = None
def test_gdal_merge_2(script_path, tmp_path, sample_tifs):

ds = drv.Create("tmp/in3.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(127)
ds = None

ds = drv.Create("tmp/in4.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(255)
ds = None
output_tif = str(tmp_path / "test_gdal_merge_2.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-q -o tmp/test_gdal_merge_2.tif tmp/in1.tif tmp/in2.tif tmp/in3.tif tmp/in4.tif",
f"-q -o {output_tif} {' '.join(sample_tifs)}",
)

ds = gdal.Open("tmp/test_gdal_merge_2.tif")
ds = gdal.Open(output_tif)
assert ds.GetProjectionRef().find("WGS 84") != -1, "Expected WGS 84\nGot : %s" % (
ds.GetProjectionRef()
)
Expand All @@ -133,15 +142,17 @@ def test_gdal_merge_2(script_path):
# Test -separate and -v options


def test_gdal_merge_3(script_path):
def test_gdal_merge_3(script_path, tmp_path, sample_tifs):

output_tif = str(tmp_path / "test_gdal_merge_3.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-separate -v -o tmp/test_gdal_merge_3.tif tmp/in1.tif tmp/in2.tif tmp/in3.tif tmp/in4.tif",
f"-separate -v -o {output_tif} {' '.join(sample_tifs)}",
)

ds = gdal.Open("tmp/test_gdal_merge_3.tif")
ds = gdal.Open(output_tif)
assert ds.GetProjectionRef().find("WGS 84") != -1, "Expected WGS 84\nGot : %s" % (
ds.GetProjectionRef()
)
Expand All @@ -167,15 +178,17 @@ def test_gdal_merge_3(script_path):
# Test -init option


def test_gdal_merge_4(script_path):
def test_gdal_merge_4(script_path, tmp_path, sample_tifs):

output_tif = str(tmp_path / "test_gdal_merge_4.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-init 255 -o tmp/test_gdal_merge_4.tif tmp/in2.tif tmp/in3.tif",
f"-init 255 -o {output_tif} {sample_tifs[1]} {sample_tifs[2]}",
)

ds = gdal.Open("tmp/test_gdal_merge_4.tif")
ds = gdal.Open(output_tif)

assert ds.GetRasterBand(1).Checksum() == 4725, "Wrong checksum"

Expand All @@ -184,7 +197,7 @@ def test_gdal_merge_4(script_path):
# Test merging with alpha band (#3669)


def test_gdal_merge_5(script_path):
def test_gdal_merge_5(script_path, tmp_path):
gdal_array = pytest.importorskip("osgeo.gdal_array")
try:
gdal_array.BandRasterIONumPy
Expand All @@ -196,71 +209,46 @@ def test_gdal_merge_5(script_path):
srs.SetWellKnownGeogCS("WGS84")
wkt = srs.ExportToWkt()

ds = drv.Create("tmp/in5.tif", 10, 10, 4)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(255)
ds = None
input1_tif = str(tmp_path / "in5.tif")
with drv.Create(input1_tif, 10, 10, 4) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(255)

ds = drv.Create("tmp/in6.tif", 10, 10, 4)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(2).Fill(255)
ds.GetRasterBand(4).Fill(255)
cs = ds.GetRasterBand(4).Checksum()
ds = None
input2_tif = str(tmp_path / "in6.tif")
with drv.Create(input2_tif, 10, 10, 4) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(2).Fill(255)
ds.GetRasterBand(4).Fill(255)
cs = ds.GetRasterBand(4).Checksum()

output_tif = str(tmp_path / "test_gdal_merge_5.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
" -o tmp/test_gdal_merge_5.tif tmp/in5.tif tmp/in6.tif",
f" -o {output_tif} {input1_tif} {input2_tif}",
)

ds = gdal.Open("tmp/test_gdal_merge_5.tif")
with gdal.Open(output_tif) as ds:

assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"
ds = None
assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"

os.unlink("tmp/test_gdal_merge_5.tif")
os.unlink(output_tif)

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
" -o tmp/test_gdal_merge_5.tif tmp/in6.tif tmp/in5.tif",
f" -o {output_tif} {input1_tif} {input2_tif}",
)

ds = gdal.Open("tmp/test_gdal_merge_5.tif")
with gdal.Open(output_tif) as ds:

assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"


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


def test_gdal_merge_cleanup():

lst = [
"tmp/test_gdal_merge_1.tif",
"tmp/test_gdal_merge_2.tif",
"tmp/test_gdal_merge_3.tif",
"tmp/test_gdal_merge_4.tif",
"tmp/test_gdal_merge_5.tif",
"tmp/in1.tif",
"tmp/in2.tif",
"tmp/in3.tif",
"tmp/in4.tif",
"tmp/in5.tif",
"tmp/in6.tif",
]
for filename in lst:
try:
os.remove(filename)
except OSError:
pass
assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"
Loading

0 comments on commit bacb9fd

Please sign in to comment.