Skip to content

Commit

Permalink
Fixes for QgsSpatialIndex python test
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Oct 3, 2012
1 parent 80d645d commit 5a0feb3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
67 changes: 36 additions & 31 deletions tests/src/python/test_qgsspatialindex.py
@@ -1,41 +1,46 @@
import unittest

from qgis.core import (QGis,
from qgis.core import (QgsSpatialIndex,
QgsFeature,
QgsGeometry,
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
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)
myExpectedValue = 4
myValue = len(fids)
myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myValue)
self.assertEqual(myValue, myExpectedValue, myMessage)
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)
myExpectedValue = 0
myValue = len(fids)
myMessage = 'Expected: %s Got: %s' % (myExpectedValue, myValue)

fids.sort()
myMessage = ('Expected: %s\nGot: %s\n' %
([0, 1, 5], fids))
assert fids == [0, 1, 5], myMessage
6 changes: 2 additions & 4 deletions tests/src/python/utilities.py
Expand Up @@ -2,9 +2,6 @@
import sys
from PyQt4 import QtGui, QtCore
from qgis.core import (QgsApplication,
QgsVectorLayer,
QgsRasterLayer,
QgsRectangle,
QgsCoordinateReferenceSystem)
from qgis.gui import QgsMapCanvas
from qgis_interface import QgisInterface
Expand Down Expand Up @@ -68,7 +65,8 @@ def getQgisTestApp():
if QGISAPP is None:
myGuiFlag = True # All test will run qgis in gui mode

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

QGISAPP.initQgis()
Expand Down

0 comments on commit 5a0feb3

Please sign in to comment.