24 #include <QDomDocument> 25 #include <QDomElement> 33 , mClassData( classes )
41 if ( mSourceColorRamp )
55 int bandNumber = elem.attribute( QStringLiteral(
"band" ), QStringLiteral(
"-1" ) ).toInt();
58 QDomElement paletteElem = elem.firstChildElement( QStringLiteral(
"colorPalette" ) );
59 if ( !paletteElem.isNull() )
61 QDomNodeList paletteEntries = paletteElem.elementsByTagName( QStringLiteral(
"paletteEntry" ) );
63 QDomElement entryElem;
66 for (
int i = 0; i < paletteEntries.size(); ++i )
70 entryElem = paletteEntries.at( i ).toElement();
71 value = ( int )entryElem.attribute( QStringLiteral(
"value" ), QStringLiteral(
"0" ) ).toDouble();
73 color = QColor( entryElem.attribute( QStringLiteral(
"color" ), QStringLiteral(
"#000000" ) ) );
74 color.setAlpha( entryElem.attribute( QStringLiteral(
"alpha" ), QStringLiteral(
"255" ) ).toInt() );
75 label = entryElem.attribute( QStringLiteral(
"label" ) );
76 classData <<
Class( value, color, label );
84 QDomElement sourceColorRampElem = elem.firstChildElement( QStringLiteral(
"colorramp" ) );
85 if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral(
"name" ) ) == QLatin1String(
"[source]" ) )
100 Q_FOREACH (
const Class &
c, mClassData )
102 if ( c.
value == idx )
111 ClassData::iterator cIt = mClassData.begin();
112 for ( ; cIt != mClassData.end(); ++cIt )
114 if ( cIt->value == idx )
124 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
125 if ( !
mInput || mClassData.isEmpty() )
127 return outputBlock.release();
130 std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->
block( bandNo, extent, width, height, feedback ) );
132 if ( !inputBlock || inputBlock->isEmpty() )
135 return outputBlock.release();
143 std::shared_ptr< QgsRasterBlock > alphaBlock;
148 if ( !alphaBlock || alphaBlock->isEmpty() )
150 return outputBlock.release();
155 alphaBlock = inputBlock;
160 return outputBlock.release();
167 unsigned int *outputData = (
unsigned int * )( outputBlock->bits() );
170 for (
qgssize i = 0; i < rasterSize; ++i )
172 if ( inputBlock->isNoData( i ) )
174 outputData[i] = myDefaultColor;
177 int val = ( int ) inputBlock->value( i );
178 if ( !mColors.contains( val ) )
180 outputData[i] = myDefaultColor;
184 if ( !hasTransparency )
186 outputData[i] = mColors.value( val );
197 currentOpacity *= alphaBlock->value( i ) / 255.0;
200 QRgb
c = mColors.value( val );
201 outputData[i] = qRgba( currentOpacity * qRed( c ), currentOpacity * qGreen( c ), currentOpacity * qBlue( c ), currentOpacity * qAlpha( c ) );
205 return outputBlock.release();
210 if ( parentElem.isNull() )
215 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
218 rasterRendererElem.setAttribute( QStringLiteral(
"band" ), mBand );
219 QDomElement colorPaletteElem = doc.createElement( QStringLiteral(
"colorPalette" ) );
220 ClassData::const_iterator it = mClassData.constBegin();
221 for ( ; it != mClassData.constEnd(); ++it )
223 QColor color = it->color;
224 QDomElement colorElem = doc.createElement( QStringLiteral(
"paletteEntry" ) );
225 colorElem.setAttribute( QStringLiteral(
"value" ), it->value );
226 colorElem.setAttribute( QStringLiteral(
"color" ), color.name() );
227 colorElem.setAttribute( QStringLiteral(
"alpha" ), color.alpha() );
228 if ( !it->label.isEmpty() )
230 colorElem.setAttribute( QStringLiteral(
"label" ), it->label );
232 colorPaletteElem.appendChild( colorElem );
234 rasterRendererElem.appendChild( colorPaletteElem );
237 if ( mSourceColorRamp )
240 rasterRendererElem.appendChild( colorRampElem );
243 parentElem.appendChild( rasterRendererElem );
248 ClassData::const_iterator it = mClassData.constBegin();
249 for ( ; it != mClassData.constEnd(); ++it )
251 QString lab = it->label.isEmpty() ? QString::number( it->value ) : it->label;
252 symbolItems << qMakePair( lab, it->color );
268 mSourceColorRamp.reset( ramp );
273 return mSourceColorRamp.get();
278 QList<QgsColorRampShader::ColorRampItem>::const_iterator colorIt = table.constBegin();
280 for ( ; colorIt != table.constEnd(); ++colorIt )
282 int idx = ( int )( colorIt->value );
292 QRegularExpression linePartRx( QStringLiteral(
"[\\s,:]+" ) );
294 QStringList parts =
string.split(
'\n', QString::SkipEmptyParts );
295 Q_FOREACH (
const QString &part, parts )
297 QStringList lineParts = part.split( linePartRx, QString::SkipEmptyParts );
299 switch ( lineParts.count() )
303 int value = lineParts.at( 0 ).toInt( &ok );
307 classes <<
Class( value );
313 int value = lineParts.at( 0 ).toInt( &ok );
317 QColor
c( lineParts.at( 1 ) );
319 classes <<
Class( value,
c );
325 if ( lineParts.count() < 4 )
328 int value = lineParts.at( 0 ).toInt( &ok );
333 double r = lineParts.at( 1 ).toDouble( &rOk );
335 double g = lineParts.at( 2 ).toDouble( &gOk );
337 double b = lineParts.at( 3 ).toDouble( &bOk );
340 if ( rOk && gOk && bOk )
342 c = QColor( r, g, b );
345 if ( lineParts.count() >= 5 )
347 double alpha = lineParts.at( 4 ).toDouble( &ok );
353 if ( lineParts.count() > 5 )
355 label = lineParts.mid( 5 ).join(
' ' );
358 classes <<
Class( value, c, label );
369 QFile inputFile( path );
371 if ( inputFile.open( QIODevice::ReadOnly ) )
373 QTextStream in( &inputFile );
374 input = in.readAll();
385 std::sort( cd.begin(), cd.end(), [](
const Class & a,
const Class & b ) ->
bool 387 return a.
value < b.value;
390 Q_FOREACH (
const Class &
c, cd )
392 out << QStringLiteral(
"%1 %2 %3 %4 %5 %6" ).arg( c.
value ).arg( c.
color.red() )
395 return out.join(
'\n' );
411 int bins = std::ceil( max - min ) + 1;
423 double currentValue = histogram.
minimum;
424 double presentValues = 0;
425 for (
int idx = 0; idx < histogram.
binCount; ++idx )
430 data <<
Class( currentValue, QColor(), QString::number( currentValue ) );
433 currentValue += interval;
445 randomRamp->setTotalColorCount( data.count() );
448 if ( presentValues > 1 )
451 QgsPalettedRasterRenderer::ClassData::iterator cIt = data.begin();
452 for ( ; cIt != data.end(); ++cIt )
454 cIt->color = ramp->
color( i / presentValues );
461 void QgsPalettedRasterRenderer::updateArrays()
465 ClassData::const_iterator it = mClassData.constBegin();
466 for ( ; it != mClassData.constEnd(); ++it )
468 mColors[it->value] = qPremultiply( it->color.rgba() );
void setSourceColorRamp(QgsColorRamp *ramp)
Set the source color ramp.
static QString classDataToString(const QgsPalettedRasterRenderer::ClassData &classes)
Converts classes to a string representation, using the .clr/gdal color table file format...
A rectangle specified with double values.
QgsPalettedRasterRenderer(QgsRasterInterface *input, int bandNumber, const ClassData &classes)
Constructor for QgsPalettedRasterRenderer.
QColor color
Color to render value.
Renderer for paletted raster images.
virtual QgsRectangle extent() const
Gets the extent of the interface.
QString label
Label for value.
Abstract base class for color ramps.
double minimum
The minimum histogram value.
virtual QgsRasterInterface * input() const
Current input.
ClassData classes() const
Returns a map of value to classes (colors) used by the renderer.
Properties of a single value class.
static QDomElement saveColorRamp(const QString &name, QgsColorRamp *ramp, QDomDocument &doc)
Encodes a color ramp's settings to an XML element.
void setLabel(int idx, const QString &label)
Set category label.
double maximumValue
The maximum cell value in the raster band.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
virtual QgsRasterHistogram histogram(int bandNo, int binCount=0, double minimum=std::numeric_limits< double >::quiet_NaN(), double maximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, bool includeOutOfRange=false, QgsRasterBlockFeedback *feedback=nullptr)
Returns a band histogram.
virtual QColor color(double value) const =0
Returns the color corresponding to a specified value.
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
static const QRgb NODATA_COLOR
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
The RasterBandStats struct is a container for statistics about a single raster band.
#define QgsDebugMsgLevel(str, level)
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.
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of value to class properties.
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses) ...
static QgsPalettedRasterRenderer::ClassData classDataFromString(const QString &string)
Converts a string containing a color table or class data to to paletted renderer class data...
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
bool usesTransparency() const
QgsRasterHistogram::HistogramVector histogramVector
Store the histogram for a given layer.
static QgsColorRamp * loadColorRamp(QDomElement &element)
Creates a color ramp from the settings encoded in an XML element.
QString label(int idx) const
Returns optional category label.
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.
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...
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Totally random color ramp.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
double maximum
The maximum histogram value.
QgsColorRamp * sourceColorRamp() const
Gets the source color ramp.
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
QgsPalettedRasterRenderer * clone() const override
Clone itself, create deep copy.
static QgsPalettedRasterRenderer::ClassData classDataFromFile(const QString &path)
Opens a color table file and returns corresponding paletted renderer class data.
static QgsPalettedRasterRenderer::ClassData classDataFromRaster(QgsRasterInterface *raster, int bandNumber, QgsColorRamp *ramp=nullptr, QgsRasterBlockFeedback *feedback=nullptr)
Generates class data from a raster, for the specified bandNumber.
bool isCanceled() const
Tells whether the operation has been canceled already.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Gets symbology items if provided by renderer.
double minimumValue
The minimum cell value in the raster band.
The QgsRasterHistogram is a container for histogram of a single raster band.
int binCount
Number of bins (intervals,buckets) in histogram.
double mOpacity
Global alpha value (0-1)
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
static QgsPalettedRasterRenderer::ClassData colorTableToClassData(const QList< QgsColorRampShader::ColorRampItem > &table)
Converts a raster color table to paletted renderer class data.
Raster renderer pipe that applies colors to a raster.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.