QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
20 
22 
23 QString QgsRasterStatisticsAlgorithm::name() const
24 {
25  return QStringLiteral( "rasterlayerstatistics" );
26 }
27 
28 QString QgsRasterStatisticsAlgorithm::displayName() const
29 {
30  return QObject::tr( "Raster layer statistics" );
31 }
32 
33 QStringList QgsRasterStatisticsAlgorithm::tags() const
34 {
35  return QObject::tr( "raster,stats,statistics,maximum,minimum,range,sum,mean,standard,deviation,summary" ).split( ',' );
36 }
37 
38 QString QgsRasterStatisticsAlgorithm::group() const
39 {
40  return QObject::tr( "Raster analysis" );
41 }
42 
43 QString QgsRasterStatisticsAlgorithm::groupId() const
44 {
45  return QStringLiteral( "rasteranalysis" );
46 }
47 
48 QString 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 
53 QgsRasterStatisticsAlgorithm *QgsRasterStatisticsAlgorithm::createInstance() const
54 {
55  return new QgsRasterStatisticsAlgorithm();
56 }
57 
58 void QgsRasterStatisticsAlgorithm::initAlgorithm( const QVariantMap & )
59 {
60  addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
61  addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ),
62  QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT" ) ) );
63 
64  addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT_HTML_FILE" ), QObject::tr( "Statistics" ),
65  QObject::tr( "HTML files (*.html *.HTML)" ), QVariant(), true ) );
66  addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MIN" ), QObject::tr( "Minimum value" ) ) );
67  addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MAX" ), QObject::tr( "Maximum value" ) ) );
68  addOutput( new QgsProcessingOutputNumber( QStringLiteral( "RANGE" ), QObject::tr( "Range" ) ) );
69  addOutput( new QgsProcessingOutputNumber( QStringLiteral( "SUM" ), QObject::tr( "Sum" ) ) );
70  addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MEAN" ), QObject::tr( "Mean value" ) ) );
71  addOutput( new QgsProcessingOutputNumber( QStringLiteral( "STD_DEV" ), QObject::tr( "Standard deviation" ) ) );
72  addOutput( new QgsProcessingOutputNumber( QStringLiteral( "SUM_OF_SQUARES" ), QObject::tr( "Sum of the squares" ) ) );
73 }
74 
75 QVariantMap QgsRasterStatisticsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
76 {
77  QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
78 
79  if ( !layer )
80  throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
81 
82  int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
83  if ( band < 1 || band > layer->bandCount() )
84  throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band )
85  .arg( layer->bandCount() ) );
86 
87  QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context );
88 
90 
91  QVariantMap outputs;
92  outputs.insert( QStringLiteral( "MIN" ), stat.minimumValue );
93  outputs.insert( QStringLiteral( "MAX" ), stat.maximumValue );
94  outputs.insert( QStringLiteral( "RANGE" ), stat.range );
95  outputs.insert( QStringLiteral( "SUM" ), stat.sum );
96  outputs.insert( QStringLiteral( "MEAN" ), stat.mean );
97  outputs.insert( QStringLiteral( "STD_DEV" ), stat.stdDev );
98  outputs.insert( QStringLiteral( "SUM_OF_SQUARES" ), stat.sumOfSquares );
99 
100  if ( !outputFile.isEmpty() )
101  {
102  QFile file( outputFile );
103  if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
104  {
105  QTextStream out( &file );
106  out << QStringLiteral( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
107  out << QObject::tr( "<p>Analyzed file: %1 (band %2)</p>\n" ).arg( layer->source() ).arg( band );
108  out << QObject::tr( "<p>Minimum value: %1</p>\n" ).arg( stat.minimumValue, 0, 'g', 16 );
109  out << QObject::tr( "<p>Maximum value: %1</p>\n" ).arg( stat.maximumValue, 0, 'g', 16 );
110  out << QObject::tr( "<p>Range: %1</p>\n" ).arg( stat.range, 0, 'g', 16 );
111  out << QObject::tr( "<p>Sum: %1</p>\n" ).arg( stat.sum, 0, 'g', 16 );
112  out << QObject::tr( "<p>Mean value: %1</p>\n" ).arg( stat.mean, 0, 'g', 16 );
113  out << QObject::tr( "<p>Standard deviation: %1</p>\n" ).arg( stat.stdDev, 0, 'g', 16 );
114  out << QObject::tr( "<p>Sum of the squares: %1</p>\n" ).arg( stat.sumOfSquares, 0, 'g', 16 );
115  out << QStringLiteral( "</body></html>" );
116 
117  outputs.insert( QStringLiteral( "OUTPUT_HTML_FILE" ), outputFile );
118  }
119  }
120 
121  return outputs;
122 }
123 
QgsRasterLayer::bandCount
int bandCount() const
Returns the number of bands in this layer.
Definition: qgsrasterlayer.cpp:216
QgsRasterInterface::bandStatistics
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
Definition: qgsrasterinterface.cpp:116
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsRasterBandStats
Definition: qgsrasterbandstats.h:34
QgsRasterBandStats::sumOfSquares
double sumOfSquares
The sum of the squares. Used to calculate standard deviation.
Definition: qgsrasterbandstats.h:122
QgsRasterBandStats::mean
double mean
The mean cell value for the band. NO_DATA values are excluded.
Definition: qgsrasterbandstats.h:107
QgsRasterBandStats::range
double range
The range is the distance between min & max.
Definition: qgsrasterbandstats.h:110
QgsRectangle
Definition: qgsrectangle.h:41
qgsalgorithmrasterstatistics.h
QgsProcessingOutputNumber
Definition: qgsprocessingoutputs.h:294
QgsRasterBandStats::maximumValue
double maximumValue
The maximum cell value in the raster band.
Definition: qgsrasterbandstats.h:99
QgsRasterBandStats::All
@ All
Definition: qgsrasterbandstats.h:73
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingParameterFileDestination
Definition: qgsprocessingparameters.h:3005
QgsProcessingParameterRasterLayer
Definition: qgsprocessingparameters.h:2101
QgsRasterLayer
Definition: qgsrasterlayer.h:72
QgsRasterBandStats::sum
double sum
The sum of all cells in the band. NO_DATA values are excluded.
Definition: qgsrasterbandstats.h:119
QgsRasterBandStats::minimumValue
double minimumValue
The minimum cell value in the raster band.
Definition: qgsrasterbandstats.h:104
QgsMapLayer::source
QString source() const
Returns the source for the layer.
Definition: qgsmaplayer.cpp:192
QgsProcessingParameterBand
Definition: qgsprocessingparameters.h:3103
QgsRasterBandStats::stdDev
double stdDev
The standard deviation of the cell values.
Definition: qgsrasterbandstats.h:113
QgsProcessingException
Definition: qgsexception.h:82
QgsRasterLayer::dataProvider
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Definition: qgsrasterlayer.cpp:233