Skip to content
Permalink
Browse files

Fix zonal stats doesn't work with raster/vector in different CRS

Fixes #19027
  • Loading branch information
nyalldawson committed Jun 5, 2018
1 parent e94b1ac commit 88a49e7e331e0f728ffed8c63d99eb0f59fb2272
Showing with 66 additions and 0 deletions.
  1. +2 −0 src/analysis/vector/qgszonalstatistics.cpp
  2. +64 −0 tests/src/analysis/testqgszonalstatistics.cpp
@@ -26,6 +26,7 @@
#include "qgsrasterblock.h"
#include "qgslogger.h"
#include "qgsgeos.h"
#include "qgsproject.h"

#include <QFile>

@@ -204,6 +205,7 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
//iterate over each polygon
QgsFeatureRequest request;
request.setSubsetOfAttributes( QgsAttributeList() );
request.setDestinationCrs( mRasterLayer->crs(), QgsProject::instance()->transformContext() );
QgsFeatureIterator fi = vectorProvider->getFeatures( request );
QgsFeature f;

@@ -22,6 +22,7 @@
#include "qgsrasterlayer.h"
#include "qgszonalstatistics.h"
#include "qgsproject.h"
#include "qgsvectorlayerutils.h"

/**
* \ingroup UnitTests
@@ -38,6 +39,7 @@ class TestQgsZonalStatistics : public QObject
void cleanup() {}

void testStatistics();
void testReprojection();

private:
QgsVectorLayer *mVectorLayer = nullptr;
@@ -182,5 +184,67 @@ void TestQgsZonalStatistics::testStatistics()
QCOMPARE( f.attribute( "myqgis2__4" ).toDouble(), 0.13888888888889 );
}

void TestQgsZonalStatistics::testReprojection()
{
QString myDataPath( TEST_DATA_DIR ); //defined in CmakeLists.txt
QString myTestDataPath = myDataPath + "/zonalstatistics/";

// create a reprojected version of the layer
std::unique_ptr< QgsVectorLayer > vectorLayer( new QgsVectorLayer( myTestDataPath + "polys.shp", QStringLiteral( "poly" ), QStringLiteral( "ogr" ) ) );
std::unique_ptr< QgsVectorLayer > reprojected( vectorLayer->materialize( QgsFeatureRequest().setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3785" ) ), QgsProject::instance()->transformContext() ) ) );

QCOMPARE( reprojected->featureCount(), vectorLayer->featureCount() );
QgsZonalStatistics zs( reprojected.get(), mRasterLayer, QString(), 1, QgsZonalStatistics::All );
zs.calculateStatistics( nullptr );

QgsFeature f;
QgsFeatureRequest request;
QgsFeatureIterator it = reprojected->getFeatures( request );
bool fetched = it.nextFeature( f );
QVERIFY( fetched );
QCOMPARE( f.attribute( "count" ).toDouble(), 12.0 );
QCOMPARE( f.attribute( "sum" ).toDouble(), 8.0 );
QCOMPARE( f.attribute( "mean" ).toDouble(), 0.666666666666667 );
QCOMPARE( f.attribute( "median" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "stdev" ).toDouble(), 0.47140452079103201 );
QCOMPARE( f.attribute( "min" ).toDouble(), 0.0 );
QCOMPARE( f.attribute( "max" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "range" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "minority" ).toDouble(), 0.0 );
QCOMPARE( f.attribute( "majority" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "variety" ).toDouble(), 2.0 );
QCOMPARE( f.attribute( "variance" ).toDouble(), 0.222222222222222 );

fetched = it.nextFeature( f );
QVERIFY( fetched );
QCOMPARE( f.attribute( "count" ).toDouble(), 9.0 );
QCOMPARE( f.attribute( "sum" ).toDouble(), 5.0 );
QCOMPARE( f.attribute( "mean" ).toDouble(), 0.555555555555556 );
QCOMPARE( f.attribute( "median" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "stdev" ).toDouble(), 0.49690399499995302 );
QCOMPARE( f.attribute( "min" ).toDouble(), 0.0 );
QCOMPARE( f.attribute( "max" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "range" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "minority" ).toDouble(), 0.0 );
QCOMPARE( f.attribute( "majority" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "variety" ).toDouble(), 2.0 );
QCOMPARE( f.attribute( "variance" ).toDouble(), 0.24691358024691 );

fetched = it.nextFeature( f );
QVERIFY( fetched );
QCOMPARE( f.attribute( "count" ).toDouble(), 6.0 );
QCOMPARE( f.attribute( "sum" ).toDouble(), 5.0 );
QCOMPARE( f.attribute( "mean" ).toDouble(), 0.833333333333333 );
QCOMPARE( f.attribute( "median" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "stdev" ).toDouble(), 0.372677996249965 );
QCOMPARE( f.attribute( "min" ).toDouble(), 0.0 );
QCOMPARE( f.attribute( "max" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "range" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "minority" ).toDouble(), 0.0 );
QCOMPARE( f.attribute( "majority" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "variety" ).toDouble(), 2.0 );
QCOMPARE( f.attribute( "variance" ).toDouble(), 0.13888888888889 );
}

QGSTEST_MAIN( TestQgsZonalStatistics )
#include "testqgszonalstatistics.moc"

0 comments on commit 88a49e7

Please sign in to comment.
You can’t perform that action at this time.