Skip to content
Permalink
Browse files
Python formatting cleanups for zonal stats and raster layer tests
  • Loading branch information
timlinux committed Jul 26, 2013
1 parent 282ef29 commit 04068c95fcfdd2297fcf23b97fe4872bae7e34b3
Showing with 55 additions and 52 deletions.
  1. +9 −9 tests/src/python/test_qgsrasterlayer.py
  2. +46 −43 tests/src/python/test_qgszonalstatistics.py
@@ -35,13 +35,13 @@
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
#expectedFailure
)
unittest)
#expectedFailure)
# Convenience instances in case you may need them
# not used in this test
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsRasterLayer(TestCase):

def testIdentify(self):
@@ -178,14 +178,14 @@ def testShaderCrash(self):
myColorRampShader = QgsColorRampShader()
myColorRampShader.setColorRampType(QgsColorRampShader.INTERPOLATED)
myItems = []
myItem = QgsColorRampShader.ColorRampItem(10,
QtGui.QColor('#ffff00'), 'foo')
myItem = QgsColorRampShader.ColorRampItem(
10, QtGui.QColor('#ffff00'), 'foo')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(100,
QtGui.QColor('#ff00ff'), 'bar')
myItem = QgsColorRampShader.ColorRampItem(
100, QtGui.QColor('#ff00ff'), 'bar')
myItems.append(myItem)
myItem = QgsColorRampShader.ColorRampItem(1000,
QtGui.QColor('#00ff00'), 'kazam')
myItem = QgsColorRampShader.ColorRampItem(
1000, QtGui.QColor('#00ff00'), 'kazam')
myItems.append(myItem)
myColorRampShader.setColorRampItemList(myItems)
myRasterShader.setRasterShaderFunction(myColorRampShader)
@@ -18,58 +18,61 @@
from PyQt4.QtCore import *
from qgis.core import *
from qgis.analysis import *
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest,
)
from utilities import (
unitTestDataPath,
getQgisTestApp,
TestCase,
unittest)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsZonalStatistics(TestCase):
"""Tests for zonal stats class."""

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.remove(myTempPath + f)
QFile.copy(TEST_DATA_DIR + f, myTempPath + f)
def testStatistics(self):
"""Test zonal stats"""
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.remove(myTempPath + f)
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)
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]))
assert feat[1] == 12.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (8.0, feat[2]))
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
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]))
assert feat[1] == 12.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (8.0, feat[2]))
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

request.setFilterFid(1)
feat = myVector.getFeatures(request).next()
myMessage = ('Expected: %f\nGot: %f\n' % (9.0, feat[1]))
assert feat[1] == 9.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2]))
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
request.setFilterFid(1)
feat = myVector.getFeatures(request).next()
myMessage = ('Expected: %f\nGot: %f\n' % (9.0, feat[1]))
assert feat[1] == 9.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2]))
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

request.setFilterFid(2)
feat = myVector.getFeatures(request).next()
myMessage = ('Expected: %f\nGot: %f\n' % (6.0, feat[1]))
assert feat[1] == 6.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2]))
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
request.setFilterFid(2)
feat = myVector.getFeatures(request).next()
myMessage = ('Expected: %f\nGot: %f\n' % (6.0, feat[1]))
assert feat[1] == 6.0, myMessage
myMessage = ('Expected: %f\nGot: %f\n' % (5.0, feat[2]))
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

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

0 comments on commit 04068c9

Please sign in to comment.