|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsCRSCache. |
| 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__ = '06/06/2016' |
| 11 | +__copyright__ = 'Copyright 2016, 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 (QgsCRSCache, |
| 18 | + QgsCoordinateReferenceSystem, |
| 19 | + QGis, |
| 20 | + QgsUnitTypes |
| 21 | + ) |
| 22 | + |
| 23 | +from qgis.testing import start_app, unittest |
| 24 | +from qgis.PyQt.QtCore import QLocale |
| 25 | + |
| 26 | +# Convenience instances in case you may need them |
| 27 | +# not used in this test |
| 28 | + |
| 29 | +start_app() |
| 30 | + |
| 31 | + |
| 32 | +class TestQgsCRSCache(unittest.TestCase): |
| 33 | + |
| 34 | + def testInstance(self): |
| 35 | + """ test retrieving global instance """ |
| 36 | + self.assertTrue(QgsCRSCache.instance()) |
| 37 | + |
| 38 | + def testcrsByOgcWmsCrs(self): |
| 39 | + """ test retrieving CRS from cache using Ogc WMS definition """ |
| 40 | + |
| 41 | + crs = QgsCRSCache.instance().crsByOgcWmsCrs('EPSG:4326') |
| 42 | + self.assertTrue(crs.isValid()) |
| 43 | + self.assertEqual(crs.authid(), 'EPSG:4326') |
| 44 | + # a second time, so crs is fetched from cache |
| 45 | + crs = QgsCRSCache.instance().crsByOgcWmsCrs('EPSG:4326') |
| 46 | + self.assertTrue(crs.isValid()) |
| 47 | + self.assertEqual(crs.authid(), 'EPSG:4326') |
| 48 | + |
| 49 | + # invalid |
| 50 | + crs = QgsCRSCache.instance().crsByOgcWmsCrs('i am not a CRS') |
| 51 | + self.assertFalse(crs.isValid()) |
| 52 | + # a second time, so invalid crs is fetched from cache |
| 53 | + crs = QgsCRSCache.instance().crsByOgcWmsCrs('i am not a CRS') |
| 54 | + self.assertFalse(crs.isValid()) |
| 55 | + |
| 56 | + def testcrsByEpsgId(self): |
| 57 | + """ test retrieving CRS from cache using EPSG id """ |
| 58 | + |
| 59 | + crs = QgsCRSCache.instance().crsByEpsgId(3111) |
| 60 | + self.assertTrue(crs.isValid()) |
| 61 | + self.assertEqual(crs.authid(), 'EPSG:3111') |
| 62 | + # a second time, so crs is fetched from cache |
| 63 | + crs = QgsCRSCache.instance().crsByEpsgId(3111) |
| 64 | + self.assertTrue(crs.isValid()) |
| 65 | + self.assertEqual(crs.authid(), 'EPSG:3111') |
| 66 | + |
| 67 | + # invalid |
| 68 | + crs = QgsCRSCache.instance().crsByEpsgId(-9999) |
| 69 | + self.assertFalse(crs.isValid()) |
| 70 | + # a second time, so invalid crs is fetched from cache |
| 71 | + crs = QgsCRSCache.instance().crsByEpsgId(-9999) |
| 72 | + self.assertFalse(crs.isValid()) |
| 73 | + |
| 74 | + def testcrsByProj4(self): |
| 75 | + """ test retrieving CRS from cache using proj4 """ |
| 76 | + |
| 77 | + crs = QgsCRSCache.instance().crsByProj4('+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ') |
| 78 | + self.assertTrue(crs.isValid()) |
| 79 | + self.assertEqual(crs.authid(), 'EPSG:28355') |
| 80 | + # a second time, so crs is fetched from cache |
| 81 | + crs = QgsCRSCache.instance().crsByProj4('+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ') |
| 82 | + self.assertTrue(crs.isValid()) |
| 83 | + self.assertEqual(crs.authid(), 'EPSG:28355') |
| 84 | + |
| 85 | + # invalid |
| 86 | + crs = QgsCRSCache.instance().crsByProj4('asdasdasd') |
| 87 | + self.assertFalse(crs.isValid()) |
| 88 | + # a second time, so invalid crs is fetched from cache |
| 89 | + crs = QgsCRSCache.instance().crsByProj4('asdasdasd') |
| 90 | + self.assertFalse(crs.isValid()) |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + unittest.main() |
0 commit comments