diff --git a/autotest/gcore/pam.py b/autotest/gcore/pam.py index 470c29c80ad5..1a9c7da8228d 100755 --- a/autotest/gcore/pam.py +++ b/autotest/gcore/pam.py @@ -423,6 +423,25 @@ def test_pam_13(): gdal.SetConfigOption('GDAL_PAM_ENABLED', 'YES') +############################################################################### +# Test that existing PAM metadata is preserved when new is added +# https://github.com/OSGeo/gdal/issues/1430 + + +def test_pam_metadata_preserved(): + + tmpfilename = '/vsimem/tmp.pnm' + ds = gdal.GetDriverByName('PNM').Create(tmpfilename, 1, 1) + ds.SetMetadataItem('foo', 'bar') + ds = None + ds = gdal.Open(tmpfilename) + ds.GetRasterBand(1).SetMetadataItem('bar', 'baz') + ds = None + ds = gdal.Open(tmpfilename) + assert ds.GetMetadataItem('foo') == 'bar' + assert ds.GetRasterBand(1).GetMetadataItem('bar') == 'baz' + ds = None + gdal.GetDriverByName('PNM').Delete(tmpfilename) ############################################################################### # Cleanup. diff --git a/gdal/gcore/gdalpamdataset.cpp b/gdal/gcore/gdalpamdataset.cpp index 9592d524bbc1..fcdd595ef6fb 100644 --- a/gdal/gcore/gdalpamdataset.cpp +++ b/gdal/gcore/gdalpamdataset.cpp @@ -418,7 +418,10 @@ CPLErr GDALPamDataset::XMLInit( CPLXMLNode *psTree, const char *pszUnused ) /* -------------------------------------------------------------------- */ /* Apply any dataset level metadata. */ /* -------------------------------------------------------------------- */ - oMDMD.XMLInit( psTree, TRUE ); + if( oMDMD.XMLInit( psTree, TRUE ) ) + { + psPam->bHasMetadata = TRUE; + } /* -------------------------------------------------------------------- */ /* Try loading ESRI xml encoded GeodataXform. */