Skip to content

Commit

Permalink
Merge pull request #4867 from rouault/fix_4847
Browse files Browse the repository at this point in the history
GDALPamRasterBand::CloneInfo(): deal correctly with NaN nodata to avoid generating useless .aux.xml file (fixes #4847)
  • Loading branch information
rouault authored Nov 22, 2021
2 parents 5bb3cf6 + ac80057 commit cae6bfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 13 additions & 0 deletions autotest/gcore/pam.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,19 @@ def test_pam_metadata_coordinate_epoch():

gdal.GetDriverByName('PNM').Delete(tmpfilename)

###############################################################################
# Check that PAM handles correctly equality of NaN nodata values (#4847)

def test_pam_nodata_nan():

outfilename = '/vsimem/test_pam_nodata_nan.tif'
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1, gdal.GDT_Float32)
src_ds.GetRasterBand(1).SetNoDataValue(float('nan'))
gdal.GetDriverByName('GTiff').CreateCopy(outfilename, src_ds)
# Check that no PAM file is generated
assert gdal.VSIStatL(outfilename + '.aux.xml') is None
gdal.GetDriverByName('GTiff').Delete(outfilename)

###############################################################################
# Cleanup.

Expand Down
16 changes: 12 additions & 4 deletions gcore/gdalpamrasterband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,15 +564,23 @@ CPLErr GDALPamRasterBand::CloneInfo( GDALRasterBand *poSrcBand,
/* -------------------------------------------------------------------- */
if( nCloneFlags & GCIF_NODATA )
{
int bSuccess = FALSE; // TODO(schwehr): int -> bool.
int bSuccess = FALSE;
const double dfNoData = poSrcBand->GetNoDataValue( &bSuccess );

if( bSuccess )
{
if( !bOnlyIfMissing
|| GetNoDataValue( &bSuccess ) != dfNoData
|| !bSuccess )
if( !bOnlyIfMissing )
GDALPamRasterBand::SetNoDataValue( dfNoData );
else
{
const double dfExistingNoData = GetNoDataValue( &bSuccess );
if( !bSuccess ||
!((std::isnan(dfExistingNoData) && std::isnan(dfNoData)) ||
dfExistingNoData == dfNoData) )
{
GDALPamRasterBand::SetNoDataValue( dfNoData );
}
}
}
}

Expand Down

0 comments on commit cae6bfc

Please sign in to comment.