From 664799a95bb2266bd75f9bab103efc5613130d77 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 24 Sep 2024 15:32:14 +0200 Subject: [PATCH] GTiff: fix to not delete DIMAP XML files when cleaning overviews on a DIMAP2 GeoTIFF file with external overviews --- autotest/gcore/tiff_read.py | 22 ++++++++++++++++++++++ frmts/gtiff/gtiffdataset_read.cpp | 12 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/autotest/gcore/tiff_read.py b/autotest/gcore/tiff_read.py index 3a18f98579a5..b6eca054f950 100755 --- a/autotest/gcore/tiff_read.py +++ b/autotest/gcore/tiff_read.py @@ -5327,3 +5327,25 @@ def test_tiff_read_unrecognized_color_interpretation(): ds = gdal.Open("data/gtiff/unknown_colorinterp.tif") assert ds.GetRasterBand(1).GetColorInterpretation() == gdal.GCI_Undefined assert ds.GetRasterBand(1).GetMetadataItem("COLOR_INTERPRETATION") == "XXXX" + + +############################################################################### +# Check that cleaning overviews on a DIMAP2 GeoTIFF file with external overviews +# does not cause the DIMAP XML file to be cleaned + + +def test_tiff_read_ovr_dimap_pleiades(tmp_path): + + shutil.copytree("../gdrivers/data/dimap2/bundle", tmp_path / "bundle") + filename = str(tmp_path / "bundle" / "IMG_foo_R1C1.TIF") + ds = gdal.Open(filename) + ds.BuildOverviews("NEAR", [2]) + ds = None + ds = gdal.Open(filename + ".ovr") + assert ds.GetFileList() == [filename + ".ovr"] + ds = None + ds = gdal.Open(filename) + ds.BuildOverviews("", []) + ds = None + # Check that cleaning overviews did not suppress the DIMAP XML file + assert os.path.exists(tmp_path / "bundle" / "DIM_foo.XML") diff --git a/frmts/gtiff/gtiffdataset_read.cpp b/frmts/gtiff/gtiffdataset_read.cpp index c8db2594b24d..bb9da16b496d 100644 --- a/frmts/gtiff/gtiffdataset_read.cpp +++ b/frmts/gtiff/gtiffdataset_read.cpp @@ -6653,6 +6653,18 @@ void GTiffDataset::LoadMetadata() return; m_bIMDRPCMetadataLoaded = true; + if (EQUAL(CPLGetExtension(GetDescription()), "ovr")) + { + // Do not attempt to retrieve metadata files on .tif.ovr files. + // For example the Pleiades metadata reader might wrongly associate a + // DIM_xxx.XML file that was meant to be associated with the main + // TIFF file. The consequence of that wrong association is that if + // one cleans overviews, then the Delete() method would then delete + // that DIM_xxx.XML file since it would be reported in the GetFileList() + // of the overview dataset. + return; + } + GDALMDReaderManager mdreadermanager; GDALMDReaderBase *mdreader = mdreadermanager.GetReader( m_pszFilename, oOvManager.GetSiblingFiles(), MDR_ANY);