Skip to content
Permalink
Browse files

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
  • Loading branch information
m-kuhn committed Oct 24, 2018
1 parent 607cd3e commit 87fddaee59ea30768ea3c6e7332a8a2dcce8a6d8
Showing with 13 additions and 0 deletions.
  1. +11 −0 src/providers/gdal/qgsgdalprovider.cpp
  2. +2 −0 src/providers/gdal/qgsgdalprovider.h
@@ -105,6 +105,7 @@ QgsGdalProvider::QgsGdalProvider( const QString &uri, QgsError error )
, mYBlockSize( 0 )
, mGdalBaseDataset( nullptr )
, mGdalDataset( nullptr )
, mStatisticsAreReliable( false )
{
mGeoTransform[0] = 0;
mGeoTransform[1] = 1;
@@ -127,6 +128,7 @@ QgsGdalProvider::QgsGdalProvider( const QString &uri, bool update )
, mYBlockSize( 0 )
, mGdalBaseDataset( nullptr )
, mGdalDataset( nullptr )
, mStatisticsAreReliable( false )
{
mGeoTransform[0] = 0;
mGeoTransform[1] = 1;
@@ -240,7 +242,15 @@ QgsGdalProvider::~QgsGdalProvider()
}
if ( mGdalDataset )
{
// Check if already a PAM (persistent auxiliary metadata) file exists
QString pamFile = dataSourceUri() + QLatin1String( ".aux.xml" );
bool pamFileAlreadyExists = QFileInfo( pamFile ).exists();

GDALClose( mGdalDataset );

// If GDAL created a PAM file right now by using estimated metadata, delete it right away
if ( !mStatisticsAreReliable && !pamFileAlreadyExists && QFileInfo( pamFile ).exists() )
QFile( pamFile ).remove();
}
}

@@ -2443,6 +2453,7 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int theBandNo, int theStats,
myerval = GDALComputeRasterStatistics( myGdalBand, bApproxOK,
&pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
progressCallback, &myProg );
mStatisticsAreReliable = true;
}
else
{
@@ -301,6 +301,8 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase

/** \brief sublayers list saved for subsequent access */
QStringList mSubLayers;

bool mStatisticsAreReliable;
};

#endif

0 comments on commit 87fddae

Please sign in to comment.
You can’t perform that action at this time.