@@ -54,6 +54,7 @@ class TestQgsGdalProvider : public QObject
54
54
void bandName (); // test band name based on `gtiff` tags (#7317)
55
55
void bandNameNoDescription (); // test band name for when no description or tags available (#16047)
56
56
void bandNameWithDescription (); // test band name for when description available (#16047)
57
+ void interactionBetweenRasterChangeAndCache (); // test that updading a raster invalidates the GDAL dataset cache (#20104)
57
58
58
59
private:
59
60
QString mTestDataDir ;
@@ -275,5 +276,50 @@ void TestQgsGdalProvider::bandNameWithDescription()
275
276
delete provider;
276
277
}
277
278
279
+ void TestQgsGdalProvider::interactionBetweenRasterChangeAndCache ()
280
+ {
281
+ double geoTransform[6 ] = { 0 , 2 , 0 , 0 , 0 , -2 };
282
+ QgsCoordinateReferenceSystem crs;
283
+ QString filename = QStringLiteral ( " /vsimem/temp.tif" );
284
+
285
+ // Create a all-0 dataset
286
+ auto provider = QgsRasterDataProvider::create (
287
+ QStringLiteral ( " gdal" ), filename, " GTiff" , 1 , Qgis::Byte , 1 , 1 , geoTransform, crs );
288
+ delete provider;
289
+
290
+ // Open it
291
+ provider = dynamic_cast < QgsRasterDataProvider * >(
292
+ QgsProviderRegistry::instance ()->createProvider (
293
+ QStringLiteral ( " gdal" ), filename, QgsDataProvider::ProviderOptions () ) );
294
+ QVERIFY ( provider );
295
+ auto rp = dynamic_cast < QgsRasterDataProvider * >( provider );
296
+ QVERIFY ( rp );
297
+
298
+ // Create a first clone, and destroys it
299
+ auto rpClone = dynamic_cast < QgsRasterDataProvider *>( rp->clone () );
300
+ QVERIFY ( rpClone );
301
+ QCOMPARE ( rpClone->sample ( QgsPointXY ( 0.5 , -0.5 ), 1 ), 0.0 );
302
+ delete rpClone;
303
+ // Now the main provider should have a cached GDAL dataset corresponding
304
+ // to the one that was used by rpClone
305
+
306
+ // Modify the raster
307
+ rp->setEditable ( true );
308
+ auto rblock = new QgsRasterBlock ( Qgis::Byte , 1 , 1 );
309
+ rblock->setValue ( 0 , 0 , 255 );
310
+ rp->writeBlock ( rblock, 1 , 0 , 0 );
311
+ delete rblock;
312
+ rp->setEditable ( false );
313
+
314
+ // Creates a new clone, and check that we get an updated sample value
315
+ rpClone = dynamic_cast < QgsRasterDataProvider *>( rp->clone () );
316
+ QVERIFY ( rpClone );
317
+ QCOMPARE ( rpClone->sample ( QgsPointXY ( 0.5 , -0.5 ), 1 ), 255.0 );
318
+ delete rpClone;
319
+
320
+ provider->remove ();
321
+ delete provider;
322
+ }
323
+
278
324
QGSTEST_MAIN ( TestQgsGdalProvider )
279
325
#include " testqgsgdalprovider.moc"
0 commit comments