From 0f16e82430e716b9b1e72f699d9d4d164c22abfa Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sun, 25 Aug 2024 22:27:26 +0200 Subject: [PATCH] netCDF: do not override GDALPamDataset::TrySaveXML() to allow proper mixing of classic and multidim PAM metadata --- autotest/gdrivers/netcdf_multidim.py | 9 ++++++ frmts/netcdf/netcdfdataset.cpp | 43 ++-------------------------- frmts/netcdf/netcdfdataset.h | 2 -- 3 files changed, 11 insertions(+), 43 deletions(-) diff --git a/autotest/gdrivers/netcdf_multidim.py b/autotest/gdrivers/netcdf_multidim.py index f8a993f1dfca..c6134d376c07 100755 --- a/autotest/gdrivers/netcdf_multidim.py +++ b/autotest/gdrivers/netcdf_multidim.py @@ -4005,9 +4005,17 @@ def test2(): rg_subset = rg.SubsetDimensionFromSelection("/x=440750") rg_subset.OpenMDArray("Band1").GetStatistics(False, force=True) + def test3(): + ds = gdal.Open(filename) + ds.SetMetadataItem("foo", "bar") + def reopen(): + ds = gdal.Open(filename) + assert ds.GetMetadataItem("foo") == "bar" + aux_xml = open(filename + ".aux.xml", "rb").read().decode("UTF-8") + assert 'bar' in aux_xml assert ( '' in aux_xml @@ -4053,6 +4061,7 @@ def reopen(): test() test2() + test3() reopen() diff --git a/frmts/netcdf/netcdfdataset.cpp b/frmts/netcdf/netcdfdataset.cpp index e2b2eddcfb94..87d52223e980 100644 --- a/frmts/netcdf/netcdfdataset.cpp +++ b/frmts/netcdf/netcdfdataset.cpp @@ -3033,6 +3033,7 @@ CPLErr netCDFDataset::SetMetadataItem(const char *pszName, const char *pszValue, strchr(osName.c_str(), '#') != nullptr) { // do nothing + return CE_None; } else { @@ -3065,6 +3066,7 @@ CPLErr netCDFDataset::SetMetadata(char **papszMD, const char *pszDomain) SetMetadataItem(pszName, pszValue); CPLFree(pszName); } + return CE_None; } return GDALPamDataset::SetMetadata(papszMD, pszDomain); } @@ -3081,47 +3083,6 @@ const OGRSpatialReference *netCDFDataset::GetSpatialRef() const return GDALPamDataset::GetSpatialRef(); } -/************************************************************************/ -/* SerializeToXML() */ -/************************************************************************/ - -CPLXMLNode *netCDFDataset::SerializeToXML(const char *pszUnused) - -{ - // Overridden from GDALPamDataset to add only band histogram - // and statistics. See bug #4244. - - if (psPam == nullptr) - return nullptr; - - // Setup root node and attributes. - CPLXMLNode *psDSTree = CPLCreateXMLNode(nullptr, CXT_Element, "PAMDataset"); - - // Process bands. - for (int iBand = 0; iBand < GetRasterCount(); iBand++) - { - netCDFRasterBand *poBand = - static_cast(GetRasterBand(iBand + 1)); - - if (poBand == nullptr || !(poBand->GetMOFlags() & GMO_PAM_CLASS)) - continue; - - CPLXMLNode *psBandTree = poBand->SerializeToXML(pszUnused); - - if (psBandTree != nullptr) - CPLAddXMLChild(psDSTree, psBandTree); - } - - // We don't want to return anything if we had no metadata to attach. - if (psDSTree->psChild == nullptr) - { - CPLDestroyXMLNode(psDSTree); - psDSTree = nullptr; - } - - return psDSTree; -} - /************************************************************************/ /* FetchCopyParam() */ /************************************************************************/ diff --git a/frmts/netcdf/netcdfdataset.h b/frmts/netcdf/netcdfdataset.h index fe869dd2e431..536dddae7184 100644 --- a/frmts/netcdf/netcdfdataset.h +++ b/frmts/netcdf/netcdfdataset.h @@ -544,8 +544,6 @@ class netCDFDataset final : public GDALPamDataset void SetSpatialRefNoUpdate(const OGRSpatialReference *); protected: - CPLXMLNode *SerializeToXML(const char *pszVRTPath) override; - OGRLayer *ICreateLayer(const char *pszName, const OGRGeomFieldDefn *poGeomFieldDefn, CSLConstList papszOptions) override;