27 #include <QDomDocument>
28 #include <QDomElement>
35 , mClassificationMin( std::numeric_limits<double>::quiet_NaN() )
36 , mClassificationMax( std::numeric_limits<double>::quiet_NaN() )
48 if ( bandNo <= mInput->
bandCount() || bandNo > 0 )
56 mClassificationMin = min;
57 if (
auto *lShader =
shader() )
60 if ( colorRampShader )
69 mClassificationMax = max;
70 if (
auto *lShader =
shader() )
73 if ( colorRampShader )
91 if ( origColorRampShader )
101 colorRampShader->
setClip( origColorRampShader->
clip() );
126 colorRampShader->
setClip( clip );
140 int band = elem.attribute( QStringLiteral(
"band" ), QStringLiteral(
"-1" ) ).toInt();
142 QDomElement rasterShaderElem = elem.firstChildElement( QStringLiteral(
"rastershader" ) );
143 if ( !rasterShaderElem.isNull() )
153 r->
setClassificationMin( elem.attribute( QStringLiteral(
"classificationMin" ), QStringLiteral(
"NaN" ) ).toDouble() );
154 r->
setClassificationMax( elem.attribute( QStringLiteral(
"classificationMax" ), QStringLiteral(
"NaN" ) ).toDouble() );
157 QString
minMaxOrigin = elem.attribute( QStringLiteral(
"classificationMinMaxOrigin" ) );
160 if (
minMaxOrigin.contains( QLatin1String(
"MinMax" ) ) )
164 else if (
minMaxOrigin.contains( QLatin1String(
"CumulativeCut" ) ) )
168 else if (
minMaxOrigin.contains( QLatin1String(
"StdDev" ) ) )
177 if (
minMaxOrigin.contains( QLatin1String(
"FullExtent" ) ) )
181 else if (
minMaxOrigin.contains( QLatin1String(
"SubExtent" ) ) )
190 if (
minMaxOrigin.contains( QLatin1String(
"Estimated" ) ) )
207 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
208 if ( !
mInput || !mShader || !mShader->rasterShaderFunction() )
210 return outputBlock.release();
214 std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->
block( mBand,
extent, width, height, feedback ) );
215 if ( !inputBlock || inputBlock->isEmpty() )
217 QgsDebugMsg( QStringLiteral(
"No raster data!" ) );
218 return outputBlock.release();
224 std::shared_ptr< QgsRasterBlock > alphaBlock;
228 if ( !alphaBlock || alphaBlock->isEmpty() )
230 return outputBlock.release();
235 alphaBlock = inputBlock;
240 return outputBlock.release();
244 QRgb *outputBlockData = outputBlock->colorData();
248 bool isNoData =
false;
249 for (
qgssize i = 0; i < count; i++ )
251 double val = inputBlock->valueAndNoData( i, isNoData );
254 outputBlockData[i] = myDefaultColor;
258 int red, green, blue, alpha;
259 if ( !fcn->
shade( val, &red, &green, &blue, &alpha ) )
261 outputBlockData[i] = myDefaultColor;
268 red *= ( alpha / 255.0 );
269 blue *= ( alpha / 255.0 );
270 green *= ( alpha / 255.0 );
273 if ( !hasTransparency )
275 outputBlockData[i] = qRgba( red, green, blue, alpha );
287 currentOpacity *= alphaBlock->value( i ) / 255.0;
290 outputBlockData[i] = qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * alpha );
294 return outputBlock.release();
299 if ( parentElem.isNull() )
304 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
306 rasterRendererElem.setAttribute( QStringLiteral(
"band" ), mBand );
309 mShader->writeXml( doc, rasterRendererElem );
314 parentElem.appendChild( rasterRendererElem );
319 QList< QPair< QString, QColor > > symbolItems;
323 if ( shaderFunction )
347 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
348 if ( elements.size() == 0 )
352 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
355 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
356 rasterSymbolizerElem.appendChild( channelSelectionElem );
359 QDomElement channelElem = doc.createElement( QStringLiteral(
"sld:GrayChannel" ) );
360 channelSelectionElem.appendChild( channelElem );
363 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
364 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number(
band() ) ) );
365 channelElem.appendChild( sourceChannelNameElem );
368 QDomElement colorMapElem = doc.createElement( QStringLiteral(
"sld:ColorMap" ) );
372 QString rampType = QStringLiteral(
"ramp" );
380 rampType = QStringLiteral(
"values" );
383 rampType = QStringLiteral(
"intervals" );
386 rampType = QStringLiteral(
"ramp" );
390 colorMapElem.setAttribute( QStringLiteral(
"type" ), rampType );
392 colorMapElem.setAttribute( QStringLiteral(
"extended" ), QStringLiteral(
"true" ) );
393 rasterSymbolizerElem.appendChild( colorMapElem );
397 QList<QgsColorRampShader::ColorRampItem> classes = rampShader->
colorRampItemList();
398 QList<QgsColorRampShader::ColorRampItem>::const_iterator classDataIt = classes.constBegin();
399 for ( ; classDataIt != classes.constEnd(); ++classDataIt )
401 QDomElement colorMapEntryElem = doc.createElement( QStringLiteral(
"sld:ColorMapEntry" ) );
402 colorMapElem.appendChild( colorMapEntryElem );
405 colorMapEntryElem.setAttribute( QStringLiteral(
"color" ), classDataIt->color.name() );
406 colorMapEntryElem.setAttribute( QStringLiteral(
"quantity" ), classDataIt->value );
407 colorMapEntryElem.setAttribute( QStringLiteral(
"label" ), classDataIt->label );
408 if ( classDataIt->color.alphaF() != 1.0 )
410 colorMapEntryElem.setAttribute( QStringLiteral(
"opacity" ), QString::number( classDataIt->color.alphaF() ) );
430 return QList<QgsLayerTreeModelLegendNode *>();
434 return QList<QgsLayerTreeModelLegendNode *>();
436 QList<QgsLayerTreeModelLegendNode *> res;
439 if ( !name.isEmpty() )
465 res.reserve( items.size() );
466 for (
const QPair< QString, QColor > &item : items )
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
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.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
ClassificationMode classificationMode() const
Returns the classification mode.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
Type colorRampType() const
Returns the color ramp type.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
ClassificationMode
Classification modes used to create the color ramp shader.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
bool clip() const
Returns whether the shader will clip values which are out of range.
QgsColorRamp * sourceColorRamp() const
Returns the source color ramp.
QgsColorRamp * createColorRamp() const
Creates a gradient color ramp from shader settings.
Type
Supported methods for color interpolation.
@ Interpolated
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
void classifyColorRamp(int classes=0, int band=-1, const QgsRectangle &extent=QgsRectangle(), QgsRasterInterface *input=nullptr)
Classify color ramp shader.
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &list)
Sets a custom colormap.
void setColorRampType(QgsColorRampShader::Type colorRampType)
Sets the color ramp type.
Abstract base class for color ramps.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
Layer tree node points to a map layer.
Feedback object tailored for raster block reading.
static QString printValue(double value)
Print double value with all necessary significant digits.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
virtual QgsRasterInterface * input() const
Current input.
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.
void setExtent(QgsRasterMinMaxOrigin::Extent extent)
Sets the extent.
@ Estimated
Approximated statistics.
void setLimits(QgsRasterMinMaxOrigin::Limits limits)
Sets the limits.
void setStatAccuracy(QgsRasterMinMaxOrigin::StatAccuracy accuracy)
Sets the statistics accuracy.
@ CurrentCanvas
Current extent of the canvas (at the time of computation) is used to compute statistics.
@ WholeRaster
Whole raster is used to compute statistics.
@ StdDev
Range is [ mean - stdDevFactor() * stddev, mean + stdDevFactor() * stddev ].
@ MinMax
Real min-max values.
@ CumulativeCut
Range is [ min + cumulativeCutLower() * (max - min), min + cumulativeCutUpper() * (max - min) ].
Raster renderer pipe that applies colors to a raster.
double mOpacity
Global alpha value (0-1)
int mAlphaBand
Read alpha value from band.
QRgb renderColorForNodataPixel() const
Returns the color for the renderer to use to represent nodata pixels.
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.
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
bool usesTransparency() const
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
virtual 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 readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads shader state from an XML element.
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.
int alphaValue(double value, int globalTransparency=255) const
Returns the transparency value for a single value pixel.
A rectangle specified with double values.
Implementation of legend node interface for displaying arbitrary label with icon.
Raster renderer pipe for single band pseudocolor.
QgsSingleBandPseudoColorRenderer * clone() const override
Clone itself, create deep copy.
void setClassificationMin(double min)
void createShader(QgsColorRamp *colorRamp=nullptr, QgsColorRampShader::Type colorRampType=QgsColorRampShader::Interpolated, QgsColorRampShader::ClassificationMode classificationMode=QgsColorRampShader::Continuous, int classes=0, bool clip=false, const QgsRectangle &extent=QgsRectangle())
Creates a color ramp shader.
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.
int band() const
Returns the band used by the renderer.
QgsRasterShader * shader()
Returns the raster shader.
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)
double classificationMax() const
QList< int > usesBands() const override
Returns a list of band numbers used by 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.
QgsSingleBandPseudoColorRenderer(QgsRasterInterface *input, int band=-1, QgsRasterShader *shader=nullptr)
Note: takes ownership of QgsRasterShader.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
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.
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...
Contains information relating to the style entity currently being visited.