|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsReferencedGeometry. |
| 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__ = 'Nyall Dawson' |
| 10 | +__date__ = '31/08/2017' |
| 11 | +__copyright__ = 'Copyright 2017, 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 qgis # NOQA |
| 16 | + |
| 17 | +from qgis.core import (QgsRectangle, |
| 18 | + QgsPointXY, |
| 19 | + QgsReferencedRectangle, |
| 20 | + QgsReferencedPointXY, |
| 21 | + QgsCoordinateReferenceSystem) |
| 22 | +from qgis.PyQt.QtCore import QVariant |
| 23 | +from qgis.testing import start_app, unittest |
| 24 | +from utilities import compareWkt |
| 25 | + |
| 26 | +start_app() |
| 27 | + |
| 28 | + |
| 29 | +class TestQgsReferencedGeometry(unittest.TestCase): |
| 30 | + |
| 31 | + def testRectangle(self): |
| 32 | + rect = QgsReferencedRectangle(QgsRectangle(0.0, 1.0, 20.0, 10.0), QgsCoordinateReferenceSystem('epsg:3111')) |
| 33 | + self.assertEqual(rect.xMinimum(), 0.0) |
| 34 | + self.assertEqual(rect.yMinimum(), 1.0) |
| 35 | + self.assertEqual(rect.xMaximum(), 20.0) |
| 36 | + self.assertEqual(rect.yMaximum(), 10.0) |
| 37 | + self.assertEqual(rect.crs().authid(), 'EPSG:3111') |
| 38 | + |
| 39 | + rect.setCrs(QgsCoordinateReferenceSystem('epsg:28356')) |
| 40 | + self.assertEqual(rect.crs().authid(), 'EPSG:28356') |
| 41 | + |
| 42 | + # in variant |
| 43 | + v = QVariant(QgsReferencedRectangle(QgsRectangle(1.0, 2.0, 3.0, 4.0), QgsCoordinateReferenceSystem('epsg:3111'))) |
| 44 | + self.assertEqual(v.value().xMinimum(), 1.0) |
| 45 | + self.assertEqual(v.value().yMinimum(), 2.0) |
| 46 | + self.assertEqual(v.value().xMaximum(), 3.0) |
| 47 | + self.assertEqual(v.value().yMaximum(), 4.0) |
| 48 | + self.assertEqual(v.value().crs().authid(), 'EPSG:3111') |
| 49 | + |
| 50 | + # to rectangle |
| 51 | + r = QgsRectangle(rect) |
| 52 | + self.assertEqual(r.xMinimum(), 0.0) |
| 53 | + self.assertEqual(r.yMinimum(), 1.0) |
| 54 | + self.assertEqual(r.xMaximum(), 20.0) |
| 55 | + self.assertEqual(r.yMaximum(), 10.0) |
| 56 | + |
| 57 | + # test that QgsReferencedRectangle IS a QgsRectangle |
| 58 | + r2 = QgsRectangle(5, 6, 30, 40) |
| 59 | + r2.combineExtentWith(rect) |
| 60 | + self.assertEqual(r2.xMinimum(), 0.0) |
| 61 | + self.assertEqual(r2.yMinimum(), 1.0) |
| 62 | + self.assertEqual(r2.xMaximum(), 30.0) |
| 63 | + self.assertEqual(r2.yMaximum(), 40.0) |
| 64 | + |
| 65 | + def testPoint(self): |
| 66 | + point = QgsReferencedPointXY(QgsPointXY(1.0, 2.0), QgsCoordinateReferenceSystem('epsg:3111')) |
| 67 | + self.assertEqual(point.x(), 1.0) |
| 68 | + self.assertEqual(point.y(), 2.0) |
| 69 | + self.assertEqual(point.crs().authid(), 'EPSG:3111') |
| 70 | + |
| 71 | + point.setCrs(QgsCoordinateReferenceSystem('epsg:28356')) |
| 72 | + self.assertEqual(point.crs().authid(), 'EPSG:28356') |
| 73 | + |
| 74 | + # in variant |
| 75 | + v = QVariant(QgsReferencedPointXY(QgsPointXY(3.0, 4.0), QgsCoordinateReferenceSystem('epsg:3111'))) |
| 76 | + self.assertEqual(v.value().x(), 3.0) |
| 77 | + self.assertEqual(v.value().y(), 4.0) |
| 78 | + self.assertEqual(v.value().crs().authid(), 'EPSG:3111') |
| 79 | + |
| 80 | + # to QgsPointXY |
| 81 | + p = QgsPointXY(point) |
| 82 | + self.assertEqual(p.x(), 1.0) |
| 83 | + self.assertEqual(p.y(), 2.0) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + unittest.main() |
0 commit comments