QGIS API Documentation 4.3.0-Master (45633be667c)
Loading...
Searching...
No Matches
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#include <QString>
21#include <QTextStream>
22
23using namespace Qt::StringLiterals;
24
26
27QString QgsRasterStatisticsAlgorithm::name() const
28{
29 return u"rasterlayerstatistics"_s;
30}
31
32QString QgsRasterStatisticsAlgorithm::displayName() const
33{
34 return QObject::tr( "Raster layer statistics" );
35}
36
37QStringList QgsRasterStatisticsAlgorithm::tags() const
38{
39 return QObject::tr( "raster,stats,statistics,maximum,minimum,range,sum,mean,standard,deviation,summary" ).split( ',' );
40}
41
42QString QgsRasterStatisticsAlgorithm::group() const
43{
44 return QObject::tr( "Raster analysis" );
45}
46
47QString QgsRasterStatisticsAlgorithm::groupId() const
48{
49 return u"rasteranalysis"_s;
50}
51
52QString QgsRasterStatisticsAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm computes basic statistics from the values in a given band of the raster layer." );
55}
56
57QString QgsRasterStatisticsAlgorithm::shortDescription() const
58{
59 return QObject::tr( "Computes basic statistics from the values in a given band of the raster layer." );
60}
61
62QgsRasterStatisticsAlgorithm *QgsRasterStatisticsAlgorithm::createInstance() const
63{
64 return new QgsRasterStatisticsAlgorithm();
65}
66
67void QgsRasterStatisticsAlgorithm::initAlgorithm( const QVariantMap & )
68{
69 addParameter( new QgsProcessingParameterRasterLayer( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
70 addParameter( new QgsProcessingParameterBand( u"BAND"_s, QObject::tr( "Band number" ), 1, u"INPUT"_s ) );
71
72 addParameter( new QgsProcessingParameterFileDestination( u"OUTPUT_HTML_FILE"_s, QObject::tr( "Statistics" ), QObject::tr( "HTML files (*.html *.HTML)" ), QVariant(), true ) );
73 addOutput( new QgsProcessingOutputNumber( u"COUNT"_s, QObject::tr( "Count of non-NoData pixels" ) ) );
74 addOutput( new QgsProcessingOutputNumber( u"MIN"_s, QObject::tr( "Minimum value" ) ) );
75 addOutput( new QgsProcessingOutputNumber( u"MAX"_s, QObject::tr( "Maximum value" ) ) );
76 addOutput( new QgsProcessingOutputNumber( u"RANGE"_s, QObject::tr( "Range" ) ) );
77 addOutput( new QgsProcessingOutputNumber( u"SUM"_s, QObject::tr( "Sum" ) ) );
78 addOutput( new QgsProcessingOutputNumber( u"MEAN"_s, QObject::tr( "Mean value" ) ) );
79 addOutput( new QgsProcessingOutputNumber( u"STD_DEV"_s, QObject::tr( "Standard deviation" ) ) );
80 addOutput( new QgsProcessingOutputNumber( u"SUM_OF_SQUARES"_s, QObject::tr( "Sum of the squares" ) ) );
81}
82
83QVariantMap QgsRasterStatisticsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
84{
85 QGS_MARK_ALGORITHM_SOURCE
86
87 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u"INPUT"_s, context );
88
89 if ( !layer )
90 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT"_s ) );
91
92 const int band = parameterAsInt( parameters, u"BAND"_s, context );
93 if ( band < 1 || band > layer->bandCount() )
94 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band ).arg( layer->bandCount() ) );
95
96 const QString outputFile = parameterAsFileOutput( parameters, u"OUTPUT_HTML_FILE"_s, context );
97
98 const QgsRasterBandStats stat = layer->dataProvider()->bandStatistics( band, Qgis::RasterBandStatistic::All, QgsRectangle(), 0 );
99
100 QVariantMap outputs;
101 outputs.insert( u"COUNT"_s, stat.elementCount );
102 outputs.insert( u"MIN"_s, stat.minimumValue );
103 outputs.insert( u"MAX"_s, stat.maximumValue );
104 outputs.insert( u"RANGE"_s, stat.range );
105 outputs.insert( u"SUM"_s, stat.sum );
106 outputs.insert( u"MEAN"_s, stat.mean );
107 outputs.insert( u"STD_DEV"_s, stat.stdDev );
108 outputs.insert( u"SUM_OF_SQUARES"_s, stat.sumOfSquares );
109
110 if ( !outputFile.isEmpty() )
111 {
112 QFile file( outputFile );
113 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
114 {
115 QTextStream out( &file );
116 out << u"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n"_s;
117 out << QObject::tr( "<p>Analyzed file: %1 (band %2)</p>\n" ).arg( layer->source() ).arg( band );
118 out << QObject::tr( "<p>Count of non-NoData pixels: %1</p>\n" ).arg( stat.elementCount );
119 out << QObject::tr( "<p>Minimum value: %1</p>\n" ).arg( stat.minimumValue, 0, 'g', 16 );
120 out << QObject::tr( "<p>Maximum value: %1</p>\n" ).arg( stat.maximumValue, 0, 'g', 16 );
121 out << QObject::tr( "<p>Range: %1</p>\n" ).arg( stat.range, 0, 'g', 16 );
122 out << QObject::tr( "<p>Sum: %1</p>\n" ).arg( stat.sum, 0, 'g', 16 );
123 out << QObject::tr( "<p>Mean value: %1</p>\n" ).arg( stat.mean, 0, 'g', 16 );
124 out << QObject::tr( "<p>Standard deviation: %1</p>\n" ).arg( stat.stdDev, 0, 'g', 16 );
125 out << QObject::tr( "<p>Sum of the squares: %1</p>\n" ).arg( stat.sumOfSquares, 0, 'g', 16 );
126 out << u"</body></html>"_s;
127
128 outputs.insert( u"OUTPUT_HTML_FILE"_s, outputFile );
129 }
130 }
131
132 return outputs;
133}
134
@ All
All available statistics.
Definition qgis.h:6738
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.
Represents a raster layer.
A rectangle specified with double values.