Skip to content

Commit c4b2480

Browse files
committed
convert GDAL no data value to a value representable by data type, fixes #3840
1 parent 402b397 commit c4b2480

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

src/core/raster/qgsraster.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,24 @@ QgsRaster::ContrastEnhancementLimits QgsRaster::contrastEnhancementLimitsFromStr
5353
return ContrastEnhancementNone;
5454
}
5555

56-
56+
double QgsRaster::representableValue( double value, QGis::DataType dataType )
57+
{
58+
switch ( dataType )
59+
{
60+
case QGis::Byte:
61+
return static_cast<quint8>( value );
62+
case QGis::UInt16:
63+
return static_cast<quint16>( value );
64+
case QGis::Int16:
65+
return static_cast<qint16>( value );
66+
case QGis::UInt32:
67+
return static_cast<quint32>( value );
68+
case QGis::Int32:
69+
return static_cast<qint32>( value );
70+
case QGis::Float32:
71+
return static_cast<float>( value );
72+
default:
73+
break;
74+
}
75+
return value;
76+
}

src/core/raster/qgsraster.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <QString>
2222

23+
#include "qgis.h"
24+
2325
/** \ingroup core
2426
* Raster namespace.
2527
*/
@@ -108,6 +110,12 @@ class CORE_EXPORT QgsRaster
108110
static QString contrastEnhancementLimitsAsString( QgsRaster::ContrastEnhancementLimits theLimits );
109111
static ContrastEnhancementLimits contrastEnhancementLimitsFromString( QString theLimits );
110112

113+
/** Get value representable by given data type.
114+
* Supported are numerical types Byte, UInt16, Int16, UInt32, Int32, Float32, Float64.
115+
* @param value
116+
* @param dataType
117+
* @note added in version 2.1 */
118+
static double representableValue( double value, QGis::DataType dataType );
111119
};
112120

113121
#endif

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,10 @@ void QgsGdalProvider::initBaseDataset()
24112411
if ( isValid )
24122412
{
24132413
QgsDebugMsg( QString( "GDALGetRasterNoDataValue = %1" ).arg( myNoDataValue ) ) ;
2414+
// The no data value double may be non representable by data type, it can result
2415+
// in problems if that value is used to represent additional user defined no data
2416+
// see #3840
2417+
myNoDataValue = QgsRaster::representableValue( myNoDataValue, dataTypeFromGdal( myGdalDataType ) );
24142418
mSrcNoDataValue.append( myNoDataValue );
24152419
mSrcHasNoDataValue.append( true );
24162420
mUseSrcNoDataValue.append( true );

src/providers/wcs/qgswcsprovider.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ QgsWcsProvider::QgsWcsProvider( QString const &uri )
303303
if ( isValid )
304304
{
305305
QgsDebugMsg( QString( "GDALGetRasterNoDataValue = %1" ).arg( myNoDataValue ) ) ;
306+
myNoDataValue = QgsRaster::representableValue( myNoDataValue, dataTypeFromGdal( myGdalDataType ) );
306307
mSrcNoDataValue.append( myNoDataValue );
307308
mSrcHasNoDataValue.append( true );
308309
mUseSrcNoDataValue.append( true );

0 commit comments

Comments
 (0)