-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python tests for QgsRectangle and QgsSpatialIndex
- Loading branch information
Showing
3 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |