QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
26 #include <QDomDocument>
27 #include <QDomElement>
34 , mClassificationMin( std::numeric_limits<double>::quiet_NaN() )
35 , mClassificationMax( std::numeric_limits<double>::quiet_NaN() )
47 if ( bandNo <= mInput->
bandCount() || bandNo > 0 )
55 mClassificationMin = min;
56 if (
auto *lShader =
shader() )
59 if ( colorRampShader )
68 mClassificationMax = max;
69 if (
auto *lShader =
shader() )
72 if ( colorRampShader )
90 if ( origColorRampShader )
100 colorRampShader->
setClip( origColorRampShader->
clip() );
125 colorRampShader->
setClip( clip );
139 int band = elem.attribute( QStringLiteral(
"band" ), QStringLiteral(
"-1" ) ).toInt();
141 QDomElement rasterShaderElem = elem.firstChildElement( QStringLiteral(
"rastershader" ) );
142 if ( !rasterShaderElem.isNull() )
152 r->
setClassificationMin( elem.attribute( QStringLiteral(
"classificationMin" ), QStringLiteral(
"NaN" ) ).toDouble() );
153 r->
setClassificationMax( elem.attribute( QStringLiteral(
"classificationMax" ), QStringLiteral(
"NaN" ) ).toDouble() );
156 QString
minMaxOrigin = elem.attribute( QStringLiteral(
"classificationMinMaxOrigin" ) );
159 if (
minMaxOrigin.contains( QLatin1String(
"MinMax" ) ) )
163 else if (
minMaxOrigin.contains( QLatin1String(
"CumulativeCut" ) ) )
167 else if (
minMaxOrigin.contains( QLatin1String(
"StdDev" ) ) )
176 if (
minMaxOrigin.contains( QLatin1String(
"FullExtent" ) ) )
180 else if (
minMaxOrigin.contains( QLatin1String(
"SubExtent" ) ) )
189 if (
minMaxOrigin.contains( QLatin1String(
"Estimated" ) ) )
206 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
207 if ( !
mInput || !mShader || !mShader->rasterShaderFunction() )
209 return outputBlock.release();
213 std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->
block( mBand,
extent, width, height, feedback ) );
214 if ( !inputBlock || inputBlock->isEmpty() )
216 QgsDebugMsg( QStringLiteral(
"No raster data!" ) );
217 return outputBlock.release();
223 std::shared_ptr< QgsRasterBlock > alphaBlock;
227 if ( !alphaBlock || alphaBlock->isEmpty() )
229 return outputBlock.release();
234 alphaBlock = inputBlock;
239 return outputBlock.release();
243 QRgb *outputBlockData = outputBlock->colorData();
247 bool isNoData =
false;
248 for (
qgssize i = 0; i < count; i++ )
250 double val = inputBlock->valueAndNoData( i, isNoData );
253 outputBlockData[i] = myDefaultColor;
257 int red, green, blue, alpha;
258 if ( !fcn->
shade( val, &red, &green, &blue, &alpha ) )
260 outputBlockData[i] = myDefaultColor;
267 red *= ( alpha / 255.0 );
268 blue *= ( alpha / 255.0 );
269 green *= ( alpha / 255.0 );
272 if ( !hasTransparency )
274 outputBlockData[i] = qRgba( red, green, blue, alpha );
286 currentOpacity *= alphaBlock->value( i ) / 255.0;
289 outputBlockData[i] = qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * alpha );
293 return outputBlock.release();
298 if ( parentElem.isNull() )
303 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
305 rasterRendererElem.setAttribute( QStringLiteral(
"band" ), mBand );
308 mShader->writeXml( doc, rasterRendererElem );
313 parentElem.appendChild( rasterRendererElem );
321 if ( shaderFunction )
344 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
345 if ( elements.size() == 0 )
349 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
352 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
353 rasterSymbolizerElem.appendChild( channelSelectionElem );
356 QDomElement channelElem = doc.createElement( QStringLiteral(
"sld:GrayChannel" ) );
357 channelSelectionElem.appendChild( channelElem );
360 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
361 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number(
band() ) ) );
362 channelElem.appendChild( sourceChannelNameElem );
365 QDomElement colorMapElem = doc.createElement( QStringLiteral(
"sld:ColorMap" ) );
369 QString rampType = QStringLiteral(
"ramp" );
377 rampType = QStringLiteral(
"values" );
380 rampType = QStringLiteral(
"intervals" );
383 rampType = QStringLiteral(
"ramp" );
387 colorMapElem.setAttribute( QStringLiteral(
"type" ), rampType );
389 colorMapElem.setAttribute( QStringLiteral(
"extended" ), QStringLiteral(
"true" ) );
390 rasterSymbolizerElem.appendChild( colorMapElem );
394 QList<QgsColorRampShader::ColorRampItem> classes = rampShader->
colorRampItemList();
395 QList<QgsColorRampShader::ColorRampItem>::const_iterator classDataIt = classes.constBegin();
396 for ( ; classDataIt != classes.constEnd(); ++classDataIt )
398 QDomElement colorMapEntryElem = doc.createElement( QStringLiteral(
"sld:ColorMapEntry" ) );
399 colorMapElem.appendChild( colorMapEntryElem );
402 colorMapEntryElem.setAttribute( QStringLiteral(
"color" ), classDataIt->color.name() );
403 colorMapEntryElem.setAttribute( QStringLiteral(
"quantity" ), classDataIt->value );
404 colorMapEntryElem.setAttribute( QStringLiteral(
"label" ), classDataIt->label );
405 if ( classDataIt->color.alphaF() != 1.0 )
407 colorMapEntryElem.setAttribute( QStringLiteral(
"opacity" ), QString::number( classDataIt->color.alphaF() ) );
Abstract base class for color ramps.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
static QString printValue(double value)
Print double value with all necessary significant digits.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
QgsSingleBandPseudoColorRenderer * clone() const override
Clone itself, create deep copy.
QgsSingleBandPseudoColorRenderer(QgsRasterInterface *input, int band=-1, QgsRasterShader *shader=nullptr)
Note: takes ownership of QgsRasterShader.
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
QgsRasterInterface * mInput
int alphaValue(double value, int globalTransparency=255) const
Returns the transparency value for a single value pixel.
virtual void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const
Returns legend symbology items if provided by renderer.
void setShader(QgsRasterShader *shader)
Takes ownership of the shader.
A color ramp entity for QgsStyle databases.
void classifyColorRamp(int classes=0, int band=-1, const QgsRectangle &extent=QgsRectangle(), QgsRasterInterface *input=nullptr)
Classify color ramp shader.
void setBand(int bandNo)
Sets the band used by the renderer.
double classificationMin() const
An interface for classes which can visit style entity (e.g.
void setStatAccuracy(QgsRasterMinMaxOrigin::StatAccuracy accuracy)
Sets the statistics accuracy.
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.
int band() const
Returns the band used by the renderer.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Type
Supported methods for color interpolation.
A rectangle specified with double values.
Contains information relating to the style entity currently being visited.
@ WholeRaster
Whole raster is used to compute statistics.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Gets symbology items if provided by renderer.
void setClassificationMax(double max)
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
void setColorRampType(QgsColorRampShader::Type colorRampType)
Sets the color ramp type.
QgsRasterMinMaxOrigin mMinMaxOrigin
Origin of min/max values.
double classificationMax() const
@ CumulativeCut
Range is [ min + cumulativeCutLower() * (max - min), min + cumulativeCutUpper() * (max - min) ].
QgsColorRamp * sourceColorRamp() const
Returns the source color ramp.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
@ MinMax
Real min-max values.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Raster renderer pipe that applies colors to a raster.
void readXml(const QDomElement &elem)
Reads shader state from an XML element.
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.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses)
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
void setClassificationMin(double min)
bool usesTransparency() const
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &list)
Sets a custom colormap.
int mAlphaBand
Read alpha value from band.
int bandCount() const override
Gets number of bands.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
@ Estimated
Approximated statistics.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
Type colorRampType() const
Returns the color ramp type.
ClassificationMode
Classification modes used to create the color ramp shader.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
QMap< QString, QString > QgsStringMap
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Base class for processing filters like renderers, reprojector, resampler etc.
@ StdDev
Range is [ mean - stdDevFactor() * stddev, mean + stdDevFactor() * stddev ].
QRgb renderColorForNodataPixel() const
Returns the color for the renderer to use to represent nodata pixels.
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.
virtual QgsRasterInterface * input() const
Current input.
bool clip() const
Returns whether the shader will clip values which are out of range.
void setLimits(QgsRasterMinMaxOrigin::Limits limits)
Sets the limits.
Feedback object tailored for raster block reading.
double mOpacity
Global alpha value (0-1)
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.
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
ClassificationMode classificationMode() const
Returns the classification mode.
void setExtent(QgsRasterMinMaxOrigin::Extent extent)
Sets the extent.
Raster renderer pipe for single band pseudocolor.
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
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.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
@ CurrentCanvas
Current extent of the canvas (at the time of computation) is used to compute statistics.
Interface for all raster shaders.
QgsRasterShader * shader()
Returns the raster shader.
@ Interpolated
Interpolates the color between two class breaks linearly.
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 QgsRectangle extent() const
Gets the extent of the interface.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
virtual void setMinimumValue(double value)
Sets the minimum value for the raster shader.
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...