[QGIS Commit] r10172 - trunk/qgis/src/core/raster

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun Feb 15 10:16:04 EST 2009


Author: ersts
Date: 2009-02-15 10:16:04 -0500 (Sun, 15 Feb 2009)
New Revision: 10172

Modified:
   trunk/qgis/src/core/raster/qgsrasterlayer.cpp
Log:
-Fixed problem when loading of default style, closes ticket #1480
-Added additional initialization of QgsRasterLayer class variables
-Plugged a memory leak when chaning shaders

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.cpp
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2009-02-15 01:48:08 UTC (rev 10171)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2009-02-15 15:16:04 UTC (rev 10172)
@@ -85,23 +85,32 @@
     mInvertColor( false )
 {
 
+  mRasterType = QgsRasterLayer::GrayOrUndefined;
+
+  mRedBandName = TRSTRING_NOT_SET;
+  mGreenBandName = TRSTRING_NOT_SET;
+  mBlueBandName = TRSTRING_NOT_SET;
+  mGrayBandName = TRSTRING_NOT_SET;
+  mTransparencyBandName = TRSTRING_NOT_SET;
+
+
   mUserDefinedRGBMinimumMaximum = false; //defaults needed to bypass enhanceContrast
   mUserDefinedGrayMinimumMaximum = false;
   mRGBMinimumMaximumEstimated = true;
   mGrayMinimumMaximumEstimated = true;
 
+  mDrawingStyle = QgsRasterLayer::UndefinedDrawingStyle;
+  mContrastEnhancementAlgorithm = QgsContrastEnhancement::NoEnhancement;
+  mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader;
   mRasterShader = new QgsRasterShader();
 
-  if ( loadDefaultStyleFlag )
-  {
-    bool defaultLoadedFlag = false;
-    loadDefaultStyle( defaultLoadedFlag );
-    if ( defaultLoadedFlag )
-    {
-      return;
-    }
-  }
+  mHasPyramids = false;
+  mNoDataValue = -9999;
+  mValidNoDataValue = false;
 
+  mGdalBaseDataset = 0;
+  mGdalDataset = 0;
+
   // Initialise the affine transform matrix
   mGeoTransform[0] =  0;
   mGeoTransform[1] =  1;
@@ -121,6 +130,18 @@
   if ( ! path.isEmpty() )
   {
     readFile( path ); // XXX check for failure?
+
+    //readFile() is really an extension of the constructor as many imporant fields are set in this method
+    //loadDefaultStyle() can not be called before the layer has actually be opened
+    if ( loadDefaultStyleFlag )
+    {
+      bool defaultLoadedFlag = false;
+      loadDefaultStyle( defaultLoadedFlag );
+      if ( defaultLoadedFlag )
+      {
+        return;
+      }
+    }
   }
 
 } // QgsRasterLayer ctor
@@ -199,6 +220,10 @@
     if ( mGdalBaseDataset )
     {
       GDALDereferenceDataset( mGdalBaseDataset );
+    }
+
+    if( mGdalDataset )
+    {
       GDALClose( mGdalDataset );
     }
   }
@@ -3162,8 +3187,6 @@
       mRasterShader = new QgsRasterShader();
     }
 
-    mColorShadingAlgorithm = theShadingAlgorithm;
-
     switch ( theShadingAlgorithm )
     {
       case PseudoColorShader:
@@ -3182,6 +3205,9 @@
         mRasterShader->setRasterShaderFunction( new QgsRasterShaderFunction() );
         break;
     }
+
+    //Set the class variable after the call to setRasterShader(), so memory recovery can happen
+    mColorShadingAlgorithm = theShadingAlgorithm;
   }
   QgsDebugMsg( "mColorShadingAlgorithm = " + QString::number( theShadingAlgorithm ) );
 }
@@ -3378,6 +3404,12 @@
 
 void QgsRasterLayer::setRasterShaderFunction( QgsRasterShaderFunction* theFunction )
 {
+  //Free old shader if it is not a userdefined shader
+  if( mColorShadingAlgorithm != QgsRasterLayer::UserDefinedShader && 0 != mRasterShader->rasterShaderFunction() )
+  {
+    delete( mRasterShader->rasterShaderFunction() );
+  }
+
   if ( theFunction )
   {
     mRasterShader->setRasterShaderFunction( theFunction );
@@ -3387,7 +3419,7 @@
   {
     //If NULL as passed in, set a default shader function to prevent segfaults
     mRasterShader->setRasterShaderFunction( new QgsRasterShaderFunction() );
-    mColorShadingAlgorithm = QgsRasterLayer::UserDefinedShader;
+    mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader;
   }
 }
 
@@ -5175,6 +5207,8 @@
     //Set up a new color ramp shader
     setColorShadingAlgorithm( ColorRampShader );
     QgsColorRampShader* myColorRampShader = ( QgsColorRampShader* ) mRasterShader->rasterShaderFunction();
+    //TODO: Make sure the set algorithm and cast was successful,
+    //e.g., if ( 0 != myColorRampShader && myColorRampShader->shaderTypeAsString == "ColorRampShader" )
     myColorRampShader->setColorRampType( QgsColorRampShader::INTERPOLATED );
     myColorRampShader->setColorRampItemList( *colorTable( 1 ) );
   }



More information about the QGIS-commit mailing list