Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new algorithm to calculate zonal statistics into a new sink
- Loading branch information
Showing
8 changed files
with
447 additions
and
267 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
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
189 changes: 189 additions & 0 deletions
189
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,189 @@ | ||
/*************************************************************************** | ||
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 (feature based)" ); | ||
} | ||
|
||
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; | ||
} | ||
|
||
QgsProcessingFeatureSource::Flag QgsZonalStatisticsFeatureBasedAlgorithm::sourceFlags() const | ||
{ | ||
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks; | ||
} | ||
|
||
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(); | ||
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 ( const auto &result : results.toStdMap() ) | ||
{ | ||
attributes.replace( mStatFieldsMapping.value( result.first ), result.second ); | ||
} | ||
|
||
QgsFeature resultFeature = feature; | ||
resultFeature.setAttributes( attributes ); | ||
|
||
return QgsFeatureList { resultFeature }; | ||
} | ||
|
||
bool QgsZonalStatisticsFeatureBasedAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const | ||
{ | ||
Q_UNUSED( layer ) | ||
return false; | ||
} | ||
|
||
///@endcond |
Oops, something went wrong.