Skip to content

Commit

Permalink
[processing] port raster layer statistics to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Dec 9, 2019
1 parent 2ff85f9 commit b85fe90
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 131 deletions.
3 changes: 0 additions & 3 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -346,9 +346,6 @@ qgis:rasterlayerhistogram: >

The raster layer must have a single band.

qgis:rasterlayerstatistics: >
This algorithm computes basic statistics from the values in a given band of the raster layer.

qgis:rastersampling: >
This algorithm creates a new vector layer with the same attributes of the input layer and the raster values corresponding on the point location.

Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -77,7 +77,6 @@
from .RandomSelectionWithinSubsets import RandomSelectionWithinSubsets
from .RasterCalculator import RasterCalculator
from .RasterLayerHistogram import RasterLayerHistogram
from .RasterLayerStatistics import RasterLayerStatistics
from .RasterSampling import RasterSampling
from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
Expand Down Expand Up @@ -165,7 +164,6 @@ def getAlgs(self):
RandomSelectionWithinSubsets(),
RasterCalculator(),
RasterLayerHistogram(),
RasterLayerStatistics(),
RasterSampling(),
RectanglesOvalsDiamondsFixed(),
RectanglesOvalsDiamondsVariable(),
Expand Down
119 changes: 0 additions & 119 deletions python/plugins/processing/algs/qgis/RasterLayerStatistics.py

This file was deleted.

Expand Up @@ -647,7 +647,7 @@ tests:
hash: f09384c64f56286ec4146a7b9a679cea7c6711ec4c7d77eec054e364
type: rasterhash

- algorithm: qgis:rasterlayerstatistics
- algorithm: native:rasterlayerstatistics
name: Raster layer statistics
params:
INPUT:
Expand All @@ -659,13 +659,13 @@ tests:
name: raster_statistics.html
type: regex
rules:
- 'Minimum value: 85.0'
- 'Maximum value: 243.0'
- 'Range: 158.0'
- 'Sum: 19213301.982429504'
- 'Mean value: 147.17197994967066'
- 'Minimum value: 85'
- 'Maximum value: 243'
- 'Range: 158'
- 'Sum: 19213301.9824295'
- 'Mean value: 147.1719799496707'
- 'Standard deviation: 43.9618116337985'
- 'Sum of the squares: 252304334.52061242'
- 'Sum of the squares: 252304334.5206124'

- algorithm: qgis:rasterlayeruniquevaluesreport
name: Raster layer unique values report
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -105,6 +105,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmrasterlayeruniquevalues.cpp
processing/qgsalgorithmrasterlogicalop.cpp
processing/qgsalgorithmrasterize.cpp
processing/qgsalgorithmrasterstatistics.cpp
processing/qgsalgorithmrastersurfacevolume.cpp
processing/qgsalgorithmrasterzonalstats.cpp
processing/qgsalgorithmreclassifybylayer.cpp
Expand Down
124 changes: 124 additions & 0 deletions src/analysis/processing/qgsalgorithmrasterstatistics.cpp
@@ -0,0 +1,124 @@
/***************************************************************************
qgsalgorithmrasterstatistics.cpp
---------------------
begin : December 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsalgorithmrasterstatistics.h"


///@cond PRIVATE

QString QgsRasterStatisticsAlgorithm::name() const
{
return QStringLiteral( "rasterlayerstatistics" );
}

QString QgsRasterStatisticsAlgorithm::displayName() const
{
return QObject::tr( "Raster layer statistics" );
}

QStringList QgsRasterStatisticsAlgorithm::tags() const
{
return QObject::tr( "raster,stats,statistics,maximum,minimum,range,sum,mean,standard,deviation,summary" ).split( ',' );
}

QString QgsRasterStatisticsAlgorithm::group() const
{
return QObject::tr( "Raster analysis" );
}

QString QgsRasterStatisticsAlgorithm::groupId() const
{
return QStringLiteral( "rasteranalysis" );
}

QString QgsRasterStatisticsAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm computes basic statistics from the values in a given band of the raster layer." );
}

QgsRasterStatisticsAlgorithm *QgsRasterStatisticsAlgorithm::createInstance() const
{
return new QgsRasterStatisticsAlgorithm();
}

void QgsRasterStatisticsAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ),
QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT" ) ) );

addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT_HTML_FILE" ), QObject::tr( "Statistics" ),
QObject::tr( "HTML files (*.html *.HTML)" ), QVariant(), true ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MIN" ), QObject::tr( "Minimum value" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MAX" ), QObject::tr( "Maximum value" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "RANGE" ), QObject::tr( "Range" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "SUM" ), QObject::tr( "Sum" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MEAN" ), QObject::tr( "Mean value" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "STD_DEV" ), QObject::tr( "Standard deviation" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "SUM_OF_SQUARES" ), QObject::tr( "Sum of the squares" ) ) );
}

QVariantMap QgsRasterStatisticsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );

if ( !layer )
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );

int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
if ( band < 1 || band > layer->bandCount() )
throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band )
.arg( layer->bandCount() ) );

QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context );

QgsRasterBandStats stat = layer->dataProvider()->bandStatistics( band, QgsRasterBandStats::All, QgsRectangle(), 0 );

QVariantMap outputs;
outputs.insert( QStringLiteral( "MIN" ), stat.minimumValue );
outputs.insert( QStringLiteral( "MAX" ), stat.maximumValue );
outputs.insert( QStringLiteral( "RANGE" ), stat.range );
outputs.insert( QStringLiteral( "SUM" ), stat.sum );
outputs.insert( QStringLiteral( "MEAN" ), stat.mean );
outputs.insert( QStringLiteral( "STD_DEV" ), stat.stdDev );
outputs.insert( QStringLiteral( "SUM_OF_SQUARES" ), stat.sumOfSquares );

if ( !outputFile.isEmpty() )
{
QFile file( outputFile );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
QTextStream out( &file );
out << QStringLiteral( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
out << QObject::tr( "<p>Analyzed file: %1 (band %1)</p>\n" ).arg( layer->source() ).arg( band );
out << QObject::tr( "<p>Minimum value: %1</p>\n" ).arg( stat.minimumValue, 0, 'g', 16 );
out << QObject::tr( "<p>Maxumum value: %1</p>\n" ).arg( stat.maximumValue, 0, 'g', 16 );
out << QObject::tr( "<p>Range: %1</p>\n" ).arg( stat.range, 0, 'g', 16 );
out << QObject::tr( "<p>Sum: %1</p>\n" ).arg( stat.sum, 0, 'g', 16 );
out << QObject::tr( "<p>Mean value: %1</p>\n" ).arg( stat.mean, 0, 'g', 16 );
out << QObject::tr( "<p>Standard deviation: %1</p>\n" ).arg( stat.stdDev, 0, 'g', 16 );
out << QObject::tr( "<p>Sum of the squares: %1</p>\n" ).arg( stat.sumOfSquares, 0, 'g', 16 );
out << QStringLiteral( "</body></html>" );

outputs.insert( QStringLiteral( "OUTPUT_HTML_FILE" ), outputFile );
}
}

return outputs;
}

///@endcond
54 changes: 54 additions & 0 deletions src/analysis/processing/qgsalgorithmrasterstatistics.h
@@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmrasterstatistics.h
------------------------------
begin : December 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSALGORITHMRASTERSTATISTICS_H
#define QGSALGORITHMRASTERSTATISTICS_H

#define SIP_NO_FILE

#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"

///@cond PRIVATE

/**
* Native raster statistics algorithm.
*/
class QgsRasterStatisticsAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsRasterStatisticsAlgorithm() = default;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QgsRasterStatisticsAlgorithm *createInstance() const override SIP_FACTORY;

protected:

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

///@endcond PRIVATE

#endif // QGSALGORITHMRASTERSTATISTICS_H
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -99,6 +99,7 @@
#include "qgsalgorithmrasterlayeruniquevalues.h"
#include "qgsalgorithmrasterlogicalop.h"
#include "qgsalgorithmrasterize.h"
#include "qgsalgorithmrasterstatistics.h"
#include "qgsalgorithmrastersurfacevolume.h"
#include "qgsalgorithmrasterzonalstats.h"
#include "qgsalgorithmreclassifybylayer.h"
Expand Down Expand Up @@ -285,6 +286,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsRasterizeAlgorithm() );
addAlgorithm( new QgsRasterPixelsToPointsAlgorithm() );
addAlgorithm( new QgsRasterPixelsToPolygonsAlgorithm() );
addAlgorithm( new QgsRasterStatisticsAlgorithm() );
addAlgorithm( new QgsRasterSurfaceVolumeAlgorithm() );
addAlgorithm( new QgsAlgorithmRemoveDuplicateVertices() );
addAlgorithm( new QgsReclassifyByLayerAlgorithm() );
Expand Down

0 comments on commit b85fe90

Please sign in to comment.