Skip to content
Permalink
Browse files

python tests for QgsRectangle and QgsSpatialIndex

  • Loading branch information
alexbruy committed Sep 7, 2012
1 parent 4d630b7 commit 26dc40676df41b28f3a15fd6a2f4bf9c40d7ca43
Showing with 190 additions and 0 deletions.
  1. +2 −0 tests/src/python/CMakeLists.txt
  2. +147 −0 tests/src/python/test_qgsrectangle.py
  3. +41 −0 tests/src/python/test_qgsspatialindex.py
@@ -8,3 +8,5 @@ ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
ADD_PYTHON_TEST(PyQgsMemoryProvider test_qgsmemoryprovider.py)
ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
ADD_PYTHON_TEST(PyQgsRectangle test_qgsrectangle.py)
ADD_PYTHON_TEST(PyQgsSpatialIndex test_qgsspatialindex.py)
@@ -0,0 +1,147 @@
import unittest

from qgis.core import (QGis,
QgsRectangle,
QgsPoint)

from utilities import getQgisTestApp
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsRectangle(unittest.TestCase):

def testCtor(self):
rect = QgsRectangle( 5.0, 5.0, 10.0, 10.0)

assert rect.isEmpty(), "Empty rectangle constructed"

myMessage = ('Expected: %s\nGot: %s\n' %
(5.0, rect.xMinimum()))
assert rect.xMinimum() == 5.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(5.0, rect.yMinimum()))
assert rect.yMinimum() == 5.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.xMaximum()))
assert rect.xMaximum() == 10.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.yMaximum()))
assert rect.yMaximum() == 10.0, myMessage


def testDimensions(self):
rect = QgsRectangle( 0.0, 0.0, 10.0, 10.0)

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.width()))
assert rect.width() == 10.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.height()))
assert rect.height() == 10.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
("5.0, 5.0", rect.center().toString()))
assert rect.center() == QgsPoint(5.0, 5.0), myMessage

rect.scale(2.0)

myMessage = ('Expected: %s\nGot: %s\n' %
(20.0, rect.width()))
assert rect.width() == 20.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(20.0, rect.height()))
assert rect.height() == 20.0, myMessage

def testIntersection(self):
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
rect2 = QgsRectangle( 2.0, 2.0, 7.0, 7.0)

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.intersects(rect2)))
assert rect1.intersects(rect2), myMessage

rect3 = rect1.intersect(rect2)
assert rect3.isEmpty(), "Empty rectangle returned"

myMessage = ('Expected: %s\nGot: %s\n' %
(3.0, rect.width()))
assert rect.width() == 3.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(3.0, rect.height()))
assert rect.height() == 3.0, myMessage

def testContains(self):
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
rect2 = QgsRectangle( 2.0, 2.0, 7.0, 7.0)
pnt1 = QgsPoint(4.0, 4.0)
pnt2 = QgsPoint(6.0, 2.0)

rect3 = rect1.intersect(rect2)

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(rect3)))
assert rect1.contains(rect3), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect2.contains(rect3)))
assert rect2.contains(rect3), myMessage

# test for point
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(pnt1)))
assert rect1.contains(pnt1), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect2.contains(pnt1)))
assert rect2.contains(pnt1), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect3.contains(pnt1)))
assert rect3.contains(pnt1), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(False, rect1.contains(pnt2)))
assert rect1.contains(pnt2), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect2.contains(pnt2)))
assert rect2.contains(pnt2), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect3.contains(pnt2)))
assert rect3.contains(pnt2), myMessage

def testUnion(self):
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
rect2 = QgsRectangle( 2.0, 2.0, 7.0, 7.0)
pnt1 = QgsPoint(6.0, 2.0)

rect1.combineExtentWith(rect2)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(rect2)))
assert rect1.contains(rect2), myMessage

print rect1.toString()
assert rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0), "Wrong combine with rectangle result"

rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
rect1.combineExtentWith(6.0, 2.0)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(pnt1)))
assert rect1.contains(pnt1), myMessage

print rect1.toString()
assert rect1 == QgsRectangle(0.0, 0.0, 6.0, 6.0), "Wrong combine with point result"

rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
rect1.unionRect(rect2)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(rect2)))
assert rect1.contains(rect2), myMessage

assert rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0), "Wrong union result"
@@ -0,0 +1,41 @@
import unittest

from qgis.core import (QGis,
QgsRectangle,
QgsPoint)

from utilities import getQgisTestApp
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsSpatialIndex(unittest.TestCase):

def testIndex(self):
idx = QgsSpatialIndex()
fid = 0
for y in range(5, 15, 5):
for x in range(5, 25, 5):
ft = QgsFeature()
ft.setFeatureId(fid)
ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(x, y)))
idx.insertFeature(ft)
fid += 1

# intersection test
rect = QgsRectangle(7.0, 3.0, 17.0, 13.0)
fids = idx.intersects(rect)

assert len(fids) == 0, "No intersections"

fids.sort()
myMessage = ('Expected: %s\nGot: %s\n' %
([1, 2, 5, 6], fids))
assert fids == [1, 2, 5, 6], myMessage

# nearest neighbor test
fids = idx.nearestNeighbor(QgsPoint(8.75, 6.25), 3)
assert len(fids) == 0, "No intersections"

fids.sort()
myMessage = ('Expected: %s\nGot: %s\n' %
([0, 1, 5], fids))
assert fids == [0, 1, 5], myMessage

0 comments on commit 26dc406

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