void QgsProjectFileTransform::convertRasterProperties( QDomDocument& doc, QDomNode& parentNode, QDomElement& rasterPropertiesElem, QgsRasterLayer* rlayer ) { //no data //TODO: We would need to set no data on all bands, but we don't know number of bands here QDomNode noDataNode = rasterPropertiesElem.namedItem( "mNoDataValue" ); QDomElement noDataElement = noDataNode.toElement(); if ( !noDataElement.text().isEmpty() ) { QgsDebugMsg( "mNoDataValue = " + noDataElement.text() ); QDomElement noDataElem = doc.createElement( "noData" ); QDomElement noDataRangeList = doc.createElement( "noDataRangeList" ); noDataRangeList.setAttribute( "bandNo", 1 ); QDomElement noDataRange = doc.createElement( "noDataRange" ); noDataRange.setAttribute( "min", noDataElement.text() ); noDataRange.setAttribute( "max", noDataElement.text() ); noDataRangeList.appendChild( noDataRange ); noDataElem.appendChild( noDataRangeList ); parentNode.appendChild( noDataElem ); } QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" ); //convert general properties //invert color rasterRendererElem.setAttribute( "invertColor", "0" ); QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( "mInvertColor" ); if ( !invertColorElem.isNull() ) { if ( invertColorElem.text() == "true" ) { rasterRendererElem.setAttribute( "invertColor", "1" ); } } //opacity rasterRendererElem.setAttribute( "opacity", "1" ); QDomElement transparencyElem = parentNode.firstChildElement( "transparencyLevelInt" ); if ( !transparencyElem.isNull() ) { double transparency = transparencyElem.text().toInt(); rasterRendererElem.setAttribute( "opacity", QString::number( transparency / 255.0 ) ); } //alphaBand was not saved until now (bug) rasterRendererElem.setAttribute( "alphaBand", -1 ); //gray band is used for several renderers int grayBand = rasterBandNumber( rasterPropertiesElem, "mGrayBandName", rlayer ); //convert renderer specific properties QString drawingStyle = rasterPropertiesElem.firstChildElement( "mDrawingStyle" ).text(); // While PalettedColor should normaly contain only integer values, usually // color palette 0-255, it may happen (Tim, issue #7023) that it contains // colormap classification with double values and text labels // (which should normaly only appear in SingleBandPseudoColor drawingStyle) // => we have to check first the values and change drawingStyle if necessary if ( drawingStyle == "PalettedColor" ) { QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" ); QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( "colorRampEntry" ); for ( int i = 0; i < colorRampEntryList.size(); ++i ) { QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement(); QString strValue = colorRampEntryElem.attribute( "value" ); double value = strValue.toDouble(); if ( value < 0 || value > 10000 || value != ( int )value ) { QgsDebugMsg( QString( "forcing SingleBandPseudoColor value = %1" ).arg( value ) ); drawingStyle = "SingleBandPseudoColor"; break; } } } 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" ) { newColorRampShaderElem.setAttribute( "colorRampType", "INTERPOLATED" ); //get minmax from rasterlayer QgsRasterBandStats rasterBandStats = rlayer->dataProvider()->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 = 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 ( colorShadingAlgorithm == "ColorRampShader" ) { 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 = doc.createElement( "item" ); newItemElem.setAttribute( "value", value ); newItemElem.setAttribute( "label", label ); newItemElem.setAttribute( "color", newColor.name() ); newColorRampShaderElem.appendChild( newItemElem ); } } } 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 ) { 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", 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( doc, rasterPropertiesElem, rasterRendererElem ); } else { return; } //replace rasterproperties element with rasterrenderer element if ( !parentNode.isNull() ) { parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem ); } }
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() ) { if ( invertColorElem.text() == "true" ) { rasterRendererElem.setAttribute( "invertColor", "1" ); } } //opacity rasterRendererElem.setAttribute( "opacity", "1" ); QDomElement transparencyElem = parentNode.firstChildElement( "transparencyLevelInt" ); if ( !transparencyElem.isNull() ) { double transparency = transparencyElem.text().toInt(); rasterRendererElem.setAttribute( "opacity", QString::number( transparency / 255.0 ) ); } //alphaBand was not saved until now (bug) rasterRendererElem.setAttribute( "alphaBand", -1 ); //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( 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" ) { newColorRampShaderElem.setAttribute( "colorRampType", "INTERPOLATED" ); //get minmax from rasterlayer QgsRasterBandStats rasterBandStats = rlayer->dataProvider()->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 = 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 ( colorShadingAlgorithm == "ColorRampShader" ) { 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 = doc.createElement( "item" ); newItemElem.setAttribute( "value", value ); newItemElem.setAttribute( "label", label ); newItemElem.setAttribute( "color", newColor.name() ); newColorRampShaderElem.appendChild( newItemElem ); } } } 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 ) { 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", 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( doc, rasterPropertiesElem, rasterRendererElem ); } else { return; } //replace rasterproperties element with rasterrenderer element if ( !parentNode.isNull() ) { parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem ); } }