Skip to content

Commit 0a27d0c

Browse files
committedJul 17, 2013
unittest for QgsZonalStatistics
1 parent 08c80b4 commit 0a27d0c

File tree

13 files changed

+239
-5
lines changed

13 files changed

+239
-5
lines changed
 

‎tests/src/analysis/CMakeLists.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SET (util_SRCS)
55
#####################################################
66
# Don't forget to include output directory, otherwise
77
# the UI file won't be wrapped!
8-
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
8+
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
99
${CMAKE_CURRENT_BINARY_DIR}
1010
${CMAKE_SOURCE_DIR}/src/core
1111
${CMAKE_SOURCE_DIR}/src/core/raster
@@ -47,7 +47,7 @@ ENDIF (APPLE)
4747
#qtests in the executable file list as the moc is
4848
#directly included in the sources
4949
#and should not be compiled twice. Trying to include
50-
#them in will cause an error at build time
50+
#them in will cause an error at build time
5151

5252
#No relinking and full RPATH for the install tree
5353
#See: http://www.cmake.org/Wiki/CMake_RPATH_handling#No_relinking_and_full_RPATH_for_the_install_tree
@@ -71,6 +71,4 @@ ENDMACRO (ADD_QGIS_TEST)
7171

7272
ADD_QGIS_TEST(analyzertest testqgsvectoranalyzer.cpp)
7373
ADD_QGIS_TEST(openstreetmaptest testopenstreetmap.cpp)
74-
75-
76-
74+
ADD_QGIS_TEST(zonalstatisticstest testqgszonalstatistics.cpp)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/***************************************************************************
2+
testqgszonalstatistics.cpp
3+
--------------------------------------
4+
Date : 15 Jul 2013
5+
Copyright : (C) 2013 by Alexander Bruy
6+
Email : alexander dot bruy at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QDir>
17+
#include <QtTest>
18+
19+
#include "qgsapplication.h"
20+
#include "qgsvectorlayer.h"
21+
#include "qgszonalstatistics.h"
22+
23+
/** \ingroup UnitTests
24+
* This is a unit test for the zonal statistics class
25+
*/
26+
class TestQgsZonalStatistics: public QObject
27+
{
28+
Q_OBJECT;
29+
private slots:
30+
void initTestCase();
31+
void cleanupTestCase() {};
32+
void init() {};
33+
void cleanup() {};
34+
35+
void testStatistics();
36+
37+
private:
38+
QgsVectorLayer* mVectorLayer;
39+
QString mRasterPath;
40+
};
41+
42+
void TestQgsZonalStatistics::initTestCase()
43+
{
44+
QgsApplication::init();
45+
QgsApplication::initQgis();
46+
QgsApplication::showSettings();
47+
48+
QString myDataPath( TEST_DATA_DIR ); //defined in CmakeLists.txt
49+
QString myTestDataPath = myDataPath + QDir::separator() + "zonalstatistics" + QDir::separator();
50+
QString myTempPath = QDir::tempPath() + QDir::separator();
51+
52+
// copy test data to temp directory
53+
QDir testDir( myTestDataPath );
54+
QStringList files = testDir.entryList( QDir::Files | QDir::NoDotAndDotDot );
55+
for ( int i = 0; i < files.size(); ++i )
56+
{
57+
QFile::remove( myTempPath + files.at( i ) );
58+
QVERIFY( QFile::copy( myTestDataPath + files.at( i ), myTempPath + files.at( i ) ) );
59+
}
60+
61+
mVectorLayer = new QgsVectorLayer( myTempPath + "polys.shp", "poly", "ogr" );
62+
mRasterPath = myTempPath + "edge_problem.asc";
63+
}
64+
65+
void TestQgsZonalStatistics::testStatistics()
66+
{
67+
QgsZonalStatistics zs( mVectorLayer, mRasterPath, "", 1 );
68+
zs.calculateStatistics( NULL );
69+
70+
QgsFeature f;
71+
QgsFeatureRequest request;
72+
request.setFilterFid( 0 );
73+
bool fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
74+
QVERIFY( fetched );
75+
QCOMPARE( f.attribute( "count" ).toDouble(), 12.0 );
76+
QCOMPARE( f.attribute( "sum" ).toDouble(), 8.0 );
77+
QCOMPARE( f.attribute( "mean" ).toDouble(), 0.666666666666667 );
78+
79+
request.setFilterFid( 1 );
80+
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
81+
QVERIFY( fetched );
82+
QCOMPARE( f.attribute( "count" ).toDouble(), 9.0 );
83+
QCOMPARE( f.attribute( "sum" ).toDouble(), 5.0 );
84+
QCOMPARE( f.attribute( "mean" ).toDouble(), 0.555555555555556 );
85+
86+
request.setFilterFid( 2 );
87+
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
88+
QVERIFY( fetched );
89+
QCOMPARE( f.attribute( "count" ).toDouble(), 6.0 );
90+
QCOMPARE( f.attribute( "sum" ).toDouble(), 5.0 );
91+
QCOMPARE( f.attribute( "mean" ).toDouble(), 0.833333333333333 );
92+
93+
// same with long prefix to ensure that field name truncation handled correctly
94+
QgsZonalStatistics zsl( mVectorLayer, mRasterPath, "myqgis2_", 1 );
95+
zsl.calculateStatistics( NULL );
96+
97+
request.setFilterFid( 0 );
98+
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
99+
QVERIFY( fetched );
100+
QCOMPARE( f.attribute( "myqgis2_co" ).toDouble(), 12.0 );
101+
QCOMPARE( f.attribute( "myqgis2_su" ).toDouble(), 8.0 );
102+
QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.666666666666667 );
103+
104+
request.setFilterFid( 1 );
105+
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
106+
QVERIFY( fetched );
107+
QCOMPARE( f.attribute( "myqgis2_co" ).toDouble(), 9.0 );
108+
QCOMPARE( f.attribute( "myqgis2_su" ).toDouble(), 5.0 );
109+
QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.555555555555556 );
110+
111+
request.setFilterFid( 2 );
112+
fetched = mVectorLayer->getFeatures( request ).nextFeature( f );
113+
QVERIFY( fetched );
114+
QCOMPARE( f.attribute( "myqgis2_co" ).toDouble(), 6.0 );
115+
QCOMPARE( f.attribute( "myqgis2_su" ).toDouble(), 5.0 );
116+
QCOMPARE( f.attribute( "myqgis2_me" ).toDouble(), 0.833333333333333 );
117+
}
118+
119+
QTEST_MAIN( TestQgsZonalStatistics )
120+
#include "moc_testqgszonalstatistics.cxx"

‎tests/src/python/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
2525
#ADD_PYTHON_TEST(PyQgsPalLabeling test_qgspallabeling.py)
2626
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
2727
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
28+
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
29+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsZonalStatistics.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Alexander Bruy'
10+
__date__ = '15/07/2013'
11+
__copyright__ = 'Copyright 2013, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import os
16+
17+
from PyQt4.QtCore import *
18+
from qgis.core import *
19+
from qgis.analysis import *
20+
from utilities import (unitTestDataPath,
21+
getQgisTestApp,
22+
TestCase,
23+
unittest,
24+
)
25+
26+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
27+
28+
class TestQgsZonalStatistics(TestCase):
29+
30+
def testStatistics(self):
31+
sep = os.sep
32+
TEST_DATA_DIR = unitTestDataPath() + sep + "zonalstatistics" + sep
33+
myTempPath = QDir.tempPath() + sep
34+
testDir = QDir(TEST_DATA_DIR)
35+
for f in testDir.entryList(QDir.Files):
36+
QFile.copy(TEST_DATA_DIR + f, myTempPath + f)
37+
38+
myVector = QgsVectorLayer(myTempPath + "polys.shp", "poly", "ogr")
39+
myRasterPath = myTempPath + "edge_problem.asc"
40+
zs = QgsZonalStatistics( myVector, myRasterPath, "", 1 )
41+
zs.calculateStatistics(None)
42+
43+
feat = QgsFeature()
44+
# validate statistics for each feature
45+
request = QgsFeatureRequest().setFilterFid(0)
46+
feat = myVector.getFeatures(request).next()
47+
myMessage = ('Expected: %f\nGot: %f\n' % (12.0, feat[1].toDouble()[0]))
48+
assert feat[1] == 12.0, myMessage
49+
myMessage = ('Expected: %f\nGot: %f\n' % (8.0, feat[2].toDouble()[0]))
50+
assert feat[2] == 8.0, myMessage
51+
myMessage = ('Expected: %f\nGot: %f\n' % (0.666666666666667, feat[3].toDouble()[0]))
52+
assert feat[3] == 0.666666666666667, myMessage
53+
54+
request.setFilterFid(1)
55+
feat = myVector.getFeatures(request).next()
56+
myMessage = ('Expected: %f\nGot: %f\n' % (9.0, feat[1].toDouble()[0]))
57+
assert feat[1] == 9.0, myMessage
58+
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2].toDouble()[0]))
59+
assert feat[2] == 5.0, myMessage
60+
myMessage = ('Expected: %f\nGot: %f\n' % (0.555555555555556, feat[3].toDouble()[0]))
61+
assert feat[3] == 0.555555555555556, myMessage
62+
63+
request.setFilterFid(2)
64+
feat = myVector.getFeatures(request).next()
65+
myMessage = ('Expected: %f\nGot: %f\n' % (6.0, feat[1].toDouble()[0]))
66+
assert feat[1] == 6.0, myMessage
67+
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2].toDouble()[0]))
68+
assert feat[2] == 5.0, myMessage
69+
myMessage = ('Expected: %f\nGot: %f\n' % (0.833333333333333, feat[3].toDouble()[0]))
70+
assert feat[3] == 0.833333333333333, myMessage
71+
72+
if __name__ == '__main__':
73+
unittest.main()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ncols 4
2+
nrows 3
3+
xllcorner 100.379357000000
4+
yllcorner -0.960588000000
5+
cellsize 0.000045000000
6+
NODATA_value nan
7+
1 1 0 0
8+
1 1 0 0
9+
1 1 1 1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<PAMDataset>
2+
<PAMRasterBand band="1">
3+
<Histograms>
4+
<HistItem>
5+
<HistMin>-0,04166666666666666</HistMin>
6+
<HistMax>1,041666666666667</HistMax>
7+
<BucketCount>12</BucketCount>
8+
<IncludeOutOfRange>0</IncludeOutOfRange>
9+
<Approximate>0</Approximate>
10+
<HistCounts>4|0|0|0|0|0|0|0|0|0|0|8</HistCounts>
11+
</HistItem>
12+
<HistItem>
13+
<HistMin>-0.04166666666666666</HistMin>
14+
<HistMax>1.041666666666667</HistMax>
15+
<BucketCount>12</BucketCount>
16+
<IncludeOutOfRange>0</IncludeOutOfRange>
17+
<Approximate>0</Approximate>
18+
<HistCounts>4|0|0|0|0|0|0|0|0|0|0|8</HistCounts>
19+
</HistItem>
20+
</Histograms>
21+
<Metadata>
22+
<MDI key="STATISTICS_MAXIMUM">1</MDI>
23+
<MDI key="STATISTICS_MEAN">0.66666666666667</MDI>
24+
<MDI key="STATISTICS_MINIMUM">0</MDI>
25+
<MDI key="STATISTICS_STDDEV">0.47140452079103</MDI>
26+
</Metadata>
27+
</PAMRasterBand>
28+
</PAMDataset>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8
158 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
556 Bytes
Binary file not shown.
124 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.