23QString QgsRasterStatisticsAlgorithm::name()
const
25 return QStringLiteral(
"rasterlayerstatistics" );
28QString QgsRasterStatisticsAlgorithm::displayName()
const
30 return QObject::tr(
"Raster layer statistics" );
33QStringList QgsRasterStatisticsAlgorithm::tags()
const
35 return QObject::tr(
"raster,stats,statistics,maximum,minimum,range,sum,mean,standard,deviation,summary" ).split(
',' );
38QString QgsRasterStatisticsAlgorithm::group()
const
40 return QObject::tr(
"Raster analysis" );
43QString QgsRasterStatisticsAlgorithm::groupId()
const
45 return QStringLiteral(
"rasteranalysis" );
48QString QgsRasterStatisticsAlgorithm::shortHelpString()
const
50 return QObject::tr(
"This algorithm computes basic statistics from the values in a given band of the raster layer." );
53QgsRasterStatisticsAlgorithm *QgsRasterStatisticsAlgorithm::createInstance()
const
55 return new QgsRasterStatisticsAlgorithm();
58void QgsRasterStatisticsAlgorithm::initAlgorithm(
const QVariantMap & )
62 QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT" ) ) );
65 QObject::tr(
"HTML files (*.html *.HTML)" ), QVariant(),
true ) );
78 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
83 const int band = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
84 if ( band < 1 || band > layer->
bandCount() )
85 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band )
88 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT_HTML_FILE" ), context );
93 outputs.insert( QStringLiteral(
"COUNT" ), stat.
elementCount );
94 outputs.insert( QStringLiteral(
"MIN" ), stat.
minimumValue );
95 outputs.insert( QStringLiteral(
"MAX" ), stat.
maximumValue );
96 outputs.insert( QStringLiteral(
"RANGE" ), stat.
range );
97 outputs.insert( QStringLiteral(
"SUM" ), stat.
sum );
98 outputs.insert( QStringLiteral(
"MEAN" ), stat.
mean );
99 outputs.insert( QStringLiteral(
"STD_DEV" ), stat.
stdDev );
100 outputs.insert( QStringLiteral(
"SUM_OF_SQUARES" ), stat.
sumOfSquares );
102 if ( !outputFile.isEmpty() )
104 QFile file( outputFile );
105 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
107 QTextStream out( &file );
108#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
109 out.setCodec(
"UTF-8" );
111 out << QStringLiteral(
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
112 out << QObject::tr(
"<p>Analyzed file: %1 (band %2)</p>\n" ).arg( layer->
source() ).arg( band );
113 out << QObject::tr(
"<p>Count of non-NoData pixels: %1</p>\n" ).arg( stat.
elementCount );
114 out << QObject::tr(
"<p>Minimum value: %1</p>\n" ).arg( stat.
minimumValue, 0,
'g', 16 );
115 out << QObject::tr(
"<p>Maximum value: %1</p>\n" ).arg( stat.
maximumValue, 0,
'g', 16 );
116 out << QObject::tr(
"<p>Range: %1</p>\n" ).arg( stat.
range, 0,
'g', 16 );
117 out << QObject::tr(
"<p>Sum: %1</p>\n" ).arg( stat.
sum, 0,
'g', 16 );
118 out << QObject::tr(
"<p>Mean value: %1</p>\n" ).arg( stat.
mean, 0,
'g', 16 );
119 out << QObject::tr(
"<p>Standard deviation: %1</p>\n" ).arg( stat.
stdDev, 0,
'g', 16 );
120 out << QObject::tr(
"<p>Sum of the squares: %1</p>\n" ).arg( stat.
sumOfSquares, 0,
'g', 16 );
121 out << QStringLiteral(
"</body></html>" );
123 outputs.insert( QStringLiteral(
"OUTPUT_HTML_FILE" ), outputFile );
@ All
All available statistics.
QString source() const
Returns the source for the layer.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A numeric output for processing algorithms.
A raster band parameter for Processing algorithms.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A raster layer parameter for processing algorithms.
The RasterBandStats struct is a container for statistics about a single raster band.
qgssize elementCount
The number of not no data cells in the band.
double sumOfSquares
The sum of the squares. Used to calculate standard deviation.
double mean
The mean cell value for the band. NO_DATA values are excluded.
double stdDev
The standard deviation of the cell values.
double minimumValue
The minimum cell value in the raster band.
double sum
The sum of all cells in the band. NO_DATA values are excluded.
double maximumValue
The maximum cell value in the raster band.
double range
The range is the distance between min & max.
Q_DECL_DEPRECATED QgsRasterBandStats bandStatistics(int bandNo, int stats, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
Represents a raster layer.
int bandCount() const
Returns the number of bands in this layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
A rectangle specified with double values.