Skip to content

Commit 70d4a27

Browse files
committed
Do not persist estimated GDAL metadata
GDAL saves metadata like min and max values into a .aux.xml sidecar file next to raster files. It does this always, even when the calculated values are estimated. In subsequent runs of GDAL processing tools it will use these values as if they were reliable. This patch takes care of deleting newly written .aux.xml files if there is a risk that they include estimated data. Fix #19517 https://issues.qgis.org/issues/19517
1 parent 190f938 commit 70d4a27

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,15 @@ QgsGdalProvider::~QgsGdalProvider()
470470
}
471471
if ( mGdalDataset )
472472
{
473+
// Check if already a PAM (persistent auxiliary metadata) file exists
474+
QString pamFile = dataSourceUri( true ) + QLatin1String( ".aux.xml" );
475+
bool pamFileAlreadyExists = QFileInfo( pamFile ).exists();
476+
473477
GDALClose( mGdalDataset );
478+
479+
// If GDAL created a PAM file right now by using estimated metadata, delete it right away
480+
if ( !mStatisticsAreReliable && !pamFileAlreadyExists && QFileInfo( pamFile ).exists() )
481+
QFile( pamFile ).remove();
474482
}
475483

476484
if ( mpParent && *mpParent == this )
@@ -2468,6 +2476,7 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int bandNo, int stats, const
24682476
myerval = GDALComputeRasterStatistics( myGdalBand, bApproxOK,
24692477
&pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
24702478
progressCallback, &myProg );
2479+
mStatisticsAreReliable = true;
24712480
}
24722481
else
24732482
{

src/providers/gdal/qgsgdalprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
296296
* Converts a world (\a x, \a y) coordinate to a pixel \a row and \a col.
297297
*/
298298
bool worldToPixel( double x, double y, int &col, int &row ) const;
299+
300+
bool mStatisticsAreReliable = false;
299301
};
300302

301303
#endif

0 commit comments

Comments
 (0)