Skip to content

Commit

Permalink
Use project file transformation for raster symbology also when loadin…
Browse files Browse the repository at this point in the history
…g from qml
  • Loading branch information
mhugent committed Apr 18, 2012
1 parent 5dd3ede commit 61ceec5
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 601 deletions.
282 changes: 144 additions & 138 deletions src/core/qgsprojectfiletransform.cpp
Expand Up @@ -466,173 +466,176 @@ void QgsProjectFileTransform::transform1800to1900()
for ( int i = 0; i < layerItemList.size(); ++i )
{
QDomElement rasterPropertiesElem = layerItemList.at( i ).toElement();
QDomElement rasterRendererElem = mDom.createElement( "rasterrenderer" );

//create raster layer (necessary to convert band name to band number
QDomNode layerNode = rasterPropertiesElem.parentNode();
QDomElement dataSourceElem = layerNode.firstChildElement( "datasource" );
QDomElement layerNameElem = layerNode.firstChildElement( "layername" );
QgsRasterLayer rasterLayer( QgsProject::instance()->readPath( dataSourceElem.text() ), layerNameElem.text() );
convertRasterProperties( mDom, layerNode, rasterPropertiesElem, &rasterLayer );
}
QgsDebugMsg( mDom.toString() );
}

//convert general properties
void QgsProjectFileTransform::convertRasterProperties( QDomDocument& doc, QDomNode& parentNode,
QDomElement& rasterPropertiesElem, QgsRasterLayer* rlayer )
{
QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
//convert general properties

//invert color
rasterRendererElem.setAttribute( "invertColor", "0" );
QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( "mInvertColor" );
if ( !invertColorElem.isNull() )
//invert color
rasterRendererElem.setAttribute( "invertColor", "0" );
QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( "mInvertColor" );
if ( !invertColorElem.isNull() )
{
if ( invertColorElem.text() == "true" )
{
if ( invertColorElem.text() == "true" )
{
rasterRendererElem.setAttribute( "invertColor", "1" );
}
rasterRendererElem.setAttribute( "invertColor", "1" );
}
}

//opacity
rasterRendererElem.setAttribute( "opacity", "1" );
QDomElement transparencyElem = layerNode.firstChildElement( "transparencyLevelInt" );
if ( !transparencyElem.isNull() )
{
double transparency = transparencyElem.text().toInt();
rasterRendererElem.setAttribute( "opacity", transparency / 255.0 );
}
//opacity
rasterRendererElem.setAttribute( "opacity", "1" );
QDomElement transparencyElem = parentNode.firstChildElement( "transparencyLevelInt" );
if ( !transparencyElem.isNull() )
{
double transparency = transparencyElem.text().toInt();
rasterRendererElem.setAttribute( "opacity", transparency / 255.0 );
}

//alphaBand was not saved until now (bug)
rasterRendererElem.setAttribute( "alphaBand", -1 );
//alphaBand was not saved until now (bug)
rasterRendererElem.setAttribute( "alphaBand", -1 );

//gray band is used for several renderers
int grayBand = rasterBandNumber( rasterPropertiesElem, "mGrayBandName", &rasterLayer );
//gray band is used for several renderers
int grayBand = rasterBandNumber( rasterPropertiesElem, "mGrayBandName", rlayer );

//convert renderer specific properties
QString drawingStyle = rasterPropertiesElem.firstChildElement( "mDrawingStyle" ).text();
if ( drawingStyle == "SingleBandGray" )
{
rasterRendererElem.setAttribute( "type", "singlebandgray" );
rasterRendererElem.setAttribute( "grayBand", grayBand );
transformContrastEnhancement( mDom, rasterPropertiesElem, rasterRendererElem );
}
else if ( drawingStyle == "SingleBandPseudoColor" )
//convert renderer specific properties
QString drawingStyle = rasterPropertiesElem.firstChildElement( "mDrawingStyle" ).text();
if ( drawingStyle == "SingleBandGray" )
{
rasterRendererElem.setAttribute( "type", "singlebandgray" );
rasterRendererElem.setAttribute( "grayBand", grayBand );
transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
}
else if ( drawingStyle == "SingleBandPseudoColor" )
{
rasterRendererElem.setAttribute( "type", "singlebandpseudocolor" );
rasterRendererElem.setAttribute( "band", grayBand );
QDomElement newRasterShaderElem = doc.createElement( "rastershader" );
QDomElement newColorRampShaderElem = doc.createElement( "colorrampshader" );
newRasterShaderElem.appendChild( newColorRampShaderElem );
rasterRendererElem.appendChild( newRasterShaderElem );

//switch depending on mColorShadingAlgorithm
QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( "mColorShadingAlgorithm" ).text();
if ( colorShadingAlgorithm == "PseudoColorShader" || colorShadingAlgorithm == "FreakOutShader" )
{
rasterRendererElem.setAttribute( "type", "singlebandpseudocolor" );
rasterRendererElem.setAttribute( "band", grayBand );
QDomElement newRasterShaderElem = mDom.createElement( "rastershader" );
QDomElement newColorRampShaderElem = mDom.createElement( "colorrampshader" );
newRasterShaderElem.appendChild( newColorRampShaderElem );
rasterRendererElem.appendChild( newRasterShaderElem );

//switch depending on mColorShadingAlgorithm
QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( "mColorShadingAlgorithm" ).text();
if ( colorShadingAlgorithm == "PseudoColorShader" || colorShadingAlgorithm == "FreakOutShader" )
{
newColorRampShaderElem.setAttribute( "colorRampType", "INTERPOLATED" );
newColorRampShaderElem.setAttribute( "colorRampType", "INTERPOLATED" );

//get minmax from rasterlayer
QgsRasterBandStats rasterBandStats = rasterLayer.bandStatistics( grayBand );
double minValue = rasterBandStats.minimumValue;
double maxValue = rasterBandStats.maximumValue;
double breakSize = ( maxValue - minValue ) / 3;
//get minmax from rasterlayer
QgsRasterBandStats rasterBandStats = rlayer->bandStatistics( grayBand );
double minValue = rasterBandStats.minimumValue;
double maxValue = rasterBandStats.maximumValue;
double breakSize = ( maxValue - minValue ) / 3;

QStringList colorList;
if ( colorShadingAlgorithm == "FreakOutShader" )
{
colorList << "#ff00ff" << "#00ffff" << "#ff0000" << "#00ff00";
}
else //pseudocolor
{
colorList << "#0000ff" << "#00ffff" << "#ffff00" << "#ff0000";
}
QStringList::const_iterator colorIt = colorList.constBegin();
double boundValue = minValue;
for ( ; colorIt != colorList.constEnd(); ++colorIt )
{
QDomElement newItemElem = mDom.createElement( "item" );
newItemElem.setAttribute( "value", QString::number( boundValue ) );
newItemElem.setAttribute( "label", QString::number( boundValue ) );
newItemElem.setAttribute( "color", *colorIt );
newColorRampShaderElem.appendChild( newItemElem );
boundValue += breakSize;
}
QStringList colorList;
if ( colorShadingAlgorithm == "FreakOutShader" )
{
colorList << "#ff00ff" << "#00ffff" << "#ff0000" << "#00ff00";
}
else if ( colorShadingAlgorithm == "ColorRampShader" )
else //pseudocolor
{
QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" );
QString type = customColorRampElem.firstChildElement( "colorRampType" ).text();
newColorRampShaderElem.setAttribute( "colorRampType", type );
QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( "colorRampEntry" );

QString value, label;
QColor newColor;
int red, green, blue;
QDomElement currentItemElem;
for ( int i = 0; i < colorNodeList.size(); ++i )
{
currentItemElem = colorNodeList.at( i ).toElement();
value = currentItemElem.attribute( "value" );
label = currentItemElem.attribute( "label" );
red = currentItemElem.attribute( "red" ).toInt();
green = currentItemElem.attribute( "green" ).toInt();
blue = currentItemElem.attribute( "blue" ).toInt();
newColor = QColor( red, green, blue );
QDomElement newItemElem = mDom.createElement( "item" );
newItemElem.setAttribute( "value", value );
newItemElem.setAttribute( "label", label );
newItemElem.setAttribute( "color", newColor.name() );
newColorRampShaderElem.appendChild( newItemElem );
}
colorList << "#0000ff" << "#00ffff" << "#ffff00" << "#ff0000";
}
QStringList::const_iterator colorIt = colorList.constBegin();
double boundValue = minValue;
for ( ; colorIt != colorList.constEnd(); ++colorIt )
{
QDomElement newItemElem = doc.createElement( "item" );
newItemElem.setAttribute( "value", QString::number( boundValue ) );
newItemElem.setAttribute( "label", QString::number( boundValue ) );
newItemElem.setAttribute( "color", *colorIt );
newColorRampShaderElem.appendChild( newItemElem );
boundValue += breakSize;
}
}
else if ( drawingStyle == "PalettedColor" )
else if ( colorShadingAlgorithm == "ColorRampShader" )
{
rasterRendererElem.setAttribute( "type", "paletted" );
rasterRendererElem.setAttribute( "band", grayBand );
QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" );
QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( "colorRampEntry" );
QDomElement newColorPaletteElem = mDom.createElement( "colorPalette" );

int red = 0;
int green = 0;
int blue = 0;
int value = 0;
QDomElement colorRampEntryElem;
for ( int i = 0; i < colorRampEntryList.size(); ++i )
QString type = customColorRampElem.firstChildElement( "colorRampType" ).text();
newColorRampShaderElem.setAttribute( "colorRampType", type );
QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( "colorRampEntry" );

QString value, label;
QColor newColor;
int red, green, blue;
QDomElement currentItemElem;
for ( int i = 0; i < colorNodeList.size(); ++i )
{
colorRampEntryElem = colorRampEntryList.at( i ).toElement();
QDomElement newPaletteElem = mDom.createElement( "paletteEntry" );
value = ( int )( colorRampEntryElem.attribute( "value" ).toDouble() );
newPaletteElem.setAttribute( "value", value );
red = colorRampEntryElem.attribute( "red" ).toInt();
green = colorRampEntryElem.attribute( "green" ).toInt();
blue = colorRampEntryElem.attribute( "blue" ).toInt();
newPaletteElem.setAttribute( "color", QColor( red, green, blue ).name() );
newColorPaletteElem.appendChild( newPaletteElem );
currentItemElem = colorNodeList.at( i ).toElement();
value = currentItemElem.attribute( "value" );
label = currentItemElem.attribute( "label" );
red = currentItemElem.attribute( "red" ).toInt();
green = currentItemElem.attribute( "green" ).toInt();
blue = currentItemElem.attribute( "blue" ).toInt();
newColor = QColor( red, green, blue );
QDomElement newItemElem = doc.createElement( "item" );
newItemElem.setAttribute( "value", value );
newItemElem.setAttribute( "label", label );
newItemElem.setAttribute( "color", newColor.name() );
newColorRampShaderElem.appendChild( newItemElem );
}
rasterRendererElem.appendChild( newColorPaletteElem );
}
else if ( drawingStyle == "MultiBandColor" )
}
else if ( drawingStyle == "PalettedColor" )
{
rasterRendererElem.setAttribute( "type", "paletted" );
rasterRendererElem.setAttribute( "band", grayBand );
QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" );
QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( "colorRampEntry" );
QDomElement newColorPaletteElem = doc.createElement( "colorPalette" );

int red = 0;
int green = 0;
int blue = 0;
int value = 0;
QDomElement colorRampEntryElem;
for ( int i = 0; i < colorRampEntryList.size(); ++i )
{
rasterRendererElem.setAttribute( "type", "multibandcolor" );
colorRampEntryElem = colorRampEntryList.at( i ).toElement();
QDomElement newPaletteElem = doc.createElement( "paletteEntry" );
value = ( int )( colorRampEntryElem.attribute( "value" ).toDouble() );
newPaletteElem.setAttribute( "value", value );
red = colorRampEntryElem.attribute( "red" ).toInt();
green = colorRampEntryElem.attribute( "green" ).toInt();
blue = colorRampEntryElem.attribute( "blue" ).toInt();
newPaletteElem.setAttribute( "color", QColor( red, green, blue ).name() );
newColorPaletteElem.appendChild( newPaletteElem );
}
rasterRendererElem.appendChild( newColorPaletteElem );
}
else if ( drawingStyle == "MultiBandColor" )
{
rasterRendererElem.setAttribute( "type", "multibandcolor" );

//red band, green band, blue band
int redBand = rasterBandNumber( rasterPropertiesElem, "mRedBandName", &rasterLayer );
int greenBand = rasterBandNumber( rasterPropertiesElem, "mGreenBandName", &rasterLayer );
int blueBand = rasterBandNumber( rasterPropertiesElem, "mBlueBandName", &rasterLayer );
rasterRendererElem.setAttribute( "redBand", redBand );
rasterRendererElem.setAttribute( "greenBand", greenBand );
rasterRendererElem.setAttribute( "blueBand", blueBand );
//red band, green band, blue band
int redBand = rasterBandNumber( rasterPropertiesElem, "mRedBandName", rlayer );
int greenBand = rasterBandNumber( rasterPropertiesElem, "mGreenBandName", rlayer );
int blueBand = rasterBandNumber( rasterPropertiesElem, "mBlueBandName", rlayer );
rasterRendererElem.setAttribute( "redBand", redBand );
rasterRendererElem.setAttribute( "greenBand", greenBand );
rasterRendererElem.setAttribute( "blueBand", blueBand );

transformContrastEnhancement( mDom, rasterPropertiesElem, rasterRendererElem );
}
else
{
return;
}
transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
}
else
{
return;
}

//replace rasterproperties element with rasterrenderer element
if ( !layerNode.isNull() )
{
layerNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
}
//replace rasterproperties element with rasterrenderer element
if ( !parentNode.isNull() )
{
parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
}
QgsDebugMsg( mDom.toString() );
}

int QgsProjectFileTransform::rasterBandNumber( const QDomElement& rasterPropertiesElem, const QString bandName,
Expand Down Expand Up @@ -752,5 +755,8 @@ void QgsProjectFileTransform::transformContrastEnhancement( QDomDocument& doc, c
void QgsProjectFileTransform::transformRasterTransparency( QDomDocument& doc, const QDomElement& orig, QDomElement& rendererElem )
{
//soon...
Q_UNUSED( doc );
Q_UNUSED( orig );
Q_UNUSED( rendererElem );
}

7 changes: 4 additions & 3 deletions src/core/qgsprojectfiletransform.h
Expand Up @@ -58,6 +58,7 @@ class QgsProjectFileTransform
*/
void dump();

static void convertRasterProperties( QDomDocument& doc, QDomNode& parentNode, QDomElement& rasterPropertiesElem, QgsRasterLayer* rlayer );

private:

Expand Down Expand Up @@ -86,9 +87,9 @@ class QgsProjectFileTransform
void transform1800to1900();

//helper functions
int rasterBandNumber( const QDomElement& rasterPropertiesElem, const QString bandName, QgsRasterLayer* rlayer );
void transformContrastEnhancement( QDomDocument& doc, const QDomElement& rasterproperties, QDomElement& rendererElem );
void transformRasterTransparency( QDomDocument& doc, const QDomElement& orig, QDomElement& rendererElem );
static int rasterBandNumber( const QDomElement& rasterPropertiesElem, const QString bandName, QgsRasterLayer* rlayer );
static void transformContrastEnhancement( QDomDocument& doc, const QDomElement& rasterproperties, QDomElement& rendererElem );
static void transformRasterTransparency( QDomDocument& doc, const QDomElement& orig, QDomElement& rendererElem );
};


Expand Down

0 comments on commit 61ceec5

Please sign in to comment.