Skip to content

Commit

Permalink
unittest for QgsZonalStatistics
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jul 17, 2013
1 parent 08c80b4 commit 0a27d0c
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 5 deletions.
8 changes: 3 additions & 5 deletions tests/src/analysis/CMakeLists.txt
Expand Up @@ -5,7 +5,7 @@ SET (util_SRCS)
#####################################################
# Don't forget to include output directory, otherwise
# the UI file won't be wrapped!
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/src/core
${CMAKE_SOURCE_DIR}/src/core/raster
Expand Down Expand Up @@ -47,7 +47,7 @@ ENDIF (APPLE)
#qtests in the executable file list as the moc is
#directly included in the sources
#and should not be compiled twice. Trying to include
#them in will cause an error at build time
#them in will cause an error at build time

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

ADD_QGIS_TEST(analyzertest testqgsvectoranalyzer.cpp)
ADD_QGIS_TEST(openstreetmaptest testopenstreetmap.cpp)



ADD_QGIS_TEST(zonalstatisticstest testqgszonalstatistics.cpp)
120 changes: 120 additions & 0 deletions tests/src/analysis/testqgszonalstatistics.cpp
@@ -0,0 +1,120 @@
/***************************************************************************
testqgszonalstatistics.cpp
--------------------------------------
Date : 15 Jul 2013
Copyright : (C) 2013 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* 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 <QDir>
#include <QtTest>

#include "qgsapplication.h"
#include "qgsvectorlayer.h"
#include "qgszonalstatistics.h"

/** \ingroup UnitTests
* This is a unit test for the zonal statistics class
*/
class TestQgsZonalStatistics: public QObject
{
Q_OBJECT;
private slots:
void initTestCase();
void cleanupTestCase() {};
void init() {};
void cleanup() {};

void testStatistics();

private:
QgsVectorLayer* mVectorLayer;
QString mRasterPath;
};

void TestQgsZonalStatistics::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();

QString myDataPath( TEST_DATA_DIR ); //defined in CmakeLists.txt
QString myTestDataPath = myDataPath + QDir::separator() + "zonalstatistics" + QDir::separator();
QString myTempPath = QDir::tempPath() + QDir::separator();

// copy test data to temp directory
QDir testDir( myTestDataPath );
QStringList files = testDir.entryList( QDir::Files | QDir::NoDotAndDotDot );
for ( int i = 0; i < files.size(); ++i )
{
QFile::remove( myTempPath + files.at( i ) );
QVERIFY( QFile::copy( myTestDataPath + files.at( i ), myTempPath + files.at( i ) ) );
}

mVectorLayer = new QgsVectorLayer( myTempPath + "polys.shp", "poly", "ogr" );
mRasterPath = myTempPath + "edge_problem.asc";
}

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

QgsFeature f;
QgsFeatureRequest request;
request.setFilterFid( 0 );
bool fetched = mVectorLayer->getFeatures( request ).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 );

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 );

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 );

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

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 );

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 );

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 );
}

QTEST_MAIN( TestQgsZonalStatistics )
#include "moc_testqgszonalstatistics.cxx"
2 changes: 2 additions & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -25,3 +25,5 @@ ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
#ADD_PYTHON_TEST(PyQgsPalLabeling test_qgspallabeling.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)

73 changes: 73 additions & 0 deletions tests/src/python/test_qgszonalstatistics.py
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsZonalStatistics.
.. note:: 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.
"""
__author__ = 'Alexander Bruy'
__date__ = '15/07/2013'
__copyright__ = 'Copyright 2013, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

from PyQt4.QtCore import *
from qgis.core import *
from qgis.analysis import *
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest,
)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsZonalStatistics(TestCase):

def testStatistics(self):
sep = os.sep
TEST_DATA_DIR = unitTestDataPath() + sep + "zonalstatistics" + sep
myTempPath = QDir.tempPath() + sep
testDir = QDir(TEST_DATA_DIR)
for f in testDir.entryList(QDir.Files):
QFile.copy(TEST_DATA_DIR + f, myTempPath + f)

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

feat = QgsFeature()
# validate statistics for each feature
request = QgsFeatureRequest().setFilterFid(0)
feat = myVector.getFeatures(request).next()
myMessage = ('Expected: %f\nGot: %f\n' % (12.0, feat[1].toDouble()[0]))
assert feat[1] == 12.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (8.0, feat[2].toDouble()[0]))
assert feat[2] == 8.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.666666666666667, feat[3].toDouble()[0]))
assert feat[3] == 0.666666666666667, myMessage

request.setFilterFid(1)
feat = myVector.getFeatures(request).next()
myMessage = ('Expected: %f\nGot: %f\n' % (9.0, feat[1].toDouble()[0]))
assert feat[1] == 9.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2].toDouble()[0]))
assert feat[2] == 5.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.555555555555556, feat[3].toDouble()[0]))
assert feat[3] == 0.555555555555556, myMessage

request.setFilterFid(2)
feat = myVector.getFeatures(request).next()
myMessage = ('Expected: %f\nGot: %f\n' % (6.0, feat[1].toDouble()[0]))
assert feat[1] == 6.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2].toDouble()[0]))
assert feat[2] == 5.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (0.833333333333333, feat[3].toDouble()[0]))
assert feat[3] == 0.833333333333333, myMessage

if __name__ == '__main__':
unittest.main()
9 changes: 9 additions & 0 deletions tests/testdata/zonalstatistics/edge_problem.asc
@@ -0,0 +1,9 @@
ncols 4
nrows 3
xllcorner 100.379357000000
yllcorner -0.960588000000
cellsize 0.000045000000
NODATA_value nan
1 1 0 0
1 1 0 0
1 1 1 1
28 changes: 28 additions & 0 deletions tests/testdata/zonalstatistics/edge_problem.asc.aux.xml
@@ -0,0 +1,28 @@
<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
<HistMin>-0,04166666666666666</HistMin>
<HistMax>1,041666666666667</HistMax>
<BucketCount>12</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>4|0|0|0|0|0|0|0|0|0|0|8</HistCounts>
</HistItem>
<HistItem>
<HistMin>-0.04166666666666666</HistMin>
<HistMax>1.041666666666667</HistMax>
<BucketCount>12</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>4|0|0|0|0|0|0|0|0|0|0|8</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="STATISTICS_MAXIMUM">1</MDI>
<MDI key="STATISTICS_MEAN">0.66666666666667</MDI>
<MDI key="STATISTICS_MINIMUM">0</MDI>
<MDI key="STATISTICS_STDDEV">0.47140452079103</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>
1 change: 1 addition & 0 deletions tests/testdata/zonalstatistics/edge_problem.prj
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
1 change: 1 addition & 0 deletions tests/testdata/zonalstatistics/polys.cpg
@@ -0,0 +1 @@
UTF-8
Binary file added tests/testdata/zonalstatistics/polys.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/zonalstatistics/polys.prj
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
1 change: 1 addition & 0 deletions tests/testdata/zonalstatistics/polys.qpj
@@ -0,0 +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"]]
Binary file added tests/testdata/zonalstatistics/polys.shp
Binary file not shown.
Binary file added tests/testdata/zonalstatistics/polys.shx
Binary file not shown.

0 comments on commit 0a27d0c

Please sign in to comment.