25 #include <QDomDocument> 26 #include <QDomElement> 33 , mClassificationMin( std::numeric_limits<double>::quiet_NaN() )
34 , mClassificationMax( std::numeric_limits<double>::quiet_NaN() )
49 mClassificationMin = min;
53 if ( colorRampShader )
62 mClassificationMax = max;
66 if ( colorRampShader )
79 shader =
new QgsRasterShader( mShader->minimumValue(), mShader->maximumValue() );
84 if ( origColorRampShader )
88 if ( origColorRampShader->sourceColorRamp() )
90 colorRampShader->
setSourceColorRamp( origColorRampShader->sourceColorRamp()->clone() );
94 colorRampShader->
setClip( origColorRampShader->clip() );
107 mShader.reset( shader );
119 colorRampShader->
setClip( clip );
133 int band = elem.attribute( QStringLiteral(
"band" ), QStringLiteral(
"-1" ) ).toInt();
135 QDomElement rasterShaderElem = elem.firstChildElement( QStringLiteral(
"rastershader" ) );
136 if ( !rasterShaderElem.isNull() )
139 shader->
readXml( rasterShaderElem );
146 r->
setClassificationMin( elem.attribute( QStringLiteral(
"classificationMin" ), QStringLiteral(
"NaN" ) ).toDouble() );
147 r->
setClassificationMax( elem.attribute( QStringLiteral(
"classificationMax" ), QStringLiteral(
"NaN" ) ).toDouble() );
150 QString
minMaxOrigin = elem.attribute( QStringLiteral(
"classificationMinMaxOrigin" ) );
151 if ( !minMaxOrigin.isEmpty() )
153 if ( minMaxOrigin.contains( QLatin1String(
"MinMax" ) ) )
157 else if ( minMaxOrigin.contains( QLatin1String(
"CumulativeCut" ) ) )
161 else if ( minMaxOrigin.contains( QLatin1String(
"StdDev" ) ) )
170 if ( minMaxOrigin.contains( QLatin1String(
"FullExtent" ) ) )
174 else if ( minMaxOrigin.contains( QLatin1String(
"SubExtent" ) ) )
183 if ( minMaxOrigin.contains( QLatin1String(
"Estimated" ) ) )
200 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
201 if ( !
mInput || !mShader || !mShader->rasterShaderFunction() )
203 return outputBlock.release();
207 std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->
block( mBand, extent, width, height, feedback ) );
208 if ( !inputBlock || inputBlock->isEmpty() )
210 QgsDebugMsg( QStringLiteral(
"No raster data!" ) );
211 return outputBlock.release();
217 std::shared_ptr< QgsRasterBlock > alphaBlock;
221 if ( !alphaBlock || alphaBlock->isEmpty() )
223 return outputBlock.release();
228 alphaBlock = inputBlock;
233 return outputBlock.release();
237 QRgb *outputBlockData = outputBlock->colorData();
241 bool isNoData =
false;
242 for (
qgssize i = 0; i < count; i++ )
244 double val = inputBlock->valueAndNoData( i, isNoData );
247 outputBlockData[i] = myDefaultColor;
251 int red, green, blue, alpha;
252 if ( !fcn->
shade( val, &red, &green, &blue, &alpha ) )
254 outputBlockData[i] = myDefaultColor;
261 red *= ( alpha / 255.0 );
262 blue *= ( alpha / 255.0 );
263 green *= ( alpha / 255.0 );
266 if ( !hasTransparency )
268 outputBlockData[i] = qRgba( red, green, blue, alpha );
280 currentOpacity *= alphaBlock->value( i ) / 255.0;
283 outputBlockData[i] = qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * alpha );
287 return outputBlock.release();
292 if ( parentElem.isNull() )
297 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
299 rasterRendererElem.setAttribute( QStringLiteral(
"band" ), mBand );
302 mShader->writeXml( doc, rasterRendererElem );
307 parentElem.appendChild( rasterRendererElem );
315 if ( shaderFunction )
340 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
341 if ( elements.size() == 0 )
345 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
348 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
349 rasterSymbolizerElem.appendChild( channelSelectionElem );
352 QDomElement channelElem = doc.createElement( QStringLiteral(
"sld:GrayChannel" ) );
353 channelSelectionElem.appendChild( channelElem );
356 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
357 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number(
band() ) ) );
358 channelElem.appendChild( sourceChannelNameElem );
361 QDomElement colorMapElem = doc.createElement( QStringLiteral(
"sld:ColorMap" ) );
365 QString rampType = QStringLiteral(
"ramp" );
372 rampType = QStringLiteral(
"values" );
375 rampType = QStringLiteral(
"intervals" );
378 rampType = QStringLiteral(
"ramp" );
383 colorMapElem.setAttribute( QStringLiteral(
"type" ), rampType );
385 colorMapElem.setAttribute( QStringLiteral(
"extended" ), QStringLiteral(
"true" ) );
386 rasterSymbolizerElem.appendChild( colorMapElem );
390 QList<QgsColorRampShader::ColorRampItem> classes = rampShader->
colorRampItemList();
391 QList<QgsColorRampShader::ColorRampItem>::const_iterator classDataIt = classes.constBegin();
392 for ( ; classDataIt != classes.constEnd(); ++classDataIt )
394 QDomElement colorMapEntryElem = doc.createElement( QStringLiteral(
"sld:ColorMapEntry" ) );
395 colorMapElem.appendChild( colorMapEntryElem );
398 colorMapEntryElem.setAttribute( QStringLiteral(
"color" ), classDataIt->color.name() );
399 colorMapEntryElem.setAttribute( QStringLiteral(
"quantity" ), classDataIt->value );
400 colorMapEntryElem.setAttribute( QStringLiteral(
"label" ), classDataIt->label );
401 if ( classDataIt->color.alphaF() != 1.0 )
403 colorMapEntryElem.setAttribute( QStringLiteral(
"opacity" ), QString::number( classDataIt->color.alphaF() ) );
virtual int bandCount() const =0
Gets number of bands.
virtual void setMinimumValue(double value)
Sets the minimum value for the raster shader.
A rectangle specified with double values.
double classificationMin() const
Interface for all raster shaders.
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &list)
Sets a custom colormap.
static QString printValue(double value)
Print double value with all necessary significant digits.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Gets symbology items if provided by renderer.
virtual void toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
virtual bool shade(double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha) const
Generates an new RGBA value based on one input value.
QgsSingleBandPseudoColorRenderer * clone() const override
Clone itself, create deep copy.
virtual QgsRectangle extent() const
Gets the extent of the interface.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Abstract base class for color ramps.
virtual QgsRasterInterface * input() const
Current input.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
QgsRasterMinMaxOrigin mMinMaxOrigin
Origin of min/max values.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
virtual void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const
Returns legend symbology items if provided by renderer.
QgsRasterShader * shader()
Returns the raster shader.
QMap< QString, QString > QgsStringMap
void setClassificationMax(double max)
void setShader(QgsRasterShader *shader)
Takes ownership of the shader.
Type
Supported methods for color interpolation.
Current extent of the canvas (at the time of computation) is used to compute statistics.
void setExtent(QgsRasterMinMaxOrigin::Extent extent)
Sets the extent.
virtual void setMaximumValue(double value)
Sets the maximum value for the raster shader.
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
static const QRgb NODATA_COLOR
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
void setColorRampType(QgsColorRampShader::Type colorRampType)
Sets the color ramp type.
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.
QgsRasterShaderFunction * rasterShaderFunction()
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses) ...
double classificationMax() const
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
Raster renderer pipe for single band pseudocolor.
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
Range is [ mean - stdDevFactor() * stddev, mean + stdDevFactor() * stddev ].
bool usesTransparency() const
int mAlphaBand
Read alpha value from band.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
Range is [ min + cumulativeCutLower() * (max - min), min + cumulativeCutUpper() * (max - min) ]...
Base class for processing filters like renderers, reprojector, resampler etc.
int alphaValue(double value, int globalTransparency=255) const
Returns the transparency value for a single value pixel.
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...
void classifyColorRamp(int classes=0, int band=-1, const QgsRectangle &extent=QgsRectangle(), QgsRasterInterface *input=nullptr)
Classify color ramp shader.
void setLimits(QgsRasterMinMaxOrigin::Limits limits)
Sets the limits.
Type colorRampType() const
Returns the color ramp type.
void setClassificationMin(double min)
Assigns the color of the exact matching value in the color ramp item list.
void setStatAccuracy(QgsRasterMinMaxOrigin::StatAccuracy accuracy)
Sets the statistics accuracy.
Whole raster is used to compute statistics.
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
void setBand(int bandNo)
Sets the band used by the renderer.
double mOpacity
Global alpha value (0-1)
Interpolates the color between two class breaks linearly.
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.
ClassificationMode
Classification modes used to create the color ramp shader.
Assigns the color of the higher class for every pixel between two class breaks.
QgsRasterInterface * mInput
QgsSingleBandPseudoColorRenderer(QgsRasterInterface *input, int band=-1, QgsRasterShader *shader=nullptr)
Note: takes ownership of QgsRasterShader.
void toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const override
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
Feedback object tailored for raster block reading.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Raster renderer pipe that applies colors to a raster.
int band() const
Returns the band used by the renderer.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
void readXml(const QDomElement &elem)
Reads shader state from an XML element.