28#include <QDomDocument>
36 , mClassificationMin( std::numeric_limits<double>::quiet_NaN() )
37 , mClassificationMax( std::numeric_limits<double>::quiet_NaN() )
68 mClassificationMin = min;
69 if (
auto *lShader =
shader() )
72 if ( colorRampShader )
81 mClassificationMax = max;
82 if (
auto *lShader =
shader() )
85 if ( colorRampShader )
103 if ( origColorRampShader )
106 shader->setRasterShaderFunction( colorRampShader );
134 colorRampShader->
setClip( clip );
148 const int band = elem.attribute( QStringLiteral(
"band" ), QStringLiteral(
"-1" ) ).toInt();
150 const QDomElement rasterShaderElem = elem.firstChildElement( QStringLiteral(
"rastershader" ) );
151 if ( !rasterShaderElem.isNull() )
154 shader->readXml( rasterShaderElem );
161 r->
setClassificationMin( elem.attribute( QStringLiteral(
"classificationMin" ), QStringLiteral(
"NaN" ) ).toDouble() );
162 r->
setClassificationMax( elem.attribute( QStringLiteral(
"classificationMax" ), QStringLiteral(
"NaN" ) ).toDouble() );
165 const QString
minMaxOrigin = elem.attribute( QStringLiteral(
"classificationMinMaxOrigin" ) );
168 if (
minMaxOrigin.contains( QLatin1String(
"MinMax" ) ) )
172 else if (
minMaxOrigin.contains( QLatin1String(
"CumulativeCut" ) ) )
176 else if (
minMaxOrigin.contains( QLatin1String(
"StdDev" ) ) )
185 if (
minMaxOrigin.contains( QLatin1String(
"FullExtent" ) ) )
189 else if (
minMaxOrigin.contains( QLatin1String(
"SubExtent" ) ) )
198 if (
minMaxOrigin.contains( QLatin1String(
"Estimated" ) ) )
215 auto outputBlock = std::make_unique<QgsRasterBlock>();
216 if ( !
mInput || !mShader || !mShader->rasterShaderFunction() )
218 return outputBlock.release();
222 const std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->block( mBand,
extent, width, height, feedback ) );
223 if ( !inputBlock || inputBlock->isEmpty() )
226 return outputBlock.release();
232 std::shared_ptr< QgsRasterBlock > alphaBlock;
236 if ( !alphaBlock || alphaBlock->isEmpty() )
238 return outputBlock.release();
243 alphaBlock = inputBlock;
248 return outputBlock.release();
252 QRgb *outputBlockData = outputBlock->colorData();
256 bool isNoData =
false;
257 for (
qgssize i = 0; i < count; i++ )
259 const double val = inputBlock->valueAndNoData( i, isNoData );
262 outputBlockData[i] = myDefaultColor;
266 int red, green, blue, alpha;
267 if ( !fcn->
shade( val, &red, &green, &blue, &alpha ) )
269 outputBlockData[i] = myDefaultColor;
276 red *= ( alpha / 255.0 );
277 blue *= ( alpha / 255.0 );
278 green *= ( alpha / 255.0 );
281 if ( !hasTransparency )
283 outputBlockData[i] = qRgba( red, green, blue, alpha );
295 const double alpha = alphaBlock->value( i );
298 outputBlock->setColor( i, myDefaultColor );
303 currentOpacity *= alpha / 255.0;
307 outputBlockData[i] = qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * alpha );
311 return outputBlock.release();
316 if ( parentElem.isNull() )
321 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
323 rasterRendererElem.setAttribute( QStringLiteral(
"band" ), mBand );
326 mShader->writeXml( doc, rasterRendererElem );
331 parentElem.appendChild( rasterRendererElem );
336 QList< QPair< QString, QColor > > symbolItems;
340 if ( shaderFunction )
362 toSld( doc, element, context );
371 const QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
372 if ( elements.size() == 0 )
376 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
379 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
380 rasterSymbolizerElem.appendChild( channelSelectionElem );
383 QDomElement channelElem = doc.createElement( QStringLiteral(
"sld:GrayChannel" ) );
384 channelSelectionElem.appendChild( channelElem );
387 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
388 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number( mBand ) ) );
389 channelElem.appendChild( sourceChannelNameElem );
392 QDomElement colorMapElem = doc.createElement( QStringLiteral(
"sld:ColorMap" ) );
396 QString rampType = QStringLiteral(
"ramp" );
404 rampType = QStringLiteral(
"values" );
407 rampType = QStringLiteral(
"intervals" );
410 rampType = QStringLiteral(
"ramp" );
414 colorMapElem.setAttribute( QStringLiteral(
"type" ), rampType );
416 colorMapElem.setAttribute( QStringLiteral(
"extended" ), QStringLiteral(
"true" ) );
417 rasterSymbolizerElem.appendChild( colorMapElem );
421 const QList<QgsColorRampShader::ColorRampItem> classes = rampShader->
colorRampItemList();
422 QList<QgsColorRampShader::ColorRampItem>::const_iterator classDataIt = classes.constBegin();
423 for ( ; classDataIt != classes.constEnd(); ++classDataIt )
425 QDomElement colorMapEntryElem = doc.createElement( QStringLiteral(
"sld:ColorMapEntry" ) );
426 colorMapElem.appendChild( colorMapEntryElem );
429 colorMapEntryElem.setAttribute( QStringLiteral(
"color" ), classDataIt->color.name() );
430 colorMapEntryElem.setAttribute( QStringLiteral(
"quantity" ), classDataIt->value );
431 colorMapEntryElem.setAttribute( QStringLiteral(
"label" ), classDataIt->label );
432 if ( classDataIt->color.alphaF() != 1.0 )
434 colorMapEntryElem.setAttribute( QStringLiteral(
"opacity" ), QString::number( classDataIt->color.alphaF() ) );
455 return QList<QgsLayerTreeModelLegendNode *>();
459 return QList<QgsLayerTreeModelLegendNode *>();
461 QList<QgsLayerTreeModelLegendNode *> res;
464 if ( !name.isEmpty() )
490 res.reserve( items.size() );
491 for (
const QPair< QString, QColor > &item : items )
513 bool refreshed =
false;
514 if ( min.size() >= 1 && max.size() >= 1 )
522 bool refreshed =
false;
524 if ( !std::isnan( min[0] ) )
530 if ( !std::isnan( max[0] ) )
537 if ( rampShader && refreshed )
QFlags< RasterRendererFlag > RasterRendererFlags
Flags which control behavior of raster renderers.
@ CumulativeCut
Range is [ min + cumulativeCutLower() * (max - min), min + cumulativeCutUpper() * (max - min) ].
@ StdDev
Range is [ mean - stdDevFactor() * stddev, mean + stdDevFactor() * stddev ].
@ MinimumMaximum
Real min-max values.
ShaderInterpolationMethod
Color ramp shader interpolation methods.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
@ Linear
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
@ InternalLayerOpacityHandling
The renderer internally handles the raster layer's opacity, so the default layer level opacity handli...
@ Estimated
Approximated statistics.
ShaderClassificationMethod
Color ramp shader classification methods.
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ WholeRaster
Whole raster is used to compute statistics.
@ FixedCanvas
Current extent of the canvas (at the time of computation) is used to compute statistics.
Settings for a color ramp legend node.
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
A legend node which renders a color ramp.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Qgis::ShaderInterpolationMethod colorRampType() const
Returns the color ramp interpolation method.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom color map.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
QgsColorRamp * createColorRamp() const
Creates a gradient color ramp from shader settings.
void classifyColorRamp(int classes=0, int band=-1, const QgsRectangle &extent=QgsRectangle(), QgsRasterInterface *input=nullptr)
Classify color ramp shader.
Abstract base class for color ramps.
Layer tree node points to a map layer.
Feedback object tailored for raster block reading.
static QString printValue(double value, bool localized=false)
Print double value with all necessary significant digits.
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.
void setLimits(Qgis::RasterRangeLimit limits)
Sets the limits.
void setExtent(Qgis::RasterRangeExtent extent)
Sets the extent.
void setStatAccuracy(Qgis::RasterRangeAccuracy accuracy)
Sets the statistics accuracy.
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.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
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.
QgsRasterMinMaxOrigin mMinMaxOrigin
Origin of min/max values.
bool usesTransparency() const
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.
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
virtual void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const
Returns legend symbology items if provided by renderer.
double maximumValue() const
Returns the minimum value for the raster shader.
virtual bool shade(double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha) const
Generates an new RGBA value based on one input value.
virtual void setMaximumValue(double value)
Sets the maximum value for the raster shader.
virtual void setMinimumValue(double value)
Sets the minimum value for the raster shader.
double minimumValue() const
Returns the maximum value for the raster shader.
Interface for all raster shaders.
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
Implementation of legend node interface for displaying raster legend entries.
A rectangle specified with double values.
Implementation of legend node interface for displaying arbitrary labels with icons.
QgsSingleBandPseudoColorRenderer * clone() const override
Clone itself, create deep copy.
void setClassificationMin(double min)
QList< QPair< QString, QColor > > legendSymbologyItems() const override
Returns symbology items if provided by renderer.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
Q_DECL_DEPRECATED void setBand(int bandNo)
Sets the band used by the renderer.
void setShader(QgsRasterShader *shader)
Takes ownership of the shader.
double classificationMin() const
QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Creates a set of legend nodes representing the renderer.
void setClassificationMax(double max)
QgsRasterShader * shader()
Returns the raster shader.
double classificationMax() const
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
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.
bool setInputBand(int band) override
Attempts to set the input band for the renderer.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Qgis::RasterRendererFlags flags() const override
Returns flags which dictate renderer behavior.
QgsSingleBandPseudoColorRenderer(QgsRasterInterface *input, int band=-1, QgsRasterShader *shader=nullptr)
Note: takes ownership of QgsRasterShader.
int inputBand() const override
Returns the input band for the renderer, or -1 if no input band is available.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
void createShader(QgsColorRamp *colorRamp=nullptr, Qgis::ShaderInterpolationMethod colorRampType=Qgis::ShaderInterpolationMethod::Linear, Qgis::ShaderClassificationMethod classificationMode=Qgis::ShaderClassificationMethod::Continuous, int classes=0, bool clip=false, const QgsRectangle &extent=QgsRectangle())
Creates a color ramp shader.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
bool canCreateRasterAttributeTable() const override
Returns true if the renderer is suitable for attribute table creation.
Q_DECL_DEPRECATED int band() const
Returns the band used by the renderer.
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.
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.
A color ramp entity for QgsStyle databases.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
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...
#define QgsDebugError(str)
Contains information relating to the style entity currently being visited.