Skip to content

Commit

Permalink
[gdalwarp] Fix -ovr {absolute_value} (#10193)
Browse files Browse the repository at this point in the history
Fix #10174 -ovr {absolute_value} doesn't influence choice of output resolution
  • Loading branch information
elpaso committed Jun 12, 2024
1 parent 6bd9d23 commit 75c9020
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apps/gdalwarp_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3680,6 +3680,18 @@ static GDALDatasetH GDALWarpCreateOutput(
return nullptr;
}

// Examine desired overview level and retrieve the corresponding dataset
// if it exists.
std::unique_ptr<GDALDataset> oDstDSOverview;
if (psOptions->nOvLevel >= 0)
{
oDstDSOverview.reset(GDALCreateOverviewDataset(
GDALDataset::FromHandle(hSrcDS), psOptions->nOvLevel,
/* bThisLevelOnly = */ true));
if (oDstDSOverview)
hSrcDS = oDstDSOverview.get();
}

/* --------------------------------------------------------------------
*/
/* Check if the source dataset shares some files with the dest
Expand Down Expand Up @@ -4110,6 +4122,7 @@ static GDALDatasetH GDALWarpCreateOutput(
{
nOptions |= GDAL_SWO_FORCE_SQUARE_PIXEL;
}

if (GDALSuggestedWarpOutput2(hSrcDS, psInfo->pfnTransform,
hTransformArg, adfThisGeoTransform,
&nThisPixels, &nThisLines, adfExtent,
Expand Down
26 changes: 26 additions & 0 deletions autotest/utilities/test_gdalwarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
###############################################################################

import os
import shutil
import stat

import gdaltest
Expand Down Expand Up @@ -1049,6 +1050,7 @@ def test_gdalwarp_39(gdalwarp_path, tmp_path):
def test_gdalwarp_40(gdalwarp_path, tmp_path):

src_tif = str(tmp_path / "test_gdalwarp_40_src.tif")
src_tif_copy = str(tmp_path / "test_gdalwarp_40_src_copy.tif")
dst_tif = str(tmp_path / "test_gdalwarp_40.tif")
dst_vrt = str(tmp_path / "test_gdalwarp_40.vrt")

Expand All @@ -1060,8 +1062,11 @@ def test_gdalwarp_40(gdalwarp_path, tmp_path):
cs_ov0 = out_ds.GetRasterBand(1).GetOverview(0).Checksum()
out_ds.GetRasterBand(1).GetOverview(1).Fill(255)
cs_ov1 = out_ds.GetRasterBand(1).GetOverview(1).Checksum()

out_ds = None

shutil.copy(src_tif, src_tif_copy)

# Should select main resolution
gdaltest.runexternal(f"{gdalwarp_path} {src_tif} {dst_tif} -overwrite")

Expand Down Expand Up @@ -1113,6 +1118,27 @@ def test_gdalwarp_40(gdalwarp_path, tmp_path):
assert ds.GetRasterBand(1).Checksum() == cs_ov0
ds = None

# Should select overview 0
gdaltest.runexternal(f"{gdalwarp_path} {src_tif} {dst_tif} -overwrite -ovr 0")

ds = gdal.Open(dst_tif)
assert ds.GetRasterBand(1).Checksum() == cs_ov0
ds = None

# Should select overview 0 (no overwrite)
gdaltest.runexternal(f"{gdalwarp_path} {src_tif} {dst_tif} -ovr 0")

# Repeat with no output file and no overwrite (takes a different code path)
os.unlink(dst_tif)
gdaltest.runexternal(f"{gdalwarp_path} {src_tif} {dst_tif} -ovr 0")

# Should not crash (actually it never did)
os.unlink(dst_tif)
gdaltest.runexternal(f"{gdalwarp_path} {src_tif} {src_tif_copy} {dst_tif} -ovr 0")
ds = gdal.Open(dst_tif)
assert ds.GetRasterBand(1).Checksum() == cs_ov0
ds = None

# Should select overview 0 through VRT
gdaltest.runexternal(
f"{gdalwarp_path} {src_tif} {dst_vrt} -overwrite -ts 10 10 -of VRT"
Expand Down

0 comments on commit 75c9020

Please sign in to comment.