Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #38650 from m-kuhn/zonal_stats_new_layer
[processing] Add Zonal statistics algorithm that creates a new output
- Loading branch information
Showing
11 changed files
with
520 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
python/plugins/processing/tests/testdata/expected/stats.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"name": "stats", | ||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, | ||
"features": [ | ||
{ "type": "Feature", "properties": { "fid": "polygon_mask.0", "stats_count": 24882.0, "stats_sum": 89005.0, "stats_mean": 3.5770838357045251, "stats_median": 6.0, "stats_stdev": 2.4739349627559846, "stats_min": 1.0, "stats_max": 6.0, "stats_range": 5.0, "stats_minority": 4.0, "stats_majority": 6.0, "stats_variety": 4.0, "stats_variance": 6.1203541999464539 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 18.6744202640594, 45.798496472749385 ], [ 18.683204095342351, 45.807861586984885 ], [ 18.68966279481511, 45.80456765025378 ], [ 18.694829754393322, 45.800175734612303 ], [ 18.694442232424951, 45.796235927933921 ], [ 18.683849965289628, 45.790358511413707 ], [ 18.675970351932861, 45.790229337424257 ], [ 18.673193111159573, 45.789970989445344 ], [ 18.6744202640594, 45.798496472749385 ] ] ] ] } }, | ||
{ "type": "Feature", "properties": { "fid": "polygon_mask.1", "stats_count": 14055.0, "stats_sum": 42165.0, "stats_mean": 3.0, "stats_median": 3.0, "stats_stdev": 0.0, "stats_min": 3.0, "stats_max": 3.0, "stats_range": 0.0, "stats_minority": 3.0, "stats_majority": 3.0, "stats_variety": 1.0, "stats_variance": 0.0 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 18.690696186730751, 45.786612465719514 ], [ 18.694442232424951, 45.791133555350441 ], [ 18.70090093189771, 45.785643660798598 ], [ 18.699738365992616, 45.781122571167664 ], [ 18.688435641915287, 45.777957808426017 ], [ 18.680491441563792, 45.779895418267841 ], [ 18.6800393326007, 45.783318528988403 ], [ 18.690696186730751, 45.786612465719514 ] ] ] ] } } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
src/analysis/processing/qgsalgorithmzonalstatisticsfeaturebased.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/*************************************************************************** | ||
qgsalgorithmzonalstatisticsfeaturebased.cpp | ||
--------------------- | ||
begin : September 2020 | ||
copyright : (C) 2020 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* 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 "qgsalgorithmzonalstatisticsfeaturebased.h" | ||
|
||
///@cond PRIVATE | ||
|
||
const std::vector< QgsZonalStatistics::Statistic > STATS | ||
{ | ||
QgsZonalStatistics::Count, | ||
QgsZonalStatistics::Sum, | ||
QgsZonalStatistics::Mean, | ||
QgsZonalStatistics::Median, | ||
QgsZonalStatistics::StDev, | ||
QgsZonalStatistics::Min, | ||
QgsZonalStatistics::Max, | ||
QgsZonalStatistics::Range, | ||
QgsZonalStatistics::Minority, | ||
QgsZonalStatistics::Majority, | ||
QgsZonalStatistics::Variety, | ||
QgsZonalStatistics::Variance, | ||
}; | ||
|
||
QString QgsZonalStatisticsFeatureBasedAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "zonalstatisticsfb" ); | ||
} | ||
|
||
QString QgsZonalStatisticsFeatureBasedAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Zonal statistics" ); | ||
} | ||
|
||
QStringList QgsZonalStatisticsFeatureBasedAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "stats,statistics,zones,layer,sum,maximum,minimum,mean,count,standard,deviation," | ||
"median,range,majority,minority,variety,variance,summary,raster" ).split( ',' ); | ||
} | ||
|
||
QString QgsZonalStatisticsFeatureBasedAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Raster analysis" ); | ||
} | ||
|
||
QString QgsZonalStatisticsFeatureBasedAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "rasteranalysis" ); | ||
} | ||
|
||
QString QgsZonalStatisticsFeatureBasedAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm calculates statistics of a raster layer for each feature " | ||
"of an overlapping polygon vector layer." ); | ||
} | ||
|
||
QList<int> QgsZonalStatisticsFeatureBasedAlgorithm::inputLayerTypes() const | ||
{ | ||
return QList<int>() << QgsProcessing::TypeVectorPolygon; | ||
} | ||
|
||
QgsZonalStatisticsFeatureBasedAlgorithm *QgsZonalStatisticsFeatureBasedAlgorithm::createInstance() const | ||
{ | ||
return new QgsZonalStatisticsFeatureBasedAlgorithm(); | ||
} | ||
|
||
void QgsZonalStatisticsFeatureBasedAlgorithm::initParameters( const QVariantMap &configuration ) | ||
{ | ||
Q_UNUSED( configuration ) | ||
QStringList statChoices; | ||
statChoices.reserve( STATS.size() ); | ||
for ( QgsZonalStatistics::Statistic stat : STATS ) | ||
{ | ||
statChoices << QgsZonalStatistics::displayName( stat ); | ||
} | ||
|
||
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT_RASTER" ), QObject::tr( "Raster layer" ) ) ); | ||
addParameter( new QgsProcessingParameterBand( QStringLiteral( "RASTER_BAND" ), | ||
QObject::tr( "Raster band" ), 1, QStringLiteral( "INPUT_RASTER" ) ) ); | ||
|
||
addParameter( new QgsProcessingParameterString( QStringLiteral( "COLUMN_PREFIX" ), QObject::tr( "Output column prefix" ), QStringLiteral( "_" ) ) ); | ||
|
||
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "STATISTICS" ), QObject::tr( "Statistics to calculate" ), | ||
statChoices, true, QVariantList() << 0 << 1 << 2 ) ); | ||
} | ||
|
||
QString QgsZonalStatisticsFeatureBasedAlgorithm::outputName() const | ||
{ | ||
return QObject::tr( "Zonal Statistics" ); | ||
} | ||
|
||
QgsFields QgsZonalStatisticsFeatureBasedAlgorithm::outputFields( const QgsFields &inputFields ) const | ||
{ | ||
Q_UNUSED( inputFields ) | ||
return mOutputFields; | ||
} | ||
|
||
bool QgsZonalStatisticsFeatureBasedAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
mPrefix = parameterAsString( parameters, QStringLiteral( "COLUMN_PREFIX" ), context ); | ||
|
||
const QList< int > stats = parameterAsEnums( parameters, QStringLiteral( "STATISTICS" ), context ); | ||
mStats = nullptr; | ||
for ( int s : stats ) | ||
{ | ||
mStats |= STATS.at( s ); | ||
} | ||
|
||
QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_RASTER" ), context ); | ||
if ( !rasterLayer ) | ||
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) ); | ||
|
||
mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context ); | ||
if ( mBand < 1 || mBand > rasterLayer->bandCount() ) | ||
throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ) | ||
.arg( rasterLayer->bandCount() ) ); | ||
|
||
if ( !rasterLayer->dataProvider() ) | ||
throw QgsProcessingException( QObject::tr( "Invalid raster layer. Layer %1 is invalid." ).arg( rasterLayer->id() ) ); | ||
|
||
mRaster.reset( rasterLayer->dataProvider()->clone() ); | ||
mCrs = rasterLayer->crs(); | ||
mPixelSizeX = rasterLayer->rasterUnitsPerPixelX(); | ||
mPixelSizeY = rasterLayer->rasterUnitsPerPixelY(); | ||
std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, inputParameterName(), context ) ); | ||
|
||
mOutputFields = source->fields(); | ||
|
||
for ( QgsZonalStatistics::Statistic stat : STATS ) | ||
{ | ||
if ( mStats & stat ) | ||
{ | ||
QgsField field = QgsField( mPrefix + QgsZonalStatistics::shortName( stat ), QVariant::Double, QStringLiteral( "double precision" ) ); | ||
if ( mOutputFields.names().contains( field.name() ) ) | ||
{ | ||
throw QgsProcessingException( QObject::tr( "Field %1 already exists" ).arg( field.name() ) ); | ||
} | ||
mOutputFields.append( field ); | ||
mStatFieldsMapping.insert( stat, mOutputFields.size() - 1 ); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
QgsFeatureList QgsZonalStatisticsFeatureBasedAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | ||
{ | ||
Q_UNUSED( context ) | ||
Q_UNUSED( feedback ) | ||
QgsAttributes attributes = feature.attributes(); | ||
attributes.resize( mOutputFields.size() ); | ||
|
||
QMap<QgsZonalStatistics::Statistic, QVariant> results = QgsZonalStatistics::calculateStatistics( mRaster.get(), feature.geometry(), mPixelSizeX, mPixelSizeY, mBand, mStats ); | ||
for ( auto result = results.constBegin(); result != results.constEnd(); ++result ) | ||
{ | ||
attributes.replace( mStatFieldsMapping.value( result.key() ), result.value() ); | ||
} | ||
|
||
QgsFeature resultFeature = feature; | ||
resultFeature.setAttributes( attributes ); | ||
|
||
return QgsFeatureList { resultFeature }; | ||
} | ||
|
||
bool QgsZonalStatisticsFeatureBasedAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const | ||
{ | ||
Q_UNUSED( layer ) | ||
return false; | ||
} | ||
|
||
///@endcond |
73 changes: 73 additions & 0 deletions
73
src/analysis/processing/qgsalgorithmzonalstatisticsfeaturebased.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/*************************************************************************** | ||
qgsalgorithmzonalstatisticsfeaturebased.h | ||
------------------------------ | ||
begin : September 2020 | ||
copyright : (C) 2020 Matthias Kuhn | ||
email : matthias@opengis.ch | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* 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 QGSALGORITHMZONALSTATISTICSFEATUREBASED_H | ||
#define QGSALGORITHMZONALSTATISTICSFEATUREBASED_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
#include "qgsvectorlayer.h" | ||
#include "vector/qgszonalstatistics.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native zonal statistics algorithm. | ||
*/ | ||
class QgsZonalStatisticsFeatureBasedAlgorithm : public QgsProcessingFeatureBasedAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsZonalStatisticsFeatureBasedAlgorithm() = 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; | ||
QList<int> inputLayerTypes() const override; | ||
|
||
QgsZonalStatisticsFeatureBasedAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
void initParameters( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString outputName() const override; | ||
QgsFields outputFields( const QgsFields &inputFields ) const override; | ||
|
||
bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
bool supportInPlaceEdit( const QgsMapLayer *layer ) const override; | ||
|
||
private: | ||
std::unique_ptr< QgsRasterInterface > mRaster; | ||
int mBand; | ||
QString mPrefix; | ||
QgsZonalStatistics::Statistics mStats = QgsZonalStatistics::All; | ||
QgsCoordinateReferenceSystem mCrs; | ||
double mPixelSizeX; | ||
double mPixelSizeY; | ||
QgsFields mOutputFields; | ||
QMap<QgsZonalStatistics::Statistic, int> mStatFieldsMapping; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMZONALSTATISTICSFEATUREBASED_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.