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