Skip to content

Commit

Permalink
Merge pull request #4965 from rouault/cog_overview_mask
Browse files Browse the repository at this point in the history
COG: fix potential generation failure when main imagery has overview and mask none
  • Loading branch information
rouault authored and github-actions[bot] committed Dec 10, 2021
1 parent b630c61 commit 8bede32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
19 changes: 19 additions & 0 deletions autotest/gcore/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,3 +1076,22 @@ def test_cog_copy_xmp():
_check_cog(filename)

gdal.Unlink(filename)

###############################################################################
# Test creating COG from a source dataset that has overview with 'odd' sizes
# and a mask without overview


def test_cog_odd_overview_size_and_msk():

filename = '/vsimem/test_cog_odd_overview_size_and_msk.tif'
src_ds = gdal.GetDriverByName('MEM').Create('', 511, 511)
src_ds.BuildOverviews('NEAR', [2])
src_ds.CreateMaskBand(gdal.GMF_PER_DATASET)
ds = gdal.GetDriverByName('COG').CreateCopy(filename, src_ds, options=['BLOCKSIZE=256'])
assert ds
assert ds.GetRasterBand(1).GetOverview(0).XSize == 256
assert ds.GetRasterBand(1).GetMaskBand().GetOverview(0).XSize == 256
ds = None

gdal.Unlink(filename)
25 changes: 20 additions & 5 deletions gdal/frmts/gtiff/cogdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,13 +849,28 @@ GDALDataset* GDALCOGCreator::Create(const char * pszFilename,
nCurLevel --;
}
}
else
else if( bGenerateMskOvr || bGenerateOvr )
{
while( nTmpXSize > nOvrThresholdSize || nTmpYSize > nOvrThresholdSize )
if( !bGenerateOvr )
{
nTmpXSize /= 2;
nTmpYSize /= 2;
asOverviewDims.push_back(std::pair<int,int>(nTmpXSize, nTmpYSize));
// If generating only .msk.ovr, use the exact overview size as
// the overviews of the imagery.
for(int i = 0; i < poFirstBand->GetOverviewCount(); i++ )
{
auto poOvrBand = poFirstBand->GetOverview(i);
asOverviewDims.push_back(std::pair<int,int>(
poOvrBand->GetXSize(), poOvrBand->GetYSize()));
}
}
else
{
while( nTmpXSize > nOvrThresholdSize ||
nTmpYSize > nOvrThresholdSize )
{
nTmpXSize /= 2;
nTmpYSize /= 2;
asOverviewDims.push_back(std::pair<int,int>(nTmpXSize, nTmpYSize));
}
}
}

Expand Down

0 comments on commit 8bede32

Please sign in to comment.