Skip to content

Commit

Permalink
Fixup zonal stats implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 9, 2020
1 parent 7f4e12f commit 42b4a03
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/analysis/vector/qgszonalstatistics.cpp
Expand Up @@ -57,8 +57,6 @@ QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer *polygonLayer, QgsRasterI


QgsZonalStatistics::Result QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback ) QgsZonalStatistics::Result QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
{ {
QgsVectorDataProvider *vectorProvider = nullptr;

if ( !mRasterInterface ) if ( !mRasterInterface )
{ {
return RasterInvalid; return RasterInvalid;
Expand All @@ -74,7 +72,7 @@ QgsZonalStatistics::Result QgsZonalStatistics::calculateStatistics( QgsFeedback
return LayerTypeWrong; return LayerTypeWrong;
} }


vectorProvider = mPolygonLayer->dataProvider(); QgsVectorDataProvider *vectorProvider = mPolygonLayer->dataProvider();
if ( !vectorProvider ) if ( !vectorProvider )
{ {
return LayerInvalid; return LayerInvalid;
Expand All @@ -83,6 +81,7 @@ QgsZonalStatistics::Result QgsZonalStatistics::calculateStatistics( QgsFeedback
QMap<QgsZonalStatistics::Statistic, int> statFieldIndexes; QMap<QgsZonalStatistics::Statistic, int> statFieldIndexes;


//add the new fields to the provider //add the new fields to the provider
int oldFieldCount = vectorProvider->fields().count();
QList<QgsField> newFieldList; QList<QgsField> newFieldList;
for ( QgsZonalStatistics::Statistic stat : for ( QgsZonalStatistics::Statistic stat :
{ {
Expand All @@ -105,7 +104,7 @@ QgsZonalStatistics::Result QgsZonalStatistics::calculateStatistics( QgsFeedback
QString fieldName = getUniqueFieldName( mAttributePrefix + QgsZonalStatistics::shortName( stat ), newFieldList ); QString fieldName = getUniqueFieldName( mAttributePrefix + QgsZonalStatistics::shortName( stat ), newFieldList );
QgsField field( fieldName, QVariant::Double, QStringLiteral( "double precision" ) ); QgsField field( fieldName, QVariant::Double, QStringLiteral( "double precision" ) );
newFieldList.push_back( field ); newFieldList.push_back( field );
statFieldIndexes.insert( stat, newFieldList.count() - 1 ); statFieldIndexes.insert( stat, oldFieldCount + newFieldList.count() - 1 );
} }
} }


Expand Down Expand Up @@ -288,7 +287,7 @@ QString QgsZonalStatistics::shortName( QgsZonalStatistics::Statistic statistic )
return QString(); return QString();
} }


QMap<QgsZonalStatistics::Statistic, QVariant> QgsZonalStatistics::calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, int cellSizeX, int cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics ) QMap<QgsZonalStatistics::Statistic, QVariant> QgsZonalStatistics::calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics )
{ {
QMap<QgsZonalStatistics::Statistic, QVariant> results; QMap<QgsZonalStatistics::Statistic, QVariant> results;


Expand Down
2 changes: 1 addition & 1 deletion src/analysis/vector/qgszonalstatistics.h
Expand Up @@ -155,7 +155,7 @@ class ANALYSIS_EXPORT QgsZonalStatistics
* *
* \since QGIS 3.16 * \since QGIS 3.16
*/ */
static QMap<QgsZonalStatistics::Statistic, QVariant> calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, int cellSizeX, int cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics ); static QMap<QgsZonalStatistics::Statistic, QVariant> calculateStatistics(QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics );


private: private:
QgsZonalStatistics() = default; QgsZonalStatistics() = default;
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/zonalstatistics/raster.tif.aux.xml
Expand Up @@ -5,6 +5,7 @@
<MDI key="STATISTICS_MEAN">865.86666666667</MDI> <MDI key="STATISTICS_MEAN">865.86666666667</MDI>
<MDI key="STATISTICS_MINIMUM">826</MDI> <MDI key="STATISTICS_MINIMUM">826</MDI>
<MDI key="STATISTICS_STDDEV">17.808206597584</MDI> <MDI key="STATISTICS_STDDEV">17.808206597584</MDI>
<MDI key="STATISTICS_VALID_PERCENT">53.57</MDI>
</Metadata> </Metadata>
</PAMRasterBand> </PAMRasterBand>
</PAMDataset> </PAMDataset>

0 comments on commit 42b4a03

Please sign in to comment.