Skip to content
Permalink
Browse files

Fixes for QgsSpatialIndex python test

  • Loading branch information
timlinux committed Oct 3, 2012
1 parent 80d645d commit 5a0feb3f3197dc2d0eb0f28e7ddb71d1202d2a5d
Showing with 38 additions and 35 deletions.
  1. +36 −31 tests/src/python/test_qgsspatialindex.py
  2. +2 −4 tests/src/python/utilities.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
@@ -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
@@ -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()

0 comments on commit 5a0feb3

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