QGIS API Documentation 3.43.0-Master (261ee7da134)
qgsalgorithmrasterstatistics.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrasterstatistics.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include <QTextStream>
20
22
23QString QgsRasterStatisticsAlgorithm::name() const
24{
25 return QStringLiteral( "rasterlayerstatistics" );
26}
27
28QString QgsRasterStatisticsAlgorithm::displayName() const
29{
30 return QObject::tr( "Raster layer statistics" );
31}
32
33QStringList QgsRasterStatisticsAlgorithm::tags() const
34{
35 return QObject::tr( "raster,stats,statistics,maximum,minimum,range,sum,mean,standard,deviation,summary" ).split( ',' );
36}
37
38QString QgsRasterStatisticsAlgorithm::group() const
39{
40 return QObject::tr( "Raster analysis" );
41}
42
43QString QgsRasterStatisticsAlgorithm::groupId() const
44{
45 return QStringLiteral( "rasteranalysis" );
46}
47
48QString QgsRasterStatisticsAlgorithm::shortHelpString() const
49{
50 return QObject::tr( "This algorithm computes basic statistics from the values in a given band of the raster layer." );
51}
52
53QString QgsRasterStatisticsAlgorithm::shortDescription() const
54{
55 return QObject::tr( "Computes basic statistics from the values in a given band of the raster layer." );
56}
57
58QgsRasterStatisticsAlgorithm *QgsRasterStatisticsAlgorithm::createInstance() const
59{
60 return new QgsRasterStatisticsAlgorithm();
61}
62
63void QgsRasterStatisticsAlgorithm::initAlgorithm( const QVariantMap & )
64{
65 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
66 addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ), QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT" ) ) );
67
68 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT_HTML_FILE" ), QObject::tr( "Statistics" ), QObject::tr( "HTML files (*.html *.HTML)" ), QVariant(), true ) );
69 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "COUNT" ), QObject::tr( "Count of non-NoData pixels" ) ) );
70 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MIN" ), QObject::tr( "Minimum value" ) ) );
71 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MAX" ), QObject::tr( "Maximum value" ) ) );
72 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "RANGE" ), QObject::tr( "Range" ) ) );
73 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "SUM" ), QObject::tr( "Sum" ) ) );
74 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MEAN" ), QObject::tr( "Mean value" ) ) );
75 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "STD_DEV" ), QObject::tr( "Standard deviation" ) ) );
76 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "SUM_OF_SQUARES" ), QObject::tr( "Sum of the squares" ) ) );
77}
78
79QVariantMap QgsRasterStatisticsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
80{
81 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
82
83 if ( !layer )
84 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
85
86 const int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
87 if ( band < 1 || band > layer->bandCount() )
88 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band ).arg( layer->bandCount() ) );
89
90 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context );
91
93
94 QVariantMap outputs;
95 outputs.insert( QStringLiteral( "COUNT" ), stat.elementCount );
96 outputs.insert( QStringLiteral( "MIN" ), stat.minimumValue );
97 outputs.insert( QStringLiteral( "MAX" ), stat.maximumValue );
98 outputs.insert( QStringLiteral( "RANGE" ), stat.range );
99 outputs.insert( QStringLiteral( "SUM" ), stat.sum );
100 outputs.insert( QStringLiteral( "MEAN" ), stat.mean );
101 outputs.insert( QStringLiteral( "STD_DEV" ), stat.stdDev );
102 outputs.insert( QStringLiteral( "SUM_OF_SQUARES" ), stat.sumOfSquares );
103
104 if ( !outputFile.isEmpty() )
105 {
106 QFile file( outputFile );
107 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
108 {
109 QTextStream out( &file );
110#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
111 out.setCodec( "UTF-8" );
112#endif
113 out << QStringLiteral( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
114 out << QObject::tr( "<p>Analyzed file: %1 (band %2)</p>\n" ).arg( layer->source() ).arg( band );
115 out << QObject::tr( "<p>Count of non-NoData pixels: %1</p>\n" ).arg( stat.elementCount );
116 out << QObject::tr( "<p>Minimum value: %1</p>\n" ).arg( stat.minimumValue, 0, 'g', 16 );
117 out << QObject::tr( "<p>Maximum value: %1</p>\n" ).arg( stat.maximumValue, 0, 'g', 16 );
118 out << QObject::tr( "<p>Range: %1</p>\n" ).arg( stat.range, 0, 'g', 16 );
119 out << QObject::tr( "<p>Sum: %1</p>\n" ).arg( stat.sum, 0, 'g', 16 );
120 out << QObject::tr( "<p>Mean value: %1</p>\n" ).arg( stat.mean, 0, 'g', 16 );
121 out << QObject::tr( "<p>Standard deviation: %1</p>\n" ).arg( stat.stdDev, 0, 'g', 16 );
122 out << QObject::tr( "<p>Sum of the squares: %1</p>\n" ).arg( stat.sumOfSquares, 0, 'g', 16 );
123 out << QStringLiteral( "</body></html>" );
124
125 outputs.insert( QStringLiteral( "OUTPUT_HTML_FILE" ), outputFile );
126 }
127 }
128
129 return outputs;
130}
131
@ 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.