Skip to content

Commit 5a0feb3

Browse files
committedOct 3, 2012
Fixes for QgsSpatialIndex python test
1 parent 80d645d commit 5a0feb3

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed
 
Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
11
import unittest
22

3-
from qgis.core import (QGis,
3+
from qgis.core import (QgsSpatialIndex,
4+
QgsFeature,
5+
QgsGeometry,
46
QgsRectangle,
57
QgsPoint)
68

79
from utilities import getQgisTestApp
10+
811
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
912

1013
class TestQgsSpatialIndex(unittest.TestCase):
11-
1214
def testIndex(self):
13-
idx = QgsSpatialIndex()
14-
fid = 0
15-
for y in range(5, 15, 5):
16-
for x in range(5, 25, 5):
17-
ft = QgsFeature()
18-
ft.setFeatureId(fid)
19-
ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(x, y)))
20-
idx.insertFeature(ft)
21-
fid += 1
22-
23-
# intersection test
24-
rect = QgsRectangle(7.0, 3.0, 17.0, 13.0)
25-
fids = idx.intersects(rect)
26-
27-
assert len(fids) == 0, "No intersections"
28-
29-
fids.sort()
30-
myMessage = ('Expected: %s\nGot: %s\n' %
31-
([1, 2, 5, 6], fids))
32-
assert fids == [1, 2, 5, 6], myMessage
33-
34-
# nearest neighbor test
35-
fids = idx.nearestNeighbor(QgsPoint(8.75, 6.25), 3)
36-
assert len(fids) == 0, "No intersections"
37-
38-
fids.sort()
39-
myMessage = ('Expected: %s\nGot: %s\n' %
40-
([0, 1, 5], fids))
41-
assert fids == [0, 1, 5], myMessage
15+
idx = QgsSpatialIndex()
16+
fid = 0
17+
for y in range(5, 15, 5):
18+
for x in range(5, 25, 5):
19+
ft = QgsFeature()
20+
ft.setFeatureId(fid)
21+
ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(x, y)))
22+
idx.insertFeature(ft)
23+
fid += 1
24+
25+
# intersection test
26+
rect = QgsRectangle(7.0, 3.0, 17.0, 13.0)
27+
fids = idx.intersects(rect)
28+
myExpectedValue = 4
29+
myValue = len(fids)
30+
myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myValue)
31+
self.assertEqual(myValue, myExpectedValue, myMessage)
32+
fids.sort()
33+
myMessage = ('Expected: %s\nGot: %s\n' %
34+
([1, 2, 5, 6], fids))
35+
assert fids == [1, 2, 5, 6], myMessage
36+
37+
# nearest neighbor test
38+
fids = idx.nearestNeighbor(QgsPoint(8.75, 6.25), 3)
39+
myExpectedValue = 0
40+
myValue = len(fids)
41+
myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myValue)
42+
43+
fids.sort()
44+
myMessage = ('Expected: %s\nGot: %s\n' %
45+
([0, 1, 5], fids))
46+
assert fids == [0, 1, 5], myMessage

‎tests/src/python/utilities.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import sys
33
from PyQt4 import QtGui, QtCore
44
from qgis.core import (QgsApplication,
5-
QgsVectorLayer,
6-
QgsRasterLayer,
7-
QgsRectangle,
85
QgsCoordinateReferenceSystem)
96
from qgis.gui import QgsMapCanvas
107
from qgis_interface import QgisInterface
@@ -68,7 +65,8 @@ def getQgisTestApp():
6865
if QGISAPP is None:
6966
myGuiFlag = True # All test will run qgis in gui mode
7067

71-
# Note: QGIS_PREFIX_PATH is evaluated in QgsApplication - no need to mess with it here.
68+
# Note: QGIS_PREFIX_PATH is evaluated in QgsApplication -
69+
# no need to mess with it here.
7270
QGISAPP = QgsApplication(sys.argv, myGuiFlag)
7371

7472
QGISAPP.initQgis()

0 commit comments

Comments
 (0)
Please sign in to comment.