|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsDistanceArea. |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Jürgen E. Fischer' |
| 10 | +__date__ = '19/01/2014' |
| 11 | +__copyright__ = 'Copyright 2014, The QGIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import qgis |
| 18 | + |
| 19 | +from qgis.core import (QgsGeometry, |
| 20 | + QgsPoint, |
| 21 | + QgsDistanceArea, |
| 22 | + QGis) |
| 23 | + |
| 24 | +from utilities import (getQgisTestApp, |
| 25 | + TestCase, |
| 26 | + unittest) |
| 27 | + |
| 28 | +from PyQt4.QtCore import qDebug |
| 29 | + |
| 30 | +# Convenience instances in case you may need them |
| 31 | +# not used in this test |
| 32 | + |
| 33 | +QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() |
| 34 | + |
| 35 | +class TestQgsDistanceArea(TestCase): |
| 36 | + |
| 37 | + def testMeasureLine(self): |
| 38 | + # +-+ |
| 39 | + # | | |
| 40 | + # +-+ + |
| 41 | + linestring = QgsGeometry.fromPolyline( |
| 42 | + [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ] |
| 43 | + ) |
| 44 | + da = QgsDistanceArea() |
| 45 | + length = da.measure(linestring) |
| 46 | + myMessage = ('Expected:\n%f\nGot:\n%f\n' % |
| 47 | + (4, length)) |
| 48 | + assert length == 4, myMessage |
| 49 | + |
| 50 | + def testMeasureMultiLine(self): |
| 51 | + # +-+ +-+-+ |
| 52 | + # | | | | |
| 53 | + # +-+ + + +-+ |
| 54 | + linestring = QgsGeometry.fromMultiPolyline( |
| 55 | + [ |
| 56 | + [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ], |
| 57 | + [ QgsPoint(3,0), QgsPoint(3,1), QgsPoint(5,1), QgsPoint(5,0), QgsPoint(6,0), ] |
| 58 | + ] |
| 59 | + ) |
| 60 | + da = QgsDistanceArea() |
| 61 | + length = da.measure(linestring) |
| 62 | + myMessage = ('Expected:\n%f\nGot:\n%f\n' % |
| 63 | + (9, length)) |
| 64 | + assert length == 9, myMessage |
| 65 | + |
| 66 | + def testMeasurePolygon(self): |
| 67 | + # +-+-+ |
| 68 | + # | | |
| 69 | + # + +-+ |
| 70 | + # | | |
| 71 | + # +-+ |
| 72 | + polygon = QgsGeometry.fromPolygon( |
| 73 | + [[ |
| 74 | + QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0), |
| 75 | + ]] |
| 76 | + ) |
| 77 | + |
| 78 | + da = QgsDistanceArea() |
| 79 | + area = da.measure(polygon) |
| 80 | + assert area == 3, 'Expected:\n%f\nGot:\n%f\n' % (3, area) |
| 81 | + |
| 82 | + perimeter = da.measurePerimeter(polygon) |
| 83 | + assert perimeter == 8, 'Expected:\n%f\nGot:\n%f\n' % (8, perimeter) |
| 84 | + |
| 85 | + def testMeasurePolygonWithHole(self): |
| 86 | + # +-+-+-+ |
| 87 | + # | | |
| 88 | + # + +-+ + |
| 89 | + # | | | | |
| 90 | + # + +-+ + |
| 91 | + # | | |
| 92 | + # +-+-+-+ |
| 93 | + polygon = QgsGeometry.fromPolygon( |
| 94 | + [ |
| 95 | + [ QgsPoint(0,0), QgsPoint(3,0), QgsPoint(3,3), QgsPoint(0,3), QgsPoint(0,0) ], |
| 96 | + [ QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(1,2), QgsPoint(1,1) ], |
| 97 | + ] |
| 98 | + ) |
| 99 | + da = QgsDistanceArea() |
| 100 | + area = da.measure(polygon) |
| 101 | + assert area == 8, "Expected:\n%f\nGot:\n%f\n" % (8, area) |
| 102 | + |
| 103 | + perimeter = da.measurePerimeter(polygon) |
| 104 | + assert perimeter == 12, "Expected:\n%f\nGot:\n%f\n" % (12, perimeter) |
| 105 | + |
| 106 | + def testMeasureMultiPolygon(self): |
| 107 | + # +-+-+ +-+-+ |
| 108 | + # | | | | |
| 109 | + # + +-+ +-+ + |
| 110 | + # | | | | |
| 111 | + # +-+ +-+ |
| 112 | + polygon = QgsGeometry.fromMultiPolygon( |
| 113 | + [ |
| 114 | + [ [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0), ] ], |
| 115 | + [ [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(5,2), QgsPoint(3,2), QgsPoint(3,1), QgsPoint(4,1), QgsPoint(4,0), ] ] |
| 116 | + ] |
| 117 | + ) |
| 118 | + |
| 119 | + da = QgsDistanceArea() |
| 120 | + area = da.measure(polygon) |
| 121 | + assert area == 6, 'Expected:\n%f\nGot:\n%f\n' % (6, area) |
| 122 | + |
| 123 | + perimeter = da.measurePerimeter(polygon) |
| 124 | + assert perimeter == 16, "Expected:\n%f\nGot:\n%f\n" % (16, perimeter) |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + unittest.main() |
| 128 | + |
0 commit comments