Skip to content
Permalink
Browse files
Implement readXML for paletted raster renderer
  • Loading branch information
mhugent committed Apr 1, 2012
1 parent 180c4fb commit 7a43e450094a5a33b25f1d18b0f3f886a6d9f922
@@ -319,6 +319,10 @@ void QgsRasterLayerProperties::populateTransparencyTable()
return;
}
const QgsRasterTransparency* rasterTransparency = renderer->rasterTransparency();
if ( !rasterTransparency )
{
return;
}

//Clear existing color transparency list
//NOTE: May want to just tableTransparency->clearContents() and fill back in after checking to be sure list and table are the same size
@@ -55,7 +55,7 @@ void QgsMultiBandColorRenderer::setBlueContrastEnhancement( QgsContrastEnhanceme
delete mBlueContrastEnhancement; mBlueContrastEnhancement = ce;
}

QgsRasterRenderer* QgsMultiBandColorRenderer::create( const QDomElement& elem )
QgsRasterRenderer* QgsMultiBandColorRenderer::create( const QDomElement& elem, QgsRasterDataProvider* provider )
{
return 0;
}
@@ -290,19 +290,19 @@ void QgsMultiBandColorRenderer::writeXML( QDomDocument& doc, QDomElement& parent
rasterRendererElem.setAttribute( "blueBand", mBlueBand );

//contrast enhancement
if( mRedContrastEnhancement )
if ( mRedContrastEnhancement )
{
QDomElement redContrastElem = doc.createElement( "redContrastEnhancement" );
mRedContrastEnhancement->writeXML( doc, redContrastElem );
rasterRendererElem.appendChild( redContrastElem );
}
if( mGreenContrastEnhancement )
if ( mGreenContrastEnhancement )
{
QDomElement greenContrastElem = doc.createElement( "greenContrastEnhancement" );
mGreenContrastEnhancement->writeXML( doc, greenContrastElem );
rasterRendererElem.appendChild( greenContrastElem );
}
if( mBlueContrastEnhancement )
if ( mBlueContrastEnhancement )
{
QDomElement blueContrastElem = doc.createElement( "blueContrastEnhancement" );
mBlueContrastEnhancement->writeXML( doc, blueContrastElem );
@@ -32,7 +32,7 @@ class QgsMultiBandColorRenderer: public QgsRasterRenderer
QgsContrastEnhancement* blueEnhancement = 0 );
~QgsMultiBandColorRenderer();

static QgsRasterRenderer* create( const QDomElement& elem );
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterDataProvider* provider );

void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

@@ -34,9 +34,36 @@ QgsPalettedRasterRenderer::~QgsPalettedRasterRenderer()
delete[] mColors;
}

QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem )
QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, QgsRasterDataProvider* provider )
{
return 0;
if ( elem.isNull() )
{
return 0;
}

int bandNumber = elem.attribute( "band", "-1" ).toInt();
int nColors;
QColor* colors;

QDomElement paletteElem = elem.firstChildElement( "colorPalette" );
if ( !paletteElem.isNull() )
{
QDomNodeList paletteEntries = paletteElem.elementsByTagName( "paletteEntry" );
nColors = paletteEntries.size();
colors = new QColor[ nColors ];

int value = 0;
QDomElement entryElem;
for ( int i = 0; i < nColors; ++i )
{
entryElem = paletteEntries.at( i ).toElement();
value = entryElem.attribute( "value", "0" ).toInt();
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) );
}
}
QgsRasterRenderer* r = new QgsPalettedRasterRenderer( provider, bandNumber, colors, nColors );
r->readXML( elem );
return r;
}

QColor* QgsPalettedRasterRenderer::colors() const
@@ -29,7 +29,7 @@ class QgsPalettedRasterRenderer: public QgsRasterRenderer
/**Renderer owns color array*/
QgsPalettedRasterRenderer( QgsRasterDataProvider* provider, int bandNumber, QColor* colorArray, int nColors );
~QgsPalettedRasterRenderer();
static QgsRasterRenderer* create( const QDomElement& elem );
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterDataProvider* provider );

void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

@@ -24,6 +24,7 @@ email : tim at linfiniti.com
#include "qgsrasterbandstats.h"
#include "qgsrasterlayer.h"
#include "qgsrasterpyramid.h"
#include "qgsrasterrendererregistry.h"
#include "qgsrectangle.h"
#include "qgsrendercontext.h"
#include "qgscoordinatereferencesystem.h"
@@ -43,10 +44,6 @@ email : tim at linfiniti.com
#include "qgssinglebandpseudocolorrenderer.h"
#include "qgssinglebandgrayrenderer.h"

//resamplers
#include "qgsbilinearrasterresampler.h"
#include "qgscubicrasterresampler.h"

#include <cstdio>
#include <cmath>
#include <limits>
@@ -3180,6 +3177,21 @@ bool QgsRasterLayer::readSymbology( const QDomNode& layer_node, QString& errorMe
mRasterTransparency.setTransparentThreeValuePixelList( newThreeValuePixelList );
}

//raster renderer
delete mRenderer;
mRenderer = 0;
QDomElement rendererElem = layer_node.firstChildElement( "rasterrenderer" );
if ( !rendererElem.isNull() )
{
QString rendererType = rendererElem.attribute( "type" );
QgsRasterRendererRegistryEntry rendererEntry;
if ( QgsRasterRendererRegistry::instance()->rendererData( rendererType, rendererEntry ) )
{
mRenderer = rendererEntry.rendererCreateFunction( rendererElem, dataProvider() );
}
}

#if 0
/*
* Color Ramp tab
*/
@@ -3269,6 +3281,7 @@ bool QgsRasterLayer::readSymbology( const QDomNode& layer_node, QString& errorMe
}
mRenderer->setZoomedOutResampler( zoomedOutResampler );
}
#endif //0

return true;
} //readSymbology
@@ -21,6 +21,11 @@
#include "qgsrastertransparency.h"
#include "qgsrasterviewport.h"
#include "qgsmaptopixel.h"

//resamplers
#include "qgsbilinearrasterresampler.h"
#include "qgscubicrasterresampler.h"

#include <QDomDocument>
#include <QDomElement>
#include <QImage>
@@ -334,9 +339,47 @@ void QgsRasterRenderer::_writeXML( QDomDocument& doc, QDomElement& rasterRendere
rasterRendererElem.setAttribute( "zoomedOutResampler", mZoomedOutResampler->type() );
}

//todo: write raster transparency
if ( mRasterTransparency )
{
mRasterTransparency->writeXML( doc, rasterRendererElem );
}
}

void QgsRasterRenderer::readXML( const QDomElement& rendererElem )
{
if ( rendererElem.isNull() )
{
return;
}

mType = rendererElem.attribute( "type" );
mOpacity = rendererElem.attribute( "opacity", "1.0" ).toDouble();
mAlphaBand = rendererElem.attribute( "alphaBand", "-1" ).toInt();
mMaxOversampling = rendererElem.attribute( "maxOversampling", "2.0" ).toDouble();
mInvertColor = rendererElem.attribute( "invertColor", "0" ).toInt();

QString zoomedInResamplerType = rendererElem.attribute( "zoomedInResampler" );
if ( zoomedInResamplerType == "bilinear" )
{
mZoomedInResampler = new QgsBilinearRasterResampler();
}
else if ( zoomedInResamplerType == "cubic" )
{
mZoomedInResampler = new QgsCubicRasterResampler();
}

QString zoomedOutResamplerType = rendererElem.attribute( "zoomedOutResampler" );
if ( zoomedOutResamplerType == "bilinear" )
{
mZoomedOutResampler = new QgsBilinearRasterResampler();
}

//todo: read mRasterTransparency
QDomElement rasterTransparencyElem = rendererElem.firstChildElement( "rasterTransparency" );
if ( !rasterTransparencyElem.isNull() )
{
delete mRasterTransparency;
mRasterTransparency = new QgsRasterTransparency();
mRasterTransparency->readXML( rasterTransparencyElem );
}
}
@@ -78,6 +78,9 @@ class QgsRasterRenderer

virtual void writeXML( QDomDocument& doc, QDomElement& parentElem ) const = 0;

/**Sets base class members from xml. Usually called from create() methods of subclasses*/
void readXML( const QDomElement& rendererElem );

protected:
inline double readValue( void *data, QgsRasterDataProvider::DataType type, int index );

@@ -27,7 +27,7 @@ class QgsRasterLayer;
class QgsRasterRenderer;
class QgsRasterRendererWidget;

typedef QgsRasterRenderer*( *QgsRasterRendererCreateFunc )( const QDomElement& );
typedef QgsRasterRenderer*( *QgsRasterRendererCreateFunc )( const QDomElement&, QgsRasterDataProvider* provider );
typedef QgsRasterRendererWidget*( *QgsRasterRendererWidgetCreateFunc )( QgsRasterLayer* );

struct QgsRasterRendererRegistryEntry
@@ -217,3 +217,44 @@ void QgsRasterTransparency::writeXML( QDomDocument& doc, QDomElement& parentElem
}
parentElem.appendChild( rasterTransparencyElem );
}

void QgsRasterTransparency::readXML( const QDomElement& elem )
{
if ( elem.isNull() )
{
return;
}

mTransparentSingleValuePixelList.clear();
mTransparentThreeValuePixelList.clear();
QDomElement currentEntryElem;

QDomElement singlePixelListElem = elem.firstChildElement( "singleValuePixelList" );
if ( !singlePixelListElem.isNull() )
{
QDomNodeList entryList = singlePixelListElem.elementsByTagName( "pixelListEntry" );
TransparentSingleValuePixel sp;
for ( int i = 0; i < entryList.size(); ++i )
{
currentEntryElem = entryList.at( i ).toElement();
sp.percentTransparent = currentEntryElem.attribute( "percentTransparent" ).toDouble();
sp.pixelValue = currentEntryElem.attribute( "pixelValue" ).toDouble();
mTransparentSingleValuePixelList.append( sp );
}
}
QDomElement threeValuePixelListElem = elem.firstChildElement( "threeValuePixelList" );
if ( !threeValuePixelListElem.isNull() )
{
QDomNodeList entryList = threeValuePixelListElem.elementsByTagName( "pixelListEntry" );
TransparentThreeValuePixel tp;
for ( int i = 0; i < entryList.size(); ++i )
{
currentEntryElem = entryList.at( i ).toElement();
tp.red = currentEntryElem.attribute( "red" ).toDouble();
tp.green = currentEntryElem.attribute( "green" ).toDouble();
tp.blue = currentEntryElem.attribute( "blue" ).toDouble();
tp.percentTransparent = currentEntryElem.attribute( "percentTransparent" ).toDouble();
mTransparentThreeValuePixelList.append( tp );
}
}
}
@@ -81,6 +81,8 @@ class CORE_EXPORT QgsRasterTransparency

void writeXML( QDomDocument& doc, QDomElement& parentElem ) const;

void readXML( const QDomElement& elem );

private:
/** \brief The list to hold transparency values for RGB layers */
QList<QgsRasterTransparency::TransparentThreeValuePixel> mTransparentThreeValuePixelList;
@@ -31,7 +31,7 @@ QgsSingleBandColorDataRenderer::~QgsSingleBandColorDataRenderer()
{
}

QgsRasterRenderer* QgsSingleBandColorDataRenderer::create( const QDomElement& elem )
QgsRasterRenderer* QgsSingleBandColorDataRenderer::create( const QDomElement& elem, QgsRasterDataProvider* provider )
{
return 0;
}
@@ -28,7 +28,7 @@ class QgsSingleBandColorDataRenderer: public QgsRasterRenderer
QgsSingleBandColorDataRenderer( QgsRasterDataProvider* provider, int band );
~QgsSingleBandColorDataRenderer();

static QgsRasterRenderer* create( const QDomElement& elem );
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterDataProvider* provider );

virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

@@ -32,7 +32,7 @@ QgsSingleBandGrayRenderer::~QgsSingleBandGrayRenderer()
delete mContrastEnhancement;
}

QgsRasterRenderer* QgsSingleBandGrayRenderer::create( const QDomElement& elem )
QgsRasterRenderer* QgsSingleBandGrayRenderer::create( const QDomElement& elem, QgsRasterDataProvider* provider )
{
return 0;
}
@@ -29,7 +29,7 @@ class QgsSingleBandGrayRenderer: public QgsRasterRenderer
QgsSingleBandGrayRenderer( QgsRasterDataProvider* provider, int grayBand );
~QgsSingleBandGrayRenderer();

static QgsRasterRenderer* create( const QDomElement& elem );
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterDataProvider* provider );

virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

@@ -39,7 +39,7 @@ void QgsSingleBandPseudoColorRenderer::setShader( QgsRasterShader* shader )
mShader = shader;
}

QgsRasterRenderer* QgsSingleBandPseudoColorRenderer::create( const QDomElement& elem )
QgsRasterRenderer* QgsSingleBandPseudoColorRenderer::create( const QDomElement& elem, QgsRasterDataProvider* provider )
{
return 0;
}
@@ -158,7 +158,7 @@ void QgsSingleBandPseudoColorRenderer::writeXML( QDomDocument& doc, QDomElement&
QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
_writeXML( doc, rasterRendererElem );
rasterRendererElem.setAttribute( "band", mBand );
if( mShader )
if ( mShader )
{
mShader->writeXML( doc, rasterRendererElem ); //todo: include color ramp items directly in this renderer
}
@@ -30,7 +30,7 @@ class QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer
QgsSingleBandPseudoColorRenderer( QgsRasterDataProvider* provider, int band, QgsRasterShader* shader );
~QgsSingleBandPseudoColorRenderer();

static QgsRasterRenderer* create( const QDomElement& elem );
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterDataProvider* provider );

virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

0 comments on commit 7a43e45

Please sign in to comment.