Skip to content

Commit f5c7753

Browse files
committedAug 24, 2012
Merge pull request #214 from slarosa/master
Added tests to Geometry test
2 parents d0bf482 + 11ce55c commit f5c7753

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed
 

‎tests/src/python/test_qgsgeometry.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import unittest
22

33
from qgis.core import (QgsGeometry,
4+
QgsVectorLayer,
5+
QgsFeature,
46
QgsPoint,
57
QGis)
68

79
# Convenience instances in case you may need them
810
# not used in this test
9-
#from utilities import getQgisTestApp
10-
#QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
11+
from utilities import getQgisTestApp
12+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
1113

1214
class TestQgsGeometry(unittest.TestCase):
1315

@@ -55,6 +57,95 @@ def testFromMultiPolygon(self):
5557
(QGis.WKBMultiPolygon, myMultiPolygon.type()))
5658
assert myMultiPolygon.wkbType() == QGis.WKBMultiPolygon, myMessage
5759

60+
def testIntersection(self):
61+
myLine = QgsGeometry.fromPolyline([QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 2)])
62+
myPoint = QgsGeometry.fromPoint(QgsPoint(1, 1))
63+
intersectionGeom = QgsGeometry.intersection(myLine, myPoint)
64+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
65+
(QGis.Point, intersectionGeom.type()))
66+
assert intersectionGeom.wkbType() == QGis.WKBPoint, myMessage
67+
68+
layer = QgsVectorLayer("Point", "intersection", "memory")
69+
assert layer.isValid(), "Failed to create valid point memory layer"
70+
71+
provider = layer.dataProvider()
72+
73+
ft = QgsFeature()
74+
ft.setGeometry(intersectionGeom)
75+
provider.addFeatures([ft])
76+
77+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
78+
(1, layer.featureCount()))
79+
assert layer.featureCount() == 1, myMessage
80+
81+
def testBuffer(self):
82+
myPoint = QgsGeometry.fromPoint(QgsPoint(1, 1))
83+
bufferGeom = myPoint.buffer(10, 5)
84+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
85+
(QGis.Polygon, bufferGeom.type()))
86+
assert bufferGeom.wkbType() == QGis.WKBPolygon, myMessage
87+
88+
layer = QgsVectorLayer("Polygon", "buffer", "memory")
89+
assert layer.isValid(), "Failed to create valid polygon memory layer"
90+
91+
provider = layer.dataProvider()
92+
93+
ft = QgsFeature()
94+
ft.setGeometry(bufferGeom)
95+
provider.addFeatures([ft])
96+
97+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
98+
(1, layer.featureCount()))
99+
assert layer.featureCount() == 1, myMessage
100+
101+
def testContains(self):
102+
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
103+
myPoint = QgsGeometry.fromPoint(QgsPoint(1, 1))
104+
containsGeom = QgsGeometry.contains(myPoly, myPoint)
105+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
106+
("True", containsGeom))
107+
assert containsGeom == True, myMessage
108+
109+
def testTouches(self):
110+
myLine = QgsGeometry.fromPolyline([QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 2)])
111+
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(2, 0),QgsPoint(0, 0)]])
112+
touchesGeom = QgsGeometry.touches(myLine, myPoly)
113+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
114+
("True", touchesGeom))
115+
assert touchesGeom == True, myMessage
116+
117+
def testOverlaps(self):
118+
myPolyA = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(1, 3),QgsPoint(2, 0),QgsPoint(0, 0)]])
119+
myPolyB = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
120+
overlapsGeom = QgsGeometry.overlaps(myPolyA, myPolyB)
121+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
122+
("True", overlapsGeom))
123+
assert overlapsGeom == True, myMessage
124+
125+
def testWithin(self):
126+
myLine = QgsGeometry.fromPolyline([QgsPoint(0.5, 0.5),QgsPoint(1, 1),QgsPoint(1.5, 1.5)])
127+
myPoly = QgsGeometry.fromPolygon([[QgsPoint(0, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(0, 2), QgsPoint(0, 0)]])
128+
withinGeom = QgsGeometry.within(myLine, myPoly)
129+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
130+
("True", withinGeom))
131+
assert withinGeom == True, myMessage
132+
133+
def testEquals(self):
134+
myPointA = QgsGeometry.fromPoint(QgsPoint(1, 1))
135+
myPointB = QgsGeometry.fromPoint(QgsPoint(1, 1))
136+
equalsGeom = QgsGeometry.equals(myPointA, myPointB)
137+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
138+
("True", equalsGeom))
139+
assert equalsGeom == True, myMessage
140+
141+
def testCrosses(self):
142+
myLine = QgsGeometry.fromPolyline([QgsPoint(0, 0),QgsPoint(1, 1),QgsPoint(3, 3)])
143+
myPoly = QgsGeometry.fromPolygon([[QgsPoint(1, 0),QgsPoint(2, 0),QgsPoint(2, 2),QgsPoint(1, 2), QgsPoint(1, 0)]])
144+
crossesGeom = QgsGeometry.crosses(myLine, myPoly)
145+
myMessage = ('Expected:\n%s\nGot:\n%s\n' %
146+
("True", crossesGeom))
147+
assert crossesGeom == True, myMessage
148+
58149

59150
if __name__ == '__main__':
60151
unittest.main()

0 commit comments

Comments
 (0)