Skip to content

Commit

Permalink
RasterIO: fix subpixel shift when reading from overviews with non-nea…
Browse files Browse the repository at this point in the history
…rest resampling
  • Loading branch information
tbonfort authored and github-actions[bot] committed Nov 23, 2023
1 parent 6b520a0 commit 48aceac
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
43 changes: 43 additions & 0 deletions autotest/gcore/rasterio.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,49 @@ def test_rasterio_9():
assert tab[0] == pytest.approx(1.0, abs=1e-5)


##############################################################################
# Test resampled reading from an overview level (#8794)


def test_rasterio_overview_subpixel_resampling():

numpy = pytest.importorskip("numpy")

temp_path = "/vsimem/rasterio_ovr.tif"
ds = gdal.GetDriverByName("GTiff").Create(temp_path, 8, 8, 1, gdal.GDT_Byte)
ds.GetRasterBand(1).WriteArray(
numpy.array(
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 255, 255, 255, 255, 0, 0],
[0, 0, 255, 255, 255, 255, 0, 0],
[0, 0, 255, 255, 255, 255, 0, 0],
[0, 0, 255, 255, 255, 255, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
]
)
)
ds.BuildOverviews("NEAREST", overviewlist=[2])

pix = ds.GetRasterBand(1).ReadAsArray(
xoff=1,
yoff=1,
buf_xsize=3,
buf_ysize=3,
win_xsize=6,
win_ysize=6,
resample_alg=gdal.GRIORA_Bilinear,
)
assert numpy.all(
pix == numpy.array([[64, 128, 64], [128, 255, 128], [64, 128, 64]])
)

ds = None
gdal.Unlink("/vsimem/rasterio_ovr.tif")


###############################################################################
# Test error when getting a block

Expand Down
27 changes: 19 additions & 8 deletions gcore/rasterio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3667,19 +3667,30 @@ int GDALBandGetBestOverviewLevel2(GDALRasterBand *poBand, int &nXOff,
if (nOYOff + nOYSize > poBestOverview->GetYSize())
nOYSize = poBestOverview->GetYSize() - nOYOff;

if (psExtraArg)
{
if (psExtraArg->bFloatingPointWindowValidity)
{
psExtraArg->dfXOff /= dfXRes;
psExtraArg->dfXSize /= dfXRes;
psExtraArg->dfYOff /= dfYRes;
psExtraArg->dfYSize /= dfYRes;
}
else if (psExtraArg->eResampleAlg != GRIORA_NearestNeighbour)
{
psExtraArg->bFloatingPointWindowValidity = true;
psExtraArg->dfXOff = nXOff / dfXRes;
psExtraArg->dfXSize = nXSize / dfXRes;
psExtraArg->dfYOff = nYOff / dfYRes;
psExtraArg->dfYSize = nYSize / dfYRes;
}
}

nXOff = nOXOff;
nYOff = nOYOff;
nXSize = nOXSize;
nYSize = nOYSize;

if (psExtraArg && psExtraArg->bFloatingPointWindowValidity)
{
psExtraArg->dfXOff /= dfXRes;
psExtraArg->dfXSize /= dfXRes;
psExtraArg->dfYOff /= dfYRes;
psExtraArg->dfYSize /= dfYRes;
}

return nBestOverviewLevel;
}

Expand Down

0 comments on commit 48aceac

Please sign in to comment.