Skip to content

Commit

Permalink
Merge pull request #8578 from OSGeo/backport-8567-to-release/3.7
Browse files Browse the repository at this point in the history
[Backport release/3.7] MBTiles: BuildOverviews(): correctly set minzoom metadata (fixes #8565)
  • Loading branch information
rouault authored Oct 19, 2023
2 parents f33e4f8 + 305e74d commit 4731bad
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 30 deletions.
35 changes: 35 additions & 0 deletions autotest/gdrivers/mbtiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,41 @@ def test_mbtiles_7():
gdal.Unlink("/vsimem/mbtiles_7.mbtiles")


###############################################################################
# Test building overview


@pytest.mark.require_driver("PNG")
def test_mbtiles_overview_minzoom():

filename = "/vsimem/test_mbtiles_overview_minzoom.mbtiles"

gdal.Translate(filename, "data/byte.tif", options="-outsize 1024 0")
ds = gdal.Open(filename, gdal.GA_Update)
assert ds.GetMetadataItem("minzoom") == "17"
assert ds.GetMetadataItem("maxzoom") == "17"
ds.BuildOverviews("NEAR", [2])
ds = None

ds = gdal.Open(filename)
assert ds.GetRasterBand(1).GetOverviewCount() == 1
assert ds.GetMetadataItem("minzoom") == "16"
assert ds.GetMetadataItem("maxzoom") == "17"
ds = None

ds = gdal.Open(filename, gdal.GA_Update)
ds.BuildOverviews("NEAR", [8])
ds = None

ds = gdal.Open(filename)
assert ds.GetRasterBand(1).GetOverviewCount() == 3
assert ds.GetMetadataItem("minzoom") == "14"
assert ds.GetMetadataItem("maxzoom") == "17"
ds = None

gdal.Unlink(filename)


###############################################################################
# Single band with 24 bit color table, PNG

Expand Down
82 changes: 52 additions & 30 deletions frmts/mbtiles/mbtilesdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3539,15 +3539,14 @@ CPLErr MBTilesDataset::IBuildOverviews(
int nRows = 0;
int nCols = 0;
char **papszResult = nullptr;
sqlite3_get_table(hDB, "SELECT * FROM metadata WHERE name = 'minzoom'",
&papszResult, &nRows, &nCols, nullptr);
sqlite3_get_table(
hDB, "SELECT * FROM metadata WHERE name = 'minzoom' LIMIT 2",
&papszResult, &nRows, &nCols, nullptr);
sqlite3_free_table(papszResult);
if (nRows == 1)
{
sqlite3_exec(hDB, "DELETE FROM metadata WHERE name = 'minzoom'",
nullptr, nullptr, nullptr);
pszSQL = sqlite3_mprintf(
"INSERT INTO metadata (name, value) VALUES ('minzoom', '%d')",
"UPDATE metadata SET value = %d WHERE name = 'minzoom'",
m_nZoomLevel);
sqlite3_exec(hDB, pszSQL, nullptr, nullptr, nullptr);
sqlite3_free(pszSQL);
Expand All @@ -3572,6 +3571,18 @@ CPLErr MBTilesDataset::IBuildOverviews(
}

FlushCache(false);

const auto GetOverviewIndex = [](int nVal)
{
int iOvr = -1;
while (nVal > 1)
{
nVal >>= 1;
iOvr++;
}
return iOvr;
};

for (int i = 0; i < nOverviews; i++)
{
if (panOverviewList[i] < 2)
Expand All @@ -3588,40 +3599,34 @@ CPLErr MBTilesDataset::IBuildOverviews(
panOverviewList[i]);
return CE_Failure;
}
const int iOvr = GetOverviewIndex(panOverviewList[i]);
if (iOvr >= m_nOverviewCount)
{
CPLDebug("MBTILES",
"Requested overview factor %d leads to too small overview "
"and will be ignored",
panOverviewList[i]);
}
}

GDALRasterBand ***papapoOverviewBands =
(GDALRasterBand ***)CPLCalloc(sizeof(void *), nBands);
int iCurOverview = 0;
int nMinZoom = m_nZoomLevel;
for (int i = 0; i < m_nOverviewCount; i++)
{
MBTilesDataset *poODS = m_papoOverviewDS[i];
if (poODS->m_nZoomLevel < nMinZoom)
nMinZoom = poODS->m_nZoomLevel;
}
for (int iBand = 0; iBand < nBands; iBand++)
{
papapoOverviewBands[iBand] =
(GDALRasterBand **)CPLCalloc(sizeof(void *), nOverviews);
iCurOverview = 0;
for (int i = 0; i < nOverviews; i++)
{
int nVal = panOverviewList[i];
int iOvr = -1;
while (nVal > 1)
{
nVal >>= 1;
iOvr++;
}
if (iOvr >= m_nOverviewCount)
const int iOvr = GetOverviewIndex(panOverviewList[i]);
if (iOvr < m_nOverviewCount)
{
continue;
MBTilesDataset *poODS = m_papoOverviewDS[iOvr];
papapoOverviewBands[iBand][iCurOverview] =
poODS->GetRasterBand(iBand + 1);
iCurOverview++;
}
MBTilesDataset *poODS = m_papoOverviewDS[iOvr];
papapoOverviewBands[iBand][iCurOverview] =
poODS->GetRasterBand(iBand + 1);
iCurOverview++;
}
}

Expand All @@ -3637,19 +3642,36 @@ CPLErr MBTilesDataset::IBuildOverviews(

if (eErr == CE_None)
{
// Determine new minzoom value from the existing one and the new
// requested overview levels
int nMinZoom = m_nZoomLevel;
bool bHasMinZoomMetadata = false;
int nRows = 0;
int nCols = 0;
char **papszResult = nullptr;
sqlite3_get_table(
hDB, "SELECT * FROM metadata WHERE name = 'minzoom' LIMIT 2",
hDB, "SELECT value FROM metadata WHERE name = 'minzoom' LIMIT 2",
&papszResult, &nRows, &nCols, nullptr);
if (nRows == 1 && nCols == 1 && papszResult[1])
{
bHasMinZoomMetadata = true;
nMinZoom = atoi(papszResult[1]);
}
sqlite3_free_table(papszResult);
if (nRows == 1)
if (bHasMinZoomMetadata)
{
sqlite3_exec(hDB, "DELETE FROM metadata WHERE name = 'minzoom'",
nullptr, nullptr, nullptr);
for (int i = 0; i < nOverviews; i++)
{
const int iOvr = GetOverviewIndex(panOverviewList[i]);
if (iOvr < m_nOverviewCount)
{
const MBTilesDataset *poODS = m_papoOverviewDS[iOvr];
nMinZoom = std::min(nMinZoom, poODS->m_nZoomLevel);
}
}

char *pszSQL = sqlite3_mprintf(
"INSERT INTO metadata (name, value) VALUES ('minzoom', '%d')",
"UPDATE metadata SET value = '%d' WHERE name = 'minzoom'",
nMinZoom);
sqlite3_exec(hDB, pszSQL, nullptr, nullptr, nullptr);
sqlite3_free(pszSQL);
Expand Down

0 comments on commit 4731bad

Please sign in to comment.