|
1 | 1 | import unittest
|
2 | 2 |
|
3 | 3 | from qgis.core import (QgsGeometry,
|
| 4 | + QgsVectorLayer, |
| 5 | + QgsFeature, |
4 | 6 | QgsPoint,
|
5 | 7 | QGis)
|
6 | 8 |
|
7 | 9 | # Convenience instances in case you may need them
|
8 | 10 | # 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() |
11 | 13 |
|
12 | 14 | class TestQgsGeometry(unittest.TestCase):
|
13 | 15 |
|
@@ -55,6 +57,95 @@ def testFromMultiPolygon(self):
|
55 | 57 | (QGis.WKBMultiPolygon, myMultiPolygon.type()))
|
56 | 58 | assert myMultiPolygon.wkbType() == QGis.WKBMultiPolygon, myMessage
|
57 | 59 |
|
| 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 | + |
58 | 149 |
|
59 | 150 | if __name__ == '__main__':
|
60 | 151 | unittest.main()
|
|
0 commit comments