@@ -85,22 +85,31 @@ QgsRasterLayer::QgsRasterLayer(
85
85
mInvertColor( false )
86
86
{
87
87
88
+ mRasterType = QgsRasterLayer::GrayOrUndefined;
89
+
90
+ mRedBandName = TRSTRING_NOT_SET;
91
+ mGreenBandName = TRSTRING_NOT_SET;
92
+ mBlueBandName = TRSTRING_NOT_SET;
93
+ mGrayBandName = TRSTRING_NOT_SET;
94
+ mTransparencyBandName = TRSTRING_NOT_SET;
95
+
96
+
88
97
mUserDefinedRGBMinimumMaximum = false ; // defaults needed to bypass enhanceContrast
89
98
mUserDefinedGrayMinimumMaximum = false ;
90
99
mRGBMinimumMaximumEstimated = true ;
91
100
mGrayMinimumMaximumEstimated = true ;
92
101
102
+ mDrawingStyle = QgsRasterLayer::UndefinedDrawingStyle;
103
+ mContrastEnhancementAlgorithm = QgsContrastEnhancement::NoEnhancement;
104
+ mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader;
93
105
mRasterShader = new QgsRasterShader ();
94
106
95
- if ( loadDefaultStyleFlag )
96
- {
97
- bool defaultLoadedFlag = false ;
98
- loadDefaultStyle ( defaultLoadedFlag );
99
- if ( defaultLoadedFlag )
100
- {
101
- return ;
102
- }
103
- }
107
+ mHasPyramids = false ;
108
+ mNoDataValue = -9999 ;
109
+ mValidNoDataValue = false ;
110
+
111
+ mGdalBaseDataset = 0 ;
112
+ mGdalDataset = 0 ;
104
113
105
114
// Initialise the affine transform matrix
106
115
mGeoTransform [0 ] = 0 ;
@@ -121,6 +130,18 @@ QgsRasterLayer::QgsRasterLayer(
121
130
if ( ! path.isEmpty () )
122
131
{
123
132
readFile ( path ); // XXX check for failure?
133
+
134
+ // readFile() is really an extension of the constructor as many imporant fields are set in this method
135
+ // loadDefaultStyle() can not be called before the layer has actually be opened
136
+ if ( loadDefaultStyleFlag )
137
+ {
138
+ bool defaultLoadedFlag = false ;
139
+ loadDefaultStyle ( defaultLoadedFlag );
140
+ if ( defaultLoadedFlag )
141
+ {
142
+ return ;
143
+ }
144
+ }
124
145
}
125
146
126
147
} // QgsRasterLayer ctor
@@ -199,6 +220,10 @@ QgsRasterLayer::~QgsRasterLayer()
199
220
if ( mGdalBaseDataset )
200
221
{
201
222
GDALDereferenceDataset ( mGdalBaseDataset );
223
+ }
224
+
225
+ if ( mGdalDataset )
226
+ {
202
227
GDALClose ( mGdalDataset );
203
228
}
204
229
}
@@ -3162,8 +3187,6 @@ void QgsRasterLayer::setColorShadingAlgorithm( ColorShadingAlgorithm theShadingA
3162
3187
mRasterShader = new QgsRasterShader ();
3163
3188
}
3164
3189
3165
- mColorShadingAlgorithm = theShadingAlgorithm;
3166
-
3167
3190
switch ( theShadingAlgorithm )
3168
3191
{
3169
3192
case PseudoColorShader:
@@ -3182,6 +3205,9 @@ void QgsRasterLayer::setColorShadingAlgorithm( ColorShadingAlgorithm theShadingA
3182
3205
mRasterShader ->setRasterShaderFunction ( new QgsRasterShaderFunction () );
3183
3206
break ;
3184
3207
}
3208
+
3209
+ // Set the class variable after the call to setRasterShader(), so memory recovery can happen
3210
+ mColorShadingAlgorithm = theShadingAlgorithm;
3185
3211
}
3186
3212
QgsDebugMsg ( " mColorShadingAlgorithm = " + QString::number ( theShadingAlgorithm ) );
3187
3213
}
@@ -3378,6 +3404,12 @@ void QgsRasterLayer::setNoDataValue( double theNoDataValue )
3378
3404
3379
3405
void QgsRasterLayer::setRasterShaderFunction ( QgsRasterShaderFunction* theFunction )
3380
3406
{
3407
+ // Free old shader if it is not a userdefined shader
3408
+ if ( mColorShadingAlgorithm != QgsRasterLayer::UserDefinedShader && 0 != mRasterShader ->rasterShaderFunction () )
3409
+ {
3410
+ delete ( mRasterShader ->rasterShaderFunction () );
3411
+ }
3412
+
3381
3413
if ( theFunction )
3382
3414
{
3383
3415
mRasterShader ->setRasterShaderFunction ( theFunction );
@@ -3387,7 +3419,7 @@ void QgsRasterLayer::setRasterShaderFunction( QgsRasterShaderFunction* theFuncti
3387
3419
{
3388
3420
// If NULL as passed in, set a default shader function to prevent segfaults
3389
3421
mRasterShader ->setRasterShaderFunction ( new QgsRasterShaderFunction () );
3390
- mColorShadingAlgorithm = QgsRasterLayer::UserDefinedShader ;
3422
+ mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader ;
3391
3423
}
3392
3424
}
3393
3425
@@ -5175,6 +5207,8 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
5175
5207
// Set up a new color ramp shader
5176
5208
setColorShadingAlgorithm ( ColorRampShader );
5177
5209
QgsColorRampShader* myColorRampShader = ( QgsColorRampShader* ) mRasterShader ->rasterShaderFunction ();
5210
+ // TODO: Make sure the set algorithm and cast was successful,
5211
+ // e.g., if ( 0 != myColorRampShader && myColorRampShader->shaderTypeAsString == "ColorRampShader" )
5178
5212
myColorRampShader->setColorRampType ( QgsColorRampShader::INTERPOLATED );
5179
5213
myColorRampShader->setColorRampItemList ( *colorTable ( 1 ) );
5180
5214
}
0 commit comments