|
1 | 1 | import unittest
|
2 | 2 |
|
3 |
| -from qgis.core import (QGis, |
| 3 | +from qgis.core import (QgsSpatialIndex, |
| 4 | + QgsFeature, |
| 5 | + QgsGeometry, |
4 | 6 | QgsRectangle,
|
5 | 7 | QgsPoint)
|
6 | 8 |
|
7 | 9 | from utilities import getQgisTestApp
|
| 10 | + |
8 | 11 | QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
|
9 | 12 |
|
10 | 13 | class TestQgsSpatialIndex(unittest.TestCase):
|
11 |
| - |
12 | 14 | 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 |
0 commit comments