Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add writeXML method for renderers (needs still to be implemented for …
…most types)
  • Loading branch information
mhugent committed Mar 26, 2012
1 parent 99c7a81 commit 1e2215a
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 95 deletions.
5 changes: 5 additions & 0 deletions src/core/raster/qgsmultibandcolorrenderer.cpp
Expand Up @@ -273,3 +273,8 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
stopRasterRead( *bandIt );
}
}

void QgsMultiBandColorRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
{
//soon...
}
2 changes: 2 additions & 0 deletions src/core/raster/qgsmultibandcolorrenderer.h
Expand Up @@ -55,6 +55,8 @@ class QgsMultiBandColorRenderer: public QgsRasterRenderer
/**Takes ownership*/
void setBlueContrastEnhancement( QgsContrastEnhancement* ce );

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

private:
int mRedBand;
int mGreenBand;
Expand Down
21 changes: 21 additions & 0 deletions src/core/raster/qgspalettedrasterrenderer.cpp
Expand Up @@ -19,6 +19,8 @@
#include "qgsrastertransparency.h"
#include "qgsrasterviewport.h"
#include <QColor>
#include <QDomDocument>
#include <QDomElement>
#include <QImage>

QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterDataProvider* provider, int bandNumber,
Expand Down Expand Up @@ -155,3 +157,22 @@ void QgsPalettedRasterRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
stopRasterRead( mAlphaBand );
}
}

void QgsPalettedRasterRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
{
QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
_writeXML( doc, rasterRendererElem );

rasterRendererElem.setAttribute( "band", mBandNumber );
QDomElement colorPaletteElem = doc.createElement( "colorPalette" );
for ( int i = 0; i < mNColors; ++i )
{
QDomElement colorElem = doc.createElement( "paletteEntry" );
colorElem.setAttribute( "value", i );
colorElem.setAttribute( "color", mColors[i].name() );
colorPaletteElem.appendChild( colorElem );
}
rasterRendererElem.appendChild( colorPaletteElem );

parentElem.appendChild( rasterRendererElem );
}
2 changes: 2 additions & 0 deletions src/core/raster/qgspalettedrasterrenderer.h
Expand Up @@ -38,6 +38,8 @@ class QgsPalettedRasterRenderer: public QgsRasterRenderer
/**Returns copy of color array (caller takes ownership)*/
QColor* colors() const;

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

private:
int mBandNumber;
/**Color array*/
Expand Down
102 changes: 8 additions & 94 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -3400,38 +3400,6 @@ bool QgsRasterLayer::writeSymbology( QDomNode & layer_node, QDomDocument & docum
QDomElement rasterPropertiesElement = document.createElement( "rasterproperties" );
layer_node.appendChild( rasterPropertiesElement );

// resampler
if ( mRenderer )
{
//Max oversampling
QDomElement maxOversamplingElem = document.createElement( "MaxOversampling" );
QDomText maxOversamplingText = document.createTextNode( QString::number( mRenderer->maxOversampling() ) );
maxOversamplingElem.appendChild( maxOversamplingText );
rasterPropertiesElement.appendChild( maxOversamplingElem );

QString zoomedInResamplerString = "nearest";
const QgsRasterResampler* zoomedInResampler = mRenderer->zoomedInResampler();
if ( zoomedInResampler )
{
zoomedInResamplerString = zoomedInResampler->type();
}
QDomElement zoomedInResamplerElem = document.createElement( "zoomedInResampler" );
QDomText zoomedInResamplerText = document.createTextNode( zoomedInResamplerString );
zoomedInResamplerElem.appendChild( zoomedInResamplerText );
rasterPropertiesElement.appendChild( zoomedInResamplerElem );

QString zoomedOutResamplerString = "nearest";
const QgsRasterResampler* zoomedOutResampler = mRenderer->zoomedOutResampler();
if ( zoomedOutResampler )
{
zoomedOutResamplerString = zoomedOutResampler->type();
}
QDomElement zoomedOutResamplerElem = document.createElement( "zoomedOutResampler" );
QDomText zoomedOutResamplerText = document.createTextNode( zoomedOutResamplerString );
zoomedOutResamplerElem.appendChild( zoomedOutResamplerText );
rasterPropertiesElement.appendChild( zoomedOutResamplerElem );
}

QStringList sl = subLayers();
QStringList sls = mDataProvider->subLayerStyles();

Expand Down Expand Up @@ -3477,36 +3445,6 @@ bool QgsRasterLayer::writeSymbology( QDomNode & layer_node, QDomDocument & docum
rasterPropertiesElement.appendChild( formatElement );
}

// <mDrawingStyle>
QDomElement drawStyleElement = document.createElement( "mDrawingStyle" );
QDomText drawStyleText = document.createTextNode( drawingStyleAsString() );

drawStyleElement.appendChild( drawStyleText );

rasterPropertiesElement.appendChild( drawStyleElement );

// <colorShadingAlgorithm>
QDomElement colorShadingAlgorithmElement = document.createElement( "mColorShadingAlgorithm" );
QDomText colorShadingAlgorithmText = document.createTextNode( colorShadingAlgorithmAsString() );

colorShadingAlgorithmElement.appendChild( colorShadingAlgorithmText );

rasterPropertiesElement.appendChild( colorShadingAlgorithmElement );

// <mInvertColor>
QDomElement mInvertColorElement = document.createElement( "mInvertColor" );

if ( invertHistogram() )
{
mInvertColorElement.setAttribute( "boolean", "true" );
}
else
{
mInvertColorElement.setAttribute( "boolean", "false" );
}

rasterPropertiesElement.appendChild( mInvertColorElement );

// <mRedBandName>
QDomElement mRedBandNameElement = document.createElement( "mRedBandName" );
QString writtenRedBandName = redBandName();
Expand Down Expand Up @@ -3627,37 +3565,6 @@ bool QgsRasterLayer::writeSymbology( QDomNode & layer_node, QDomDocument & docum

rasterPropertiesElement.appendChild( GrayMinimumMaximumEstimated );

// <contrastEnhancementAlgorithm>
QDomElement contrastEnhancementAlgorithmElement = document.createElement( "mContrastEnhancementAlgorithm" );
QDomText contrastEnhancementAlgorithmText = document.createTextNode( contrastEnhancementAlgorithmAsString() );

contrastEnhancementAlgorithmElement.appendChild( contrastEnhancementAlgorithmText );

rasterPropertiesElement.appendChild( contrastEnhancementAlgorithmElement );

// <minMaxValues>
QList<QgsContrastEnhancement>::const_iterator it;
QDomElement contrastEnhancementMinMaxValuesElement = document.createElement( "contrastEnhancementMinMaxValues" );
for ( it = mContrastEnhancementList.constBegin(); it != mContrastEnhancementList.constEnd(); ++it )
{
QDomElement minMaxEntry = document.createElement( "minMaxEntry" );
QDomElement minEntry = document.createElement( "min" );
QDomElement maxEntry = document.createElement( "max" );

QDomText minEntryText = document.createTextNode( QString::number( it->minimumValue() ) );
minEntry.appendChild( minEntryText );

QDomText maxEntryText = document.createTextNode( QString::number( it->maximumValue() ) );
maxEntry.appendChild( maxEntryText );

minMaxEntry.appendChild( minEntry );
minMaxEntry.appendChild( maxEntry );

contrastEnhancementMinMaxValuesElement.appendChild( minMaxEntry );
}

rasterPropertiesElement.appendChild( contrastEnhancementMinMaxValuesElement );

/*
* Transparency tab
*/
Expand All @@ -3677,7 +3584,7 @@ bool QgsRasterLayer::writeSymbology( QDomNode & layer_node, QDomDocument & docum

rasterPropertiesElement.appendChild( mNoDataValueElement );


#if 0
if ( mRasterTransparency.transparentSingleValuePixelList().count() > 0 )
{
QDomElement singleValuePixelListElement = document.createElement( "singleValuePixelList" );
Expand Down Expand Up @@ -3744,6 +3651,13 @@ bool QgsRasterLayer::writeSymbology( QDomNode & layer_node, QDomDocument & docum

rasterPropertiesElement.appendChild( customColorRampElement );
}
#endif //0

if ( mRenderer )
{
QDomElement layerElem = layer_node.toElement();
mRenderer->writeXML( document, layerElem );
}

return true;
} // bool QgsRasterLayer::writeSymbology
Expand Down
30 changes: 30 additions & 0 deletions src/core/raster/qgsrasterrenderer.cpp
Expand Up @@ -21,6 +21,8 @@
#include "qgsrastertransparency.h"
#include "qgsrasterviewport.h"
#include "qgsmaptopixel.h"
#include <QDomDocument>
#include <QDomElement>
#include <QImage>
#include <QPainter>

Expand Down Expand Up @@ -310,3 +312,31 @@ void QgsRasterRenderer::projectImage( const QImage& srcImg, QImage& dstImage, Qg
}
}
}

void QgsRasterRenderer::_writeXML( QDomDocument& doc, QDomElement& rasterRendererElem ) const
{
if ( rasterRendererElem.isNull() )
{
return;
}

rasterRendererElem.setAttribute( "type", mType );
rasterRendererElem.setAttribute( "opacity", mOpacity );
rasterRendererElem.setAttribute( "alphaBand", mAlphaBand );
rasterRendererElem.setAttribute( "maxOversampling", mMaxOversampling );
rasterRendererElem.setAttribute( "invertColor", mInvertColor );
if ( mZoomedInResampler )
{
rasterRendererElem.setAttribute( "zoomedInResampler", mZoomedInResampler->type() );
}
if ( mZoomedOutResampler )
{
rasterRendererElem.setAttribute( "zoomedOutResampler", mZoomedOutResampler->type() );
}

//todo: write raster transparency
if ( mRasterTransparency )
{
mRasterTransparency->writeXML( doc, rasterRendererElem );
}
}
7 changes: 7 additions & 0 deletions src/core/raster/qgsrasterrenderer.h
Expand Up @@ -27,6 +27,8 @@ class QgsRasterProjector;
class QgsRasterTransparency;
class QgsRasterViewPort;

class QDomElement;

class QgsRasterRenderer
{
public:
Expand Down Expand Up @@ -74,6 +76,8 @@ class QgsRasterRenderer
void setMaxOversampling( double os ) { mMaxOversampling = os; }
double maxOversampling() const { return mMaxOversampling; }

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

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

Expand All @@ -97,6 +101,9 @@ class QgsRasterRenderer
int nCols, int nRows, double oversamplingX, double oversamplingY ) const;
void stopRasterRead( int bandNumber );

/**Write upper class info into <rasterrenderer> element (called by writeXML method of subclasses)*/
void _writeXML( QDomDocument& doc, QDomElement& rasterRendererElem ) const;


QgsRasterDataProvider* mProvider;
QString mType;
Expand Down
39 changes: 38 additions & 1 deletion src/core/raster/qgsrastertransparency.cpp
Expand Up @@ -15,11 +15,13 @@ email : ersts@amnh.org
* (at your option) any later version. *
* *
***************************************************************************/
#include <QList>

#include "qgsrastertransparency.h"
#include "qgis.h"

#include <QDomDocument>
#include <QDomElement>

QgsRasterTransparency::QgsRasterTransparency()
{

Expand Down Expand Up @@ -180,3 +182,38 @@ bool QgsRasterTransparency::isEmpty( double nodataValue ) const
( mTransparentThreeValuePixelList.size() < 4 && doubleNear( mTransparentThreeValuePixelList.at( 0 ).red, nodataValue ) &&
doubleNear( mTransparentThreeValuePixelList.at( 0 ).green, nodataValue ) && doubleNear( mTransparentThreeValuePixelList.at( 0 ).blue, nodataValue ) ) ) );
}

void QgsRasterTransparency::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
{
QDomElement rasterTransparencyElem = doc.createElement( "rasterTransparency" );
if ( mTransparentSingleValuePixelList.count() > 0 )
{
QDomElement singleValuePixelListElement = doc.createElement( "singleValuePixelList" );
QList<QgsRasterTransparency::TransparentSingleValuePixel>::const_iterator it = mTransparentSingleValuePixelList.constBegin();
for ( ; it != mTransparentSingleValuePixelList.constEnd(); ++it )
{
QDomElement pixelListElement = doc.createElement( "pixelListEntry" );
pixelListElement.setAttribute( "pixelValue", QString::number( it->pixelValue, 'f' ) );
pixelListElement.setAttribute( "percentTransparent", QString::number( it->percentTransparent ) );
singleValuePixelListElement.appendChild( pixelListElement );
}
rasterTransparencyElem.appendChild( singleValuePixelListElement );

}
if ( mTransparentThreeValuePixelList.count() > 0 )
{
QDomElement threeValuePixelListElement = doc.createElement( "threeValuePixelList" );
QList<QgsRasterTransparency::TransparentThreeValuePixel>::const_iterator it = mTransparentThreeValuePixelList.constBegin();
for ( ; it != mTransparentThreeValuePixelList.constEnd(); ++it )
{
QDomElement pixelListElement = doc.createElement( "pixelListEntry" );
pixelListElement.setAttribute( "red", QString::number( it->red, 'f' ) );
pixelListElement.setAttribute( "green", QString::number( it->green, 'f' ) );
pixelListElement.setAttribute( "blue", QString::number( it->blue, 'f' ) );
pixelListElement.setAttribute( "percentTransparent", QString::number( it->percentTransparent ) );
threeValuePixelListElement.appendChild( pixelListElement );
}
rasterTransparencyElem.appendChild( threeValuePixelListElement );
}
parentElem.appendChild( rasterTransparencyElem );
}
6 changes: 6 additions & 0 deletions src/core/raster/qgsrastertransparency.h
Expand Up @@ -18,6 +18,10 @@ email : ersts@amnh.org
#ifndef QGSRASTERTRANSPARENCY_H
#define QGSRASTERTRANSPARENCY_H

#include <QList>
class QDomDocument;
class QDomElement;

/** \ingroup core
* Defines the list of pixel values to be considered as transparent or semi
* transparent when rendering rasters.
Expand Down Expand Up @@ -75,6 +79,8 @@ class CORE_EXPORT QgsRasterTransparency
/**True if there are no entries in the pixel lists except the nodata value*/
bool isEmpty( double nodataValue ) const;

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

private:
/** \brief The list to hold transparency values for RGB layers */
QList<QgsRasterTransparency::TransparentThreeValuePixel> mTransparentThreeValuePixelList;
Expand Down
5 changes: 5 additions & 0 deletions src/core/raster/qgssinglebandcolordatarenderer.cpp
Expand Up @@ -87,3 +87,8 @@ void QgsSingleBandColorDataRenderer::draw( QPainter* p, QgsRasterViewPort* viewP

stopRasterRead( mBand );
}

void QgsSingleBandColorDataRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
{
//soon...
}
2 changes: 2 additions & 0 deletions src/core/raster/qgssinglebandcolordatarenderer.h
Expand Up @@ -32,6 +32,8 @@ class QgsSingleBandColorDataRenderer: public QgsRasterRenderer

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

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

private:
int mBand;
};
Expand Down
5 changes: 5 additions & 0 deletions src/core/raster/qgssinglebandgrayrenderer.cpp
Expand Up @@ -151,3 +151,8 @@ void QgsSingleBandGrayRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
stopRasterRead( mAlphaBand );
}
}

void QgsSingleBandGrayRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
{
//soon...
}
2 changes: 2 additions & 0 deletions src/core/raster/qgssinglebandgrayrenderer.h
Expand Up @@ -39,6 +39,8 @@ class QgsSingleBandGrayRenderer: public QgsRasterRenderer
/**Takes ownership*/
void setContrastEnhancement( QgsContrastEnhancement* ce );

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

private:
int mGrayBand;
QgsContrastEnhancement* mContrastEnhancement;
Expand Down
5 changes: 5 additions & 0 deletions src/core/raster/qgssinglebandpseudocolorrenderer.cpp
Expand Up @@ -145,3 +145,8 @@ void QgsSingleBandPseudoColorRenderer::draw( QPainter* p, QgsRasterViewPort* vie
stopRasterRead( mAlphaBand );
}
}

void QgsSingleBandPseudoColorRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
{
//soon...
}
2 changes: 2 additions & 0 deletions src/core/raster/qgssinglebandpseudocolorrenderer.h
Expand Up @@ -38,6 +38,8 @@ class QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer
void setShader( QgsRasterShader* shader );
QgsRasterShader* shader() { return mShader; }

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

private:
QgsRasterShader* mShader;
int mBand;
Expand Down

0 comments on commit 1e2215a

Please sign in to comment.