@@ -70,6 +70,13 @@ email : tim at linfiniti.com
70
70
# endif
71
71
#endif
72
72
73
+ // Comparison value for equality; i.e., we shouldn't directly compare two
74
+ // floats so it's better to take their difference and see if they're within
75
+ // a certain range -- in this case twenty times the smallest value that
76
+ // doubles can take for the current system. (Yes, 20 was arbitrary.)
77
+ #define TINY_VALUE std::numeric_limits<double >::epsilon() * 20
78
+
79
+
73
80
QgsRasterLayer::QgsRasterLayer (
74
81
QString const & path,
75
82
QString const & baseName,
@@ -106,7 +113,7 @@ QgsRasterLayer::QgsRasterLayer(
106
113
107
114
mBandCount = 0 ;
108
115
mHasPyramids = false ;
109
- mNoDataValue = -9999 ;
116
+ mNoDataValue = -9999.0 ;
110
117
mValidNoDataValue = false ;
111
118
112
119
mGdalBaseDataset = 0 ;
@@ -759,18 +766,11 @@ const QgsRasterBandStats QgsRasterLayer::bandStatistics( int theBandNo )
759
766
myNXBlocks = ( GDALGetRasterXSize ( myGdalBand ) + myXBlockSize - 1 ) / myXBlockSize;
760
767
myNYBlocks = ( GDALGetRasterYSize ( myGdalBand ) + myYBlockSize - 1 ) / myYBlockSize;
761
768
762
- void *myData = CPLMalloc ( myXBlockSize * myYBlockSize * GDALGetDataTypeSize ( myDataType ) / 8 );
769
+ void *myData = CPLMalloc ( myXBlockSize * myYBlockSize * ( GDALGetDataTypeSize ( myDataType ) / 8 ) );
763
770
764
771
// unfortunately we need to make two passes through the data to calculate stddev
765
772
bool myFirstIterationFlag = true ;
766
773
767
- // Comparison value for equality; i.e., we shouldn't directly compare two
768
- // floats so it's better to take their difference and see if they're within
769
- // a certain range -- in this case twenty times the smallest value that
770
- // doubles can take for the current system. (Yes, 20 was arbitrary.)
771
- double myPrecision = std::numeric_limits<double >::epsilon () * 20 ;
772
- Q_UNUSED ( myPrecision );
773
-
774
774
// ifdefs below to remove compiler warning about unused vars
775
775
#ifdef QGISDEBUG
776
776
int success;
@@ -841,10 +841,9 @@ const QgsRasterBandStats QgsRasterLayer::bandStatistics( int theBandNo )
841
841
{
842
842
for ( int iX = 0 ; iX < nXValid; iX++ )
843
843
{
844
- double my = readValue ( myData, myDataType, iX + iY * myXBlockSize );
844
+ double myValue = readValue ( myData, myDataType, iX + ( iY * myXBlockSize ) );
845
845
846
- // if ( mValidNoDataValue && (fabs(my - mNoDataValue) < myPrecision || my == mNoDataValue || my != my))
847
- if ( mValidNoDataValue && ( my == mNoDataValue || my != my ) )
846
+ if ( mValidNoDataValue && ( fabs ( myValue - mNoDataValue ) <= TINY_VALUE || myValue != myValue ) )
848
847
{
849
848
continue ; // NULL
850
849
}
@@ -854,23 +853,23 @@ const QgsRasterBandStats QgsRasterLayer::bandStatistics( int theBandNo )
854
853
{
855
854
// this is the first iteration so initialise vars
856
855
myFirstIterationFlag = false ;
857
- myRasterBandStats.minimumValue = my ;
858
- myRasterBandStats.maximumValue = my ;
856
+ myRasterBandStats.minimumValue = myValue ;
857
+ myRasterBandStats.maximumValue = myValue ;
859
858
++myRasterBandStats.elementCount ;
860
859
} // end of true part for first iteration check
861
860
else
862
861
{
863
862
// this is done for all subsequent iterations
864
- if ( my < myRasterBandStats.minimumValue )
863
+ if ( myValue < myRasterBandStats.minimumValue )
865
864
{
866
- myRasterBandStats.minimumValue = my ;
865
+ myRasterBandStats.minimumValue = myValue ;
867
866
}
868
- if ( my > myRasterBandStats.maximumValue )
867
+ if ( myValue > myRasterBandStats.maximumValue )
869
868
{
870
- myRasterBandStats.maximumValue = my ;
869
+ myRasterBandStats.maximumValue = myValue ;
871
870
}
872
871
873
- myRasterBandStats.sum += my ;
872
+ myRasterBandStats.sum += myValue ;
874
873
++myRasterBandStats.elementCount ;
875
874
} // end of false part for first iteration check
876
875
}
@@ -912,16 +911,15 @@ const QgsRasterBandStats QgsRasterLayer::bandStatistics( int theBandNo )
912
911
{
913
912
for ( int iX = 0 ; iX < nXValid; iX++ )
914
913
{
915
- double my = readValue ( myData, myDataType, iX + iY * myXBlockSize );
914
+ double myValue = readValue ( myData, myDataType, iX + ( iY * myXBlockSize ) );
916
915
917
- // if ( mValidNoDataValue && (fabs(my - mNoDataValue) < myPrecision || my == mNoDataValue || my != my))
918
- if ( mValidNoDataValue && ( my == mNoDataValue || my != my ) )
916
+ if ( mValidNoDataValue && ( fabs ( myValue - mNoDataValue ) <= TINY_VALUE || myValue != myValue ) )
919
917
{
920
918
continue ; // NULL
921
919
}
922
920
923
921
myRasterBandStats.sumOfSquares += static_cast < double >
924
- ( pow ( my - myRasterBandStats.mean , 2 ) );
922
+ ( pow ( myValue - myRasterBandStats.mean , 2 ) );
925
923
}
926
924
}
927
925
} // end of column wise loop
@@ -1893,7 +1891,7 @@ bool QgsRasterLayer::identify( const QgsPoint& thePoint, QMap<QString, QString>&
1893
1891
#endif
1894
1892
QString v;
1895
1893
1896
- if ( mValidNoDataValue && ( mNoDataValue == value || value != value ) )
1894
+ if ( mValidNoDataValue && ( fabs ( value - mNoDataValue ) <= TINY_VALUE || value != value ) )
1897
1895
{
1898
1896
v = tr ( " null (no data)" );
1899
1897
}
@@ -3071,7 +3069,8 @@ bool QgsRasterLayer::readColorTable( int theBandNumber, QList<QgsColorRampShader
3071
3069
3072
3070
void QgsRasterLayer::resetNoDataValue ()
3073
3071
{
3074
- mNoDataValue = -9999 ;
3072
+ mNoDataValue = std::numeric_limits<int >::max ();
3073
+ mValidNoDataValue = false ;
3075
3074
if ( mGdalDataset != NULL && GDALGetRasterCount ( mGdalDataset ) > 0 )
3076
3075
{
3077
3076
int myRequestValid;
@@ -3084,7 +3083,7 @@ void QgsRasterLayer::resetNoDataValue()
3084
3083
}
3085
3084
else
3086
3085
{
3087
- setNoDataValue ( myValue );
3086
+ setNoDataValue ( - 9999.0 );
3088
3087
mValidNoDataValue = false ;
3089
3088
}
3090
3089
}
@@ -4340,7 +4339,7 @@ void QgsRasterLayer::drawMultiBandColor( QPainter * theQPainter, QgsRasterViewPo
4340
4339
myBlueValue = readValue ( myGdalBlueData, ( GDALDataType )myBlueType,
4341
4340
myRow * theRasterViewPort->drawableAreaXDim + myColumn );
4342
4341
4343
- if ( mValidNoDataValue && (( myRedValue == mNoDataValue || myRedValue != myRedValue ) || ( myGreenValue == mNoDataValue || myGreenValue != myGreenValue ) || ( myBlueValue == mNoDataValue || myBlueValue != myBlueValue ) ) )
4342
+ if ( mValidNoDataValue && (( fabs ( myRedValue - mNoDataValue ) <= TINY_VALUE || myRedValue != myRedValue ) || ( fabs ( myGreenValue - mNoDataValue ) <= TINY_VALUE || myGreenValue != myGreenValue ) || ( fabs ( myBlueValue - mNoDataValue ) <= TINY_VALUE || myBlueValue != myBlueValue ) ) )
4344
4343
{
4345
4344
myLineBuffer[ myColumn ] = myDefaultColor;
4346
4345
continue ;
@@ -4466,7 +4465,7 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
4466
4465
myPixelValue = readValue ( myGdalScanData, ( GDALDataType )myDataType,
4467
4466
myRow * theRasterViewPort->drawableAreaXDim + myColumn );
4468
4467
4469
- if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
4468
+ if ( mValidNoDataValue && ( fabs ( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
4470
4469
{
4471
4470
myLineBuffer[ myColumn ] = myDefaultColor;
4472
4471
continue ;
@@ -4554,7 +4553,7 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
4554
4553
myPixelValue = readValue ( myGdalScanData, ( GDALDataType )myDataType,
4555
4554
myRow * theRasterViewPort->drawableAreaXDim + myColumn );
4556
4555
4557
- if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
4556
+ if ( mValidNoDataValue && ( fabs ( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
4558
4557
{
4559
4558
myLineBuffer[ myColumn ] = myDefaultColor;
4560
4559
continue ;
@@ -4664,7 +4663,7 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
4664
4663
myPixelValue = readValue ( myGdalScanData, ( GDALDataType )myDataType,
4665
4664
myRow * theRasterViewPort->drawableAreaXDim + myColumn );
4666
4665
4667
- if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
4666
+ if ( mValidNoDataValue && ( fabs ( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
4668
4667
{
4669
4668
myLineBuffer[ myColumn ] = myDefaultColor;
4670
4669
continue ;
@@ -4773,7 +4772,8 @@ void QgsRasterLayer::drawSingleBandGray( QPainter * theQPainter, QgsRasterViewPo
4773
4772
// against myGrayVal will always fail ( nan==nan always
4774
4773
// returns false, by design), hence the slightly odd comparison
4775
4774
// of myGrayVal against itself.
4776
- if ( mValidNoDataValue && ( myGrayValue == mNoDataValue || myGrayValue != myGrayValue ) )
4775
+
4776
+ if ( mValidNoDataValue && ( fabs ( myGrayValue - mNoDataValue ) <= TINY_VALUE || myGrayValue != myGrayValue ) )
4777
4777
{
4778
4778
myLineBuffer[ myColumn ] = myDefaultColor;
4779
4779
continue ;
@@ -4876,7 +4876,7 @@ void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
4876
4876
myPixelValue = readValue ( myGdalScanData, myDataType,
4877
4877
myRow * theRasterViewPort->drawableAreaXDim + myColumn );
4878
4878
4879
- if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
4879
+ if ( mValidNoDataValue && ( fabs ( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
4880
4880
{
4881
4881
myLineBuffer[ myColumn ] = myDefaultColor;
4882
4882
continue ;
@@ -5198,7 +5198,7 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
5198
5198
//
5199
5199
// Determin the nodatavalue
5200
5200
//
5201
- mNoDataValue = -9999 ; // Standard default?
5201
+ mNoDataValue = -9999.0 ; // Standard default?
5202
5202
mValidNoDataValue = false ;
5203
5203
int isValid = false ;
5204
5204
double myNoDataValue = GDALGetRasterNoDataValue ( GDALGetRasterBand ( mGdalDataset , 1 ), &isValid );
0 commit comments