Skip to content
Permalink
Browse files

extend zonal statistics tests

  • Loading branch information
alexbruy committed Jan 27, 2017
1 parent aef2b00 commit 8e3042286d4e481008792530bfac5296209861b1
Showing with 102 additions and 6 deletions.
  1. +53 −5 tests/src/analysis/testqgszonalstatistics.cpp
  2. +49 −1 tests/src/python/test_qgszonalstatistics.py
@@ -84,7 +84,7 @@ void TestQgsZonalStatistics::cleanupTestCase()

void TestQgsZonalStatistics::testStatistics()
{
QgsZonalStatistics zs( mVectorLayer, mRasterPath, QLatin1String( "" ), 1 );
QgsZonalStatistics zs( mVectorLayer, mRasterPath, QLatin1String( "" ), 1, QgsZonalStatistics::All );
zs.calculateStatistics( nullptr );

QgsFeature f;
@@ -95,45 +95,93 @@ void TestQgsZonalStatistics::testStatistics()
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 );

request.setFilterFid( 1 );
fetched = mVectorLayer->getFeatures( request ).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 );

request.setFilterFid( 2 );
fetched = mVectorLayer->getFeatures( request ).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 );

// same with long prefix to ensure that field name truncation handled correctly
QgsZonalStatistics zsl( mVectorLayer, mRasterPath, QStringLiteral( "myqgis2_" ), 1 );
QgsZonalStatistics zsl( mVectorLayer, mRasterPath, QStringLiteral( "myqgis2_" ), 1, QgsZonalStatistics::All );
zsl.calculateStatistics( nullptr );

request.setFilterFid( 0 );
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
QVERIFY( fetched );
QCOMPARE( f.attribute( "myqgis2_co" ).toDouble(), 12.0 );
QCOMPARE( f.attribute( "myqgis2_su" ).toDouble(), 8.0 );
QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.666666666666667 );
//QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.666666666666667 );
//QCOMPARE( f.attribute( "myqgis2__1" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_st" ).toDouble(), 0.47140452079103201 );
//QCOMPARE( f.attribute( "myqgis2_mi" ).toDouble(), 0.0 );
//QCOMPARE( f.attribute( "myqgis2_ma" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_ra" ).toDouble(), 1.0 );
//QCOMPARE( f.attribute( "myqgis2__2" ).toDouble(), 0.0 );
//QCOMPARE( f.attribute( "myqgis2__3" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_va" ).toDouble(), 2.0 );

request.setFilterFid( 1 );
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
QVERIFY( fetched );
QCOMPARE( f.attribute( "myqgis2_co" ).toDouble(), 9.0 );
QCOMPARE( f.attribute( "myqgis2_su" ).toDouble(), 5.0 );
QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.555555555555556 );
//QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.555555555555556 );
//QCOMPARE( f.attribute( "myqgis2__1" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_st" ).toDouble(), 0.49690399499995302 );
//QCOMPARE( f.attribute( "myqgis2_mi" ).toDouble(), 0.0 );
//QCOMPARE( f.attribute( "myqgis2_ma" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_ra" ).toDouble(), 1.0 );
//QCOMPARE( f.attribute( "myqgis2__2" ).toDouble(), 0.0 );
//QCOMPARE( f.attribute( "myqgis2__3" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_va" ).toDouble(), 2.0 );

request.setFilterFid( 2 );
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
QVERIFY( fetched );
QCOMPARE( f.attribute( "myqgis2_co" ).toDouble(), 6.0 );
QCOMPARE( f.attribute( "myqgis2_su" ).toDouble(), 5.0 );
QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.833333333333333 );
//QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.833333333333333 );
//QCOMPARE( f.attribute( "myqgis2__1" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_st" ).toDouble(), 0.372677996249965 );
//QCOMPARE( f.attribute( "myqgis2_mi" ).toDouble(), 0.0 );
//QCOMPARE( f.attribute( "myqgis2_ma" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_ra" ).toDouble(), 1.0 );
//QCOMPARE( f.attribute( "myqgis2__2" ).toDouble(), 0.0 );
//QCOMPARE( f.attribute( "myqgis2__3" ).toDouble(), 1.0 );
QCOMPARE( f.attribute( "myqgis2_va" ).toDouble(), 2.0 );
}

QGSTEST_MAIN( TestQgsZonalStatistics )
@@ -39,7 +39,7 @@ def testStatistics(self):

myVector = QgsVectorLayer(myTempPath + "polys.shp", "poly", "ogr")
myRasterPath = myTempPath + "edge_problem.asc"
zs = QgsZonalStatistics(myVector, myRasterPath, "", 1)
zs = QgsZonalStatistics(myVector, myRasterPath, "", 1, QgsZonalStatistics.All)
zs.calculateStatistics(None)

feat = QgsFeature()
@@ -52,6 +52,22 @@ def testStatistics(self):
assert feat[2] == 8.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.666666666666667, feat[3]))
assert abs(feat[3] - 0.666666666666667) < 0.00001, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[4]))
assert feat[4] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.47140452079103201, feat[5]))
assert abs(feat[5] - 0.47140452079103201) < 0.00001, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.0, feat[6]))
assert feat[6] == 0.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[7]))
assert feat[7] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[8]))
assert feat[8] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.0, feat[9]))
assert feat[9] == 0.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[10]))
assert feat[10] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (2.0, feat[11]))
assert feat[11] == 2.0, myMessage

request.setFilterFid(1)
feat = next(myVector.getFeatures(request))
@@ -61,6 +77,22 @@ def testStatistics(self):
assert feat[2] == 5.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.555555555555556, feat[3]))
assert abs(feat[3] - 0.555555555555556) < 0.00001, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[4]))
assert feat[4] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.49690399499995302, feat[5]))
assert abs(feat[5] - 0.49690399499995302) < 0.00001, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.0, feat[6]))
assert feat[6] == 0.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[7]))
assert feat[7] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[8]))
assert feat[8] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.0, feat[9]))
assert feat[9] == 0.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[10]))
assert feat[10] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (2.0, feat[11]))
assert feat[11] == 2.0, myMessage

request.setFilterFid(2)
feat = next(myVector.getFeatures(request))
@@ -70,6 +102,22 @@ def testStatistics(self):
assert feat[2] == 5.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.833333333333333, feat[3]))
assert abs(feat[3] - 0.833333333333333) < 0.00001, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[4]))
assert feat[4] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.372677996249965, feat[5]))
assert abs(feat[5] - 0.372677996249965) < 0.00001, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.0, feat[6]))
assert feat[6] == 0.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[7]))
assert feat[7] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[8]))
assert feat[8] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.0, feat[9]))
assert feat[9] == 0.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (1.0, feat[10]))
assert feat[10] == 1.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (2.0, feat[11]))
assert feat[11] == 2.0, myMessage

if __name__ == '__main__':
unittest.main()

0 comments on commit 8e30422

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