31#include <QDomDocument>
36using namespace Qt::StringLiterals;
41 , mContrastEnhancement( nullptr )
52 if ( mContrastEnhancement )
72 const int grayBand = elem.attribute( u
"grayBand"_s, u
"-1"_s ).toInt();
76 if ( elem.attribute( u
"gradient"_s ) ==
"WhiteToBlack"_L1 )
81 const QDomElement contrastEnhancementElem = elem.firstChildElement( u
"contrastEnhancement"_s );
82 if ( !contrastEnhancementElem.isNull() )
86 ce->
readXml( contrastEnhancementElem );
90 auto legendSettings = std::make_unique< QgsColorRampLegendNodeSettings >();
99 mContrastEnhancement.reset( ce );
105 QgsDebugMsgLevel( u
"width = %1 height = %2"_s.arg( width ).arg( height ), 4 );
107 auto outputBlock = std::make_unique<QgsRasterBlock>();
110 return outputBlock.release();
113 const std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->block( mGrayBand,
extent, width, height, feedback ) );
114 if ( !inputBlock || inputBlock->isEmpty() )
117 return outputBlock.release();
120 std::shared_ptr< QgsRasterBlock > alphaBlock;
125 if ( !alphaBlock || alphaBlock->isEmpty() )
128 return outputBlock.release();
133 alphaBlock = inputBlock;
138 return outputBlock.release();
142 bool isNoData =
false;
145 double grayVal = inputBlock->valueAndNoData( i, isNoData );
149 outputBlock->setColor( i, myDefaultColor );
160 const double alpha = alphaBlock->value( i );
163 outputBlock->setColor( i, myDefaultColor );
168 currentAlpha *= alpha / 255.0;
172 if ( mContrastEnhancement )
174 if ( !mContrastEnhancement->isValueInDisplayableRange( grayVal ) )
176 outputBlock->setColor( i, myDefaultColor );
179 grayVal = mContrastEnhancement->enhanceContrast( grayVal );
184 grayVal = 255 - grayVal;
189 outputBlock->setColor( i, qRgba( grayVal, grayVal, grayVal, 255 ) );
193 outputBlock->setColor( i, qRgba( currentAlpha * grayVal, currentAlpha * grayVal, currentAlpha * grayVal, currentAlpha * 255 ) );
197 return outputBlock.release();
217 else if ( band > 0 && band <= mInput->
bandCount() )
227 if ( parentElem.isNull() )
232 QDomElement rasterRendererElem = doc.createElement( u
"rasterrenderer"_s );
235 rasterRendererElem.setAttribute( u
"grayBand"_s, mGrayBand );
246 rasterRendererElem.setAttribute( u
"gradient"_s,
gradient );
248 if ( mContrastEnhancement )
250 QDomElement contrastElem = doc.createElement( u
"contrastEnhancement"_s );
251 mContrastEnhancement->writeXml( doc, contrastElem );
252 rasterRendererElem.appendChild( contrastElem );
255 if ( mLegendSettings )
258 parentElem.appendChild( rasterRendererElem );
263 QList<QPair<QString, QColor> > symbolItems;
266 const QColor minColor = ( mGradient ==
BlackToWhite ) ? Qt::black : Qt::white;
267 const QColor maxColor = ( mGradient ==
BlackToWhite ) ? Qt::white : Qt::black;
268 symbolItems.push_back( qMakePair( QString::number( mContrastEnhancement->minimumValue() ), minColor ) );
269 symbolItems.push_back( qMakePair( QString::number( mContrastEnhancement->maximumValue() ), maxColor ) );
276 QList<QgsLayerTreeModelLegendNode *> res;
280 if ( !name.isEmpty() )
285 const QColor minColor = ( mGradient ==
BlackToWhite ) ? Qt::black : Qt::white;
286 const QColor maxColor = ( mGradient ==
BlackToWhite ) ? Qt::white : Qt::black;
289 mContrastEnhancement->minimumValue(),
290 mContrastEnhancement->maximumValue() );
298 if ( mGrayBand != -1 )
300 bandList << mGrayBand;
309 toSld( doc, element, context );
318 QDomNodeList elements = element.elementsByTagName( u
"sld:RasterSymbolizer"_s );
319 if ( elements.size() == 0 )
323 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
328 QDomElement channelSelectionElem = doc.createElement( u
"sld:ChannelSelection"_s );
329 elements = rasterSymbolizerElem.elementsByTagName( u
"sld:Opacity"_s );
330 if ( elements.size() != 0 )
332 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
336 elements = rasterSymbolizerElem.elementsByTagName( u
"sld:Geometry"_s );
337 if ( elements.size() != 0 )
339 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
343 rasterSymbolizerElem.insertBefore( channelSelectionElem, rasterSymbolizerElem.firstChild() );
348 QDomElement channelElem = doc.createElement( u
"sld:GrayChannel"_s );
349 channelSelectionElem.appendChild( channelElem );
352 QDomElement sourceChannelNameElem = doc.createElement( u
"sld:SourceChannelName"_s );
353 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number( mGrayBand ) ) );
354 channelElem.appendChild( sourceChannelNameElem );
359 QDomElement contrastEnhancementElem = doc.createElement( u
"sld:ContrastEnhancement"_s );
360 lContrastEnhancement->toSld( doc, contrastEnhancementElem );
366 switch ( lContrastEnhancement->contrastEnhancementAlgorithm() )
378 const QDomNodeList vendorOptions = contrastEnhancementElem.elementsByTagName( u
"sld:VendorOption"_s );
379 for (
int i = 0; i < vendorOptions.size(); ++i )
381 QDomElement vendorOption = vendorOptions.at( i ).toElement();
382 if ( vendorOption.attribute( u
"name"_s ) !=
"minValue"_L1 )
386 vendorOption.removeChild( vendorOption.firstChild() );
387 vendorOption.appendChild( doc.createTextNode( QString::number( myRasterBandStats.
minimumValue ) ) );
400 channelElem.appendChild( contrastEnhancementElem );
408 QDomElement colorMapElem = doc.createElement( u
"sld:ColorMap"_s );
409 rasterSymbolizerElem.appendChild( colorMapElem );
418 QList< QPair< QString, QColor > > colorMapping( classes );
424 const QString lowValue = classes[0].first;
425 QColor lowColor = classes[0].second;
426 lowColor.setAlpha( 0 );
427 const QString highValue = classes[1].first;
428 QColor highColor = classes[1].second;
429 highColor.setAlpha( 0 );
431 colorMapping.prepend( QPair< QString, QColor >( lowValue, lowColor ) );
432 colorMapping.append( QPair< QString, QColor >( highValue, highColor ) );
437 colorMapping[0].first = u
"0"_s;
438 colorMapping[1].first = u
"255"_s;
448 for (
auto it = colorMapping.constBegin(); it != colorMapping.constEnd() ; ++it )
451 QDomElement lowColorMapEntryElem = doc.createElement( u
"sld:ColorMapEntry"_s );
452 colorMapElem.appendChild( lowColorMapEntryElem );
453 lowColorMapEntryElem.setAttribute( u
"color"_s, it->second.name() );
454 lowColorMapEntryElem.setAttribute( u
"quantity"_s, it->first );
455 if ( it->second.alphaF() == 0.0 )
457 lowColorMapEntryElem.setAttribute( u
"opacity"_s, QString::number( it->second.alpha() ) );
465 return mLegendSettings.get();
470 if ( settings == mLegendSettings.get() )
472 mLegendSettings.reset( settings );
482 bool refreshed =
false;
484 min.size() >= 1 && max.size() >= 1 )
487 mContrastEnhancement->setMinimumValue( min[0] );
488 mContrastEnhancement->setMaximumValue( max[0] );
QFlags< RasterRendererFlag > RasterRendererFlags
Flags which control behavior of raster renderers.
@ InternalLayerOpacityHandling
The renderer internally handles the raster layer's opacity, so the default layer level opacity handli...
DataType
Raster data types.
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Settings for a color ramp legend node.
A legend node which renders a color ramp.
Handles contrast enhancement and clipping.
@ StretchToMinimumMaximum
Linear histogram.
@ StretchAndClipToMinimumMaximum
@ NoEnhancement
Default color scaling algorithm, no scaling is applied.
void readXml(const QDomElement &elem)
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Layer tree node points to a map layer.
The RasterBandStats struct is a container for statistics about a single raster band.
double minimumValue
The minimum cell value in the raster band.
Feedback object tailored for raster block reading.
QgsRasterInterface(QgsRasterInterface *input=nullptr)
QString displayBandName(int bandNumber) const
Generates a friendly, descriptive name for the specified bandNumber.
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
QgsRasterRenderer(QgsRasterInterface *input=nullptr, const QString &type=QString())
Constructor for QgsRasterRenderer.
double mOpacity
Global alpha value (0-1).
int mAlphaBand
Read alpha value from band.
QgsRectangle mLastRectangleUsedByRefreshContrastEnhancementIfNeeded
To save computations and possible infinite cycle of notifications.
QRgb renderColorForNodataPixel() const
Returns the color for the renderer to use to represent nodata pixels.
std::unique_ptr< QgsRasterTransparency > mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses).
int bandCount() const override
Gets number of bands.
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
bool needsRefresh(const QgsRectangle &extent) const
Checks if the renderer needs to be refreshed according to extent.
virtual Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Implementation of legend node interface for displaying arbitrary labels with icons.
void setLegendSettings(QgsColorRampLegendNodeSettings *settings)
Sets the color ramp shader legend settings.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
const QgsContrastEnhancement * contrastEnhancement() const
Gradient gradient() const
void setContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
Qgis::RasterRendererFlags flags() const override
Returns flags which dictate renderer behavior.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const override
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
bool setInputBand(int band) override
Attempts to set the input band for the renderer.
Q_DECL_DEPRECATED void setGrayBand(int band)
QgsSingleBandGrayRenderer(QgsRasterInterface *input, int grayBand)
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
QList< QPair< QString, QColor > > legendSymbologyItems() const override
Returns symbology items if provided by renderer.
QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Creates a set of legend nodes representing the renderer.
Q_DECL_DEPRECATED int grayBand() const
void setGradient(Gradient gradient)
int inputBand() const override
Returns the input band for the renderer, or -1 if no input band is available.
bool refresh(const QgsRectangle &extent, const QList< double > &min, const QList< double > &max, bool forceRefresh=false) override
Refreshes the renderer according to the min and max values associated with the extent.
QgsSingleBandGrayRenderer * clone() const override
Clone itself, create deep copy.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)