Skip to content

Commit

Permalink
netCDF: do not override GDALPamDataset::TrySaveXML() to allow proper …
Browse files Browse the repository at this point in the history
…mixing of classic and multidim PAM metadata
  • Loading branch information
rouault committed Aug 25, 2024
1 parent 9ff4c5d commit 0f16e82
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
9 changes: 9 additions & 0 deletions autotest/gdrivers/netcdf_multidim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<MDI key="foo">bar</MDI>' in aux_xml
assert (
'<DerivedDataset name="AsClassicDataset(1,0) view of Sliced view of /Band1 ([0:10,...])">'
in aux_xml
Expand Down Expand Up @@ -4053,6 +4061,7 @@ def reopen():

test()
test2()
test3()
reopen()


Expand Down
43 changes: 2 additions & 41 deletions frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,7 @@ CPLErr netCDFDataset::SetMetadataItem(const char *pszName, const char *pszValue,
strchr(osName.c_str(), '#') != nullptr)
{
// do nothing
return CE_None;
}
else
{
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<netCDFRasterBand *>(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() */
/************************************************************************/
Expand Down
2 changes: 0 additions & 2 deletions frmts/netcdf/netcdfdataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0f16e82

Please sign in to comment.