19 #include <QTextStream>
23 QString QgsRasterStatisticsAlgorithm::name()
const
25 return QStringLiteral(
"rasterlayerstatistics" );
28 QString QgsRasterStatisticsAlgorithm::displayName()
const
30 return QObject::tr(
"Raster layer statistics" );
33 QStringList QgsRasterStatisticsAlgorithm::tags()
const
35 return QObject::tr(
"raster,stats,statistics,maximum,minimum,range,sum,mean,standard,deviation,summary" ).split(
',' );
38 QString QgsRasterStatisticsAlgorithm::group()
const
40 return QObject::tr(
"Raster analysis" );
43 QString QgsRasterStatisticsAlgorithm::groupId()
const
45 return QStringLiteral(
"rasteranalysis" );
48 QString QgsRasterStatisticsAlgorithm::shortHelpString()
const
50 return QObject::tr(
"This algorithm computes basic statistics from the values in a given band of the raster layer." );
53 QgsRasterStatisticsAlgorithm *QgsRasterStatisticsAlgorithm::createInstance()
const
55 return new QgsRasterStatisticsAlgorithm();
58 void QgsRasterStatisticsAlgorithm::initAlgorithm(
const QVariantMap & )
62 QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT" ) ) );
65 QObject::tr(
"HTML files (*.html *.HTML)" ), QVariant(),
true ) );
77 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
82 const 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 )
87 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT_HTML_FILE" ), context );
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 );
100 if ( !outputFile.isEmpty() )
102 QFile file( outputFile );
103 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
105 QTextStream out( &file );
106 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
107 out.setCodec(
"UTF-8" );
109 out << QStringLiteral(
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
110 out << QObject::tr(
"<p>Analyzed file: %1 (band %2)</p>\n" ).arg( layer->
source() ).arg( band );
111 out << QObject::tr(
"<p>Minimum value: %1</p>\n" ).arg( stat.
minimumValue, 0,
'g', 16 );
112 out << QObject::tr(
"<p>Maximum value: %1</p>\n" ).arg( stat.
maximumValue, 0,
'g', 16 );
113 out << QObject::tr(
"<p>Range: %1</p>\n" ).arg( stat.
range, 0,
'g', 16 );
114 out << QObject::tr(
"<p>Sum: %1</p>\n" ).arg( stat.
sum, 0,
'g', 16 );
115 out << QObject::tr(
"<p>Mean value: %1</p>\n" ).arg( stat.
mean, 0,
'g', 16 );
116 out << QObject::tr(
"<p>Standard deviation: %1</p>\n" ).arg( stat.
stdDev, 0,
'g', 16 );
117 out << QObject::tr(
"<p>Sum of the squares: %1</p>\n" ).arg( stat.
sumOfSquares, 0,
'g', 16 );
118 out << QStringLiteral(
"</body></html>" );
120 outputs.insert( QStringLiteral(
"OUTPUT_HTML_FILE" ), outputFile );