Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UTF-8 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.
Binary file not shown.