@@ -310,6 +310,10 @@ QString QgsGdalProvider::metadata()
310
310
}
311
311
312
312
}
313
+ if ( mMaskBandExposedAsAlpha )
314
+ {
315
+ myMetadata += " <p class=\" glossy\" >" + tr ( " Mask band (exposed as alpha band)" ) + " </p>\n " ;
316
+ }
313
317
314
318
// end my added code
315
319
@@ -406,7 +410,7 @@ void QgsGdalProvider::readBlock( int bandNo, int xBlock, int yBlock, void *block
406
410
407
411
// QgsDebugMsg( "yBlock = " + QString::number( yBlock ) );
408
412
409
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
413
+ GDALRasterBandH myGdalBand = getBand ( bandNo );
410
414
// GDALReadBlock( myGdalBand, xBlock, yBlock, block );
411
415
412
416
// We have to read with correct data type consistent with other readBlock functions
@@ -584,7 +588,7 @@ void QgsGdalProvider::readBlock( int bandNo, QgsRectangle const & extent, int p
584
588
QgsDebugMsg ( QString ( " Couldn't allocate temporary buffer of %1 bytes" ).arg ( dataSize * tmpWidth * tmpHeight ) );
585
589
return ;
586
590
}
587
- GDALRasterBandH gdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
591
+ GDALRasterBandH gdalBand = getBand ( bandNo );
588
592
GDALDataType type = ( GDALDataType )mGdalDataType .at ( bandNo - 1 );
589
593
CPLErrorReset ();
590
594
@@ -1055,6 +1059,9 @@ int QgsGdalProvider::capabilities() const
1055
1059
1056
1060
Qgis::DataType QgsGdalProvider::sourceDataType ( int bandNo ) const
1057
1061
{
1062
+ if ( mMaskBandExposedAsAlpha && bandNo == GDALGetRasterCount ( mGdalDataset ) + 1 )
1063
+ return dataTypeFromGdal ( GDT_Byte );
1064
+
1058
1065
GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
1059
1066
GDALDataType myGdalDataType = GDALGetRasterDataType ( myGdalBand );
1060
1067
Qgis::DataType myDataType = dataTypeFromGdal ( myGdalDataType );
@@ -1094,14 +1101,17 @@ Qgis::DataType QgsGdalProvider::sourceDataType( int bandNo ) const
1094
1101
1095
1102
Qgis::DataType QgsGdalProvider::dataType ( int bandNo ) const
1096
1103
{
1104
+ if ( mMaskBandExposedAsAlpha && bandNo == GDALGetRasterCount ( mGdalDataset ) + 1 )
1105
+ return dataTypeFromGdal ( GDT_Byte );
1106
+
1097
1107
if ( bandNo <= 0 || bandNo > mGdalDataType .count () ) return Qgis::UnknownDataType;
1098
1108
1099
1109
return dataTypeFromGdal ( mGdalDataType [bandNo-1 ] );
1100
1110
}
1101
1111
1102
1112
double QgsGdalProvider::bandScale ( int bandNo ) const
1103
1113
{
1104
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
1114
+ GDALRasterBandH myGdalBand = getBand ( bandNo );
1105
1115
int bGotScale;
1106
1116
double myScale = GDALGetRasterScale ( myGdalBand, &bGotScale );
1107
1117
if ( bGotScale )
@@ -1112,7 +1122,7 @@ double QgsGdalProvider::bandScale( int bandNo ) const
1112
1122
1113
1123
double QgsGdalProvider::bandOffset ( int bandNo ) const
1114
1124
{
1115
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
1125
+ GDALRasterBandH myGdalBand = getBand ( bandNo );
1116
1126
int bGotOffset;
1117
1127
double myOffset = GDALGetRasterOffset ( myGdalBand, &bGotOffset );
1118
1128
if ( bGotOffset )
@@ -1124,13 +1134,15 @@ double QgsGdalProvider::bandOffset( int bandNo ) const
1124
1134
int QgsGdalProvider::bandCount () const
1125
1135
{
1126
1136
if ( mGdalDataset )
1127
- return GDALGetRasterCount ( mGdalDataset );
1137
+ return GDALGetRasterCount ( mGdalDataset ) + ( mMaskBandExposedAsAlpha ? 1 : 0 ) ;
1128
1138
else
1129
1139
return 1 ;
1130
1140
}
1131
1141
1132
1142
int QgsGdalProvider::colorInterpretation ( int bandNo ) const
1133
1143
{
1144
+ if ( mMaskBandExposedAsAlpha && bandNo == GDALGetRasterCount ( mGdalDataset ) + 1 )
1145
+ return colorInterpretationFromGdal ( GCI_AlphaBand );
1134
1146
GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
1135
1147
return colorInterpretationFromGdal ( GDALGetRasterColorInterpretation ( myGdalBand ) );
1136
1148
}
@@ -1229,7 +1241,7 @@ bool QgsGdalProvider::hasHistogram( int bandNo,
1229
1241
1230
1242
QgsDebugMsg ( " Looking for GDAL histogram" );
1231
1243
1232
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
1244
+ GDALRasterBandH myGdalBand = getBand ( bandNo );
1233
1245
if ( ! myGdalBand )
1234
1246
{
1235
1247
return false ;
@@ -1315,7 +1327,7 @@ QgsRasterHistogram QgsGdalProvider::histogram( int bandNo,
1315
1327
1316
1328
QgsDebugMsg ( " Computing GDAL histogram" );
1317
1329
1318
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
1330
+ GDALRasterBandH myGdalBand = getBand ( bandNo );
1319
1331
1320
1332
int bApproxOK = false ;
1321
1333
if ( sampleSize > 0 )
@@ -2204,7 +2216,7 @@ bool QgsGdalProvider::hasStatistics( int bandNo,
2204
2216
2205
2217
QgsDebugMsg ( " Looking for GDAL statistics" );
2206
2218
2207
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
2219
+ GDALRasterBandH myGdalBand = getBand ( bandNo );
2208
2220
if ( ! myGdalBand )
2209
2221
{
2210
2222
return false ;
@@ -2298,7 +2310,7 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int bandNo, int stats, const
2298
2310
}
2299
2311
2300
2312
QgsDebugMsg ( " Using GDAL statistics." );
2301
- GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , bandNo );
2313
+ GDALRasterBandH myGdalBand = getBand ( bandNo );
2302
2314
2303
2315
// int bApproxOK = false; //as we asked for stats, don't get approx values
2304
2316
// GDAL does not have sample size parameter in API, just bApproxOK or not,
@@ -2551,7 +2563,8 @@ void QgsGdalProvider::initBaseDataset()
2551
2563
// Determine the nodata value and data type
2552
2564
//
2553
2565
// mValidNoDataValue = true;
2554
- for ( int i = 1 ; i <= GDALGetRasterCount ( mGdalBaseDataset ); i++ )
2566
+ const int bandCount = GDALGetRasterCount ( mGdalBaseDataset );
2567
+ for ( int i = 1 ; i <= bandCount; i++ )
2555
2568
{
2556
2569
GDALRasterBandH myGdalBand = GDALGetRasterBand ( mGdalDataset , i );
2557
2570
GDALDataType myGdalDataType = GDALGetRasterDataType ( myGdalBand );
@@ -2657,6 +2670,18 @@ void QgsGdalProvider::initBaseDataset()
2657
2670
// QgsDebugMsg( QString( "mInternalNoDataValue[%1] = %2" ).arg( i - 1 ).arg( mInternalNoDataValue[i-1] ) );
2658
2671
}
2659
2672
2673
+ // Check if the dataset has a mask band, that applies to the whole dataset
2674
+ // If so then expose it as an alpha band.
2675
+ int nMaskFlags = GDALGetMaskFlags ( myGDALBand );
2676
+ if (( nMaskFlags == 0 && bandCount == 1 ) || nMaskFlags == GMF_PER_DATASET )
2677
+ {
2678
+ mMaskBandExposedAsAlpha = true ;
2679
+ mSrcNoDataValue .append ( std::numeric_limits<double >::quiet_NaN () );
2680
+ mSrcHasNoDataValue .append ( false );
2681
+ mUseSrcNoDataValue .append ( false );
2682
+ mGdalDataType .append ( GDT_Byte );
2683
+ }
2684
+
2660
2685
mValid = true ;
2661
2686
}
2662
2687
@@ -2720,8 +2745,7 @@ bool QgsGdalProvider::write( void* data, int band, int width, int height, int xO
2720
2745
{
2721
2746
return false ;
2722
2747
}
2723
-
2724
- GDALRasterBandH rasterBand = GDALGetRasterBand ( mGdalDataset , band );
2748
+ GDALRasterBandH rasterBand = getBand ( band );
2725
2749
if ( !rasterBand )
2726
2750
{
2727
2751
return false ;
@@ -2736,7 +2760,7 @@ bool QgsGdalProvider::setNoDataValue( int bandNo, double noDataValue )
2736
2760
return false ;
2737
2761
}
2738
2762
2739
- GDALRasterBandH rasterBand = GDALGetRasterBand ( mGdalDataset , bandNo );
2763
+ GDALRasterBandH rasterBand = getBand ( bandNo );
2740
2764
CPLErrorReset ();
2741
2765
CPLErr err = GDALSetRasterNoDataValue ( rasterBand, noDataValue );
2742
2766
if ( err != CPLE_None )
@@ -2962,6 +2986,14 @@ bool QgsGdalProvider::setEditable( bool enabled )
2962
2986
return true ;
2963
2987
}
2964
2988
2989
+ GDALRasterBandH QgsGdalProvider::getBand ( int bandNo ) const
2990
+ {
2991
+ if ( mMaskBandExposedAsAlpha && bandNo == GDALGetRasterCount ( mGdalDataset ) + 1 )
2992
+ return GDALGetMaskBand ( GDALGetRasterBand ( mGdalDataset , 1 ) );
2993
+ else
2994
+ return GDALGetRasterBand ( mGdalDataset , bandNo );
2995
+ }
2996
+
2965
2997
// pyramids resampling
2966
2998
2967
2999
// see http://www.gdal.org/gdaladdo.html
0 commit comments