Skip to content

Commit

Permalink
Merge pull request #8690 from rouault/vrt_approx_stats
Browse files Browse the repository at this point in the history
VRTSourcedRasterBand: serialize approximate statistics inside .vrt when there are overviews
  • Loading branch information
rouault authored Nov 13, 2023
2 parents 65af0b8 + 7bd5c5e commit 8e42f75
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 29 additions & 0 deletions autotest/gcore/vrt_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2535,3 +2535,32 @@ def test_vrt_read_complex_source_nodata_out_of_range(tmp_vsimem, data_type):
got_data = vrt_ds.ReadRaster(buf_type=data_type)
got_data = struct.unpack(array_type, got_data)
assert got_data == (1,)


###############################################################################
# Test serialization of approximate ComputeStatistics() when there is
# external overview


def test_vrt_read_compute_statistics_approximate(tmp_vsimem):

gtiff_filename = str(tmp_vsimem / "tmp.tif")
gdal.Translate(
gtiff_filename, gdal.Open("data/byte.tif"), format="GTiff", width=256
)
vrt_filename = str(tmp_vsimem / "tmp.vrt")
gdal.Translate(vrt_filename, gtiff_filename)
ds = gdal.Open(vrt_filename)
ds.BuildOverviews("NEAR", [2])
ds = None
ds = gdal.Open(vrt_filename)
ds.GetRasterBand(1).ComputeStatistics(True)
ds = None
ds = gdal.Open(vrt_filename)
md = ds.GetRasterBand(1).GetMetadata()
assert md["STATISTICS_APPROXIMATE"] == "YES"
assert "STATISTICS_MINIMUM" in md
assert "STATISTICS_MAXIMUM" in md
assert "STATISTICS_MEAN" in md
assert "STATISTICS_STDDEV" in md
assert md["STATISTICS_VALID_PERCENT"] == "100"
15 changes: 12 additions & 3 deletions frmts/vrt/vrtsourcedrasterband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,23 +1233,32 @@ CPLErr VRTSourcedRasterBand::ComputeStatistics(int bApproxOK, double *pdfMin,
if (poBand != nullptr && poBand != this)
{
auto l_poDS = dynamic_cast<VRTDataset *>(poDS);
CPLErr eErr;
if (l_poDS && !l_poDS->m_apoOverviews.empty() &&
dynamic_cast<VRTSourcedRasterBand *>(poBand) != nullptr)
{
auto apoTmpOverviews = std::move(l_poDS->m_apoOverviews);
l_poDS->m_apoOverviews.clear();
auto eErr = poBand->GDALRasterBand::ComputeStatistics(
eErr = poBand->GDALRasterBand::ComputeStatistics(
TRUE, pdfMin, pdfMax, pdfMean, pdfStdDev, pfnProgress,
pProgressData);
l_poDS->m_apoOverviews = std::move(apoTmpOverviews);
return eErr;
}
else
{
return poBand->ComputeStatistics(TRUE, pdfMin, pdfMax, pdfMean,
eErr = poBand->ComputeStatistics(TRUE, pdfMin, pdfMax, pdfMean,
pdfStdDev, pfnProgress,
pProgressData);
}
if (eErr == CE_None && pdfMin && pdfMax && pdfMean && pdfStdDev)
{
SetMetadataItem("STATISTICS_APPROXIMATE", "YES");
SetMetadataItem(
"STATISTICS_VALID_PERCENT",
poBand->GetMetadataItem("STATISTICS_VALID_PERCENT"));
SetStatistics(*pdfMin, *pdfMax, *pdfMean, *pdfStdDev);
}
return eErr;
}
}

Expand Down

0 comments on commit 8e42f75

Please sign in to comment.