21 #include <QDomDocument>
22 #include <QDomElement>
29 , mGrayBand( grayBand )
30 , mGradient( BlackToWhite )
31 , mContrastEnhancement( nullptr )
41 if ( mContrastEnhancement )
55 int grayBand = elem.attribute( QStringLiteral(
"grayBand" ), QStringLiteral(
"-1" ) ).toInt();
59 if ( elem.attribute( QStringLiteral(
"gradient" ) ) == QLatin1String(
"WhiteToBlack" ) )
64 QDomElement contrastEnhancementElem = elem.firstChildElement( QStringLiteral(
"contrastEnhancement" ) );
65 if ( !contrastEnhancementElem.isNull() )
69 ce->
readXml( contrastEnhancementElem );
77 mContrastEnhancement.reset( ce );
83 QgsDebugMsgLevel( QStringLiteral(
"width = %1 height = %2" ).arg( width ).arg( height ), 4 );
85 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
88 return outputBlock.release();
91 std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->
block( mGrayBand,
extent, width, height, feedback ) );
92 if ( !inputBlock || inputBlock->isEmpty() )
95 return outputBlock.release();
98 std::shared_ptr< QgsRasterBlock > alphaBlock;
103 if ( !alphaBlock || alphaBlock->isEmpty() )
106 return outputBlock.release();
111 alphaBlock = inputBlock;
116 return outputBlock.release();
120 bool isNoData =
false;
123 double grayVal = inputBlock->valueAndNoData( i, isNoData );
127 outputBlock->setColor( i, myDefaultColor );
138 currentAlpha *= alphaBlock->value( i ) / 255.0;
141 if ( mContrastEnhancement )
143 if ( !mContrastEnhancement->isValueInDisplayableRange( grayVal ) )
145 outputBlock->setColor( i, myDefaultColor );
148 grayVal = mContrastEnhancement->enhanceContrast( grayVal );
153 grayVal = 255 - grayVal;
158 outputBlock->setColor( i, qRgba( grayVal, grayVal, grayVal, 255 ) );
162 outputBlock->setColor( i, qRgba( currentAlpha * grayVal, currentAlpha * grayVal, currentAlpha * grayVal, currentAlpha * 255 ) );
166 return outputBlock.release();
171 if ( parentElem.isNull() )
176 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
179 rasterRendererElem.setAttribute( QStringLiteral(
"grayBand" ), mGrayBand );
184 gradient = QStringLiteral(
"BlackToWhite" );
188 gradient = QStringLiteral(
"WhiteToBlack" );
190 rasterRendererElem.setAttribute( QStringLiteral(
"gradient" ),
gradient );
192 if ( mContrastEnhancement )
194 QDomElement contrastElem = doc.createElement( QStringLiteral(
"contrastEnhancement" ) );
195 mContrastEnhancement->writeXml( doc, contrastElem );
196 rasterRendererElem.appendChild( contrastElem );
198 parentElem.appendChild( rasterRendererElem );
205 QColor minColor = ( mGradient ==
BlackToWhite ) ? Qt::black : Qt::white;
206 QColor maxColor = ( mGradient ==
BlackToWhite ) ? Qt::white : Qt::black;
207 symbolItems.push_back( qMakePair( QString::number( mContrastEnhancement->minimumValue() ), minColor ) );
208 symbolItems.push_back( qMakePair( QString::number( mContrastEnhancement->maximumValue() ), maxColor ) );
215 if ( mGrayBand != -1 )
217 bandList << mGrayBand;
228 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
229 if ( elements.size() == 0 )
233 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
238 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
239 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Opacity" ) );
240 if ( elements.size() != 0 )
242 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
246 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Geometry" ) );
247 if ( elements.size() != 0 )
249 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
253 rasterSymbolizerElem.insertBefore( channelSelectionElem, rasterSymbolizerElem.firstChild() );
258 QDomElement channelElem = doc.createElement( QStringLiteral(
"sld:GrayChannel" ) );
259 channelSelectionElem.appendChild( channelElem );
262 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
263 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number(
grayBand() ) ) );
264 channelElem.appendChild( sourceChannelNameElem );
269 QDomElement contrastEnhancementElem = doc.createElement( QStringLiteral(
"sld:ContrastEnhancement" ) );
270 lContrastEnhancement->toSld( doc, contrastEnhancementElem );
276 switch ( lContrastEnhancement->contrastEnhancementAlgorithm() )
288 const QDomNodeList vendorOptions = contrastEnhancementElem.elementsByTagName( QStringLiteral(
"sld:VendorOption" ) );
289 for (
int i = 0; i < vendorOptions.size(); ++i )
291 QDomElement vendorOption = vendorOptions.at( i ).toElement();
292 if ( vendorOption.attribute( QStringLiteral(
"name" ) ) != QLatin1String(
"minValue" ) )
296 vendorOption.removeChild( vendorOption.firstChild() );
297 vendorOption.appendChild( doc.createTextNode( QString::number( myRasterBandStats.
minimumValue ) ) );
310 channelElem.appendChild( contrastEnhancementElem );
315 QList< QPair< QString, QColor > > classes;
319 QDomElement colorMapElem = doc.createElement( QStringLiteral(
"sld:ColorMap" ) );
320 rasterSymbolizerElem.appendChild( colorMapElem );
329 QList< QPair< QString, QColor > > colorMapping( classes );
335 QString lowValue = classes[0].first;
336 QColor lowColor = classes[0].second;
337 lowColor.setAlpha( 0 );
338 QString highValue = classes[1].first;
339 QColor highColor = classes[1].second;
340 highColor.setAlpha( 0 );
342 colorMapping.prepend( QPair< QString, QColor >( lowValue, lowColor ) );
343 colorMapping.append( QPair< QString, QColor >( highValue, highColor ) );
348 colorMapping[0].first = QStringLiteral(
"0" );
349 colorMapping[1].first = QStringLiteral(
"255" );
359 for (
auto it = colorMapping.constBegin(); it != colorMapping.constEnd() ; ++it )
362 QDomElement lowColorMapEntryElem = doc.createElement( QStringLiteral(
"sld:ColorMapEntry" ) );
363 colorMapElem.appendChild( lowColorMapEntryElem );
364 lowColorMapEntryElem.setAttribute( QStringLiteral(
"color" ), it->second.name() );
365 lowColorMapEntryElem.setAttribute( QStringLiteral(
"quantity" ), it->first );
366 if ( it->second.alphaF() == 0.0 )
368 lowColorMapEntryElem.setAttribute( QStringLiteral(
"opacity" ), QString::number( it->second.alpha() ) );