Skip to content

Commit 688891a

Browse files
authored
Merge pull request #12427 from rouault/CheckCompatibleForDatasetIO
VRTDataset::CheckCompatibleForDatasetIO(): work better with anonymous datasets
2 parents 7672dd7 + ada99c6 commit 688891a

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

autotest/gcore/vrt_read.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,3 +3027,15 @@ def test_vrt_resampling_with_alpha_and_overviews(tmp_vsimem):
30273027
assert ds.ReadRaster(
30283028
buf_xsize=10, buf_ysize=1
30293029
) == b"\xFF\xFF\xFF\xFF\xFB\xF1\xF0\xF0\xF0\xF0" + (b"\xFF" * 10)
3030+
3031+
3032+
###############################################################################
3033+
3034+
3035+
def test_vrt_read_CheckCompatibleForDatasetIO():
3036+
3037+
anonymous_vrt = gdal.Translate("", "data/rgbsmall.tif", format="MEM")
3038+
another_vrt = gdal.Translate("", anonymous_vrt, width=25, format="VRT")
3039+
assert (
3040+
another_vrt.GetMetadataItem("CheckCompatibleForDatasetIO()", "__DEBUG__") == "1"
3041+
)

frmts/vrt/vrtdataset.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ const char *VRTDataset::GetMetadataItem(const char *pszName,
229229
{
230230
if (EQUAL(pszName, "MULTI_THREADED_RASTERIO_LAST_USED"))
231231
return m_bMultiThreadedRasterIOLastUsed ? "1" : "0";
232+
else if (EQUAL(pszName, "CheckCompatibleForDatasetIO()"))
233+
return CheckCompatibleForDatasetIO() ? "1" : "0";
232234
}
233235
return GDALDataset::GetMetadataItem(pszName, pszDomain);
234236
}
@@ -2147,6 +2149,7 @@ bool VRTDataset::CheckCompatibleForDatasetIO() const
21472149

21482150
m_nCompatibleForDatasetIO = false;
21492151

2152+
GDALDataset *poFirstBandSourceDS = nullptr;
21502153
for (int iBand = 0; iBand < nBands; iBand++)
21512154
{
21522155
auto poVRTBand = static_cast<VRTRasterBand *>(papoBands[iBand]);
@@ -2176,8 +2179,12 @@ bool VRTDataset::CheckCompatibleForDatasetIO() const
21762179
return false;
21772180

21782181
if (poSource->m_nBand != iBand + 1 ||
2179-
poSource->m_bGetMaskBand || poSource->m_osSrcDSName.empty())
2182+
poSource->m_bGetMaskBand ||
2183+
(nSources > 1 && poSource->m_osSrcDSName.empty()))
21802184
return false;
2185+
if (nSources == 1 && poSource->m_osSrcDSName.empty())
2186+
poFirstBandSourceDS =
2187+
poSource->GetRasterBand()->GetDataset();
21812188
osResampling = poSource->GetResampling();
21822189
}
21832190
}
@@ -2200,12 +2207,19 @@ bool VRTDataset::CheckCompatibleForDatasetIO() const
22002207
if (poSource->GetType() != VRTSimpleSource::GetTypeStatic())
22012208
return false;
22022209
if (poSource->m_nBand != iBand + 1 ||
2203-
poSource->m_bGetMaskBand || poSource->m_osSrcDSName.empty())
2210+
poSource->m_bGetMaskBand ||
2211+
(nSources > 1 && poSource->m_osSrcDSName.empty()))
22042212
return false;
22052213
if (!poSource->IsSameExceptBandNumber(poRefSource))
22062214
return false;
22072215
if (osResampling.compare(poSource->GetResampling()) != 0)
22082216
return false;
2217+
if (nSources == 1 && poSource->m_osSrcDSName.empty() &&
2218+
poFirstBandSourceDS !=
2219+
poSource->GetRasterBand()->GetDataset())
2220+
{
2221+
return false;
2222+
}
22092223
}
22102224
}
22112225
}

frmts/vrt/vrtsources.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,6 @@ bool VRTSimpleSource::IsSameExceptBandNumber(
900900
m_dfDstYOff == poOtherSource->m_dfDstYOff &&
901901
m_dfDstXSize == poOtherSource->m_dfDstXSize &&
902902
m_dfDstYSize == poOtherSource->m_dfDstYSize &&
903-
!m_osSrcDSName.empty() &&
904903
m_osSrcDSName == poOtherSource->m_osSrcDSName;
905904
}
906905

0 commit comments

Comments
 (0)