|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsMapUnitScale. |
| 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__ = '2015-09' |
| 11 | +__copyright__ = 'Copyright 2015, 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 |
| 16 | + |
| 17 | +from qgis.core import (QgsMapUnitScale, QgsRenderContext, QgsMapSettings, QgsRectangle) |
| 18 | +from PyQt4.QtCore import QSize |
| 19 | +from utilities import (TestCase, unittest) |
| 20 | + |
| 21 | + |
| 22 | +class PyQgsMapUnitScale(TestCase): |
| 23 | + |
| 24 | + def testConstructor(self): |
| 25 | + #test creating QgsMapUnitScale |
| 26 | + c = QgsMapUnitScale() |
| 27 | + self.assertEqual(c.minScale, 0) |
| 28 | + self.assertEqual(c.maxScale, 0) |
| 29 | + |
| 30 | + c = QgsMapUnitScale(0.0001, 0.005) |
| 31 | + self.assertEqual(c.minScale, 0.0001) |
| 32 | + self.assertEqual(c.maxScale, 0.005) |
| 33 | + |
| 34 | + def testEquality(self): |
| 35 | + #test equality operator |
| 36 | + |
| 37 | + c1 = QgsMapUnitScale(0.0001, 0.005) |
| 38 | + c2 = QgsMapUnitScale(0.0001, 0.005) |
| 39 | + self.assertEqual(c1, c2) |
| 40 | + |
| 41 | + c2.minScale = 0.0004 |
| 42 | + self.assertNotEqual(c1, c2) |
| 43 | + |
| 44 | + c2.minScale = 0.0001 |
| 45 | + c2.maxScale = 0.007 |
| 46 | + self.assertNotEqual(c1, c2) |
| 47 | + |
| 48 | + c2.maxScale = 0.005 |
| 49 | + self.assertEqual(c1, c2) |
| 50 | + |
| 51 | + def testMapUnitsPerPixel(self): |
| 52 | + #test computeMapUnitsPerPixel |
| 53 | + |
| 54 | + ms = QgsMapSettings() |
| 55 | + ms.setExtent(QgsRectangle(0, 0, 100, 100)) |
| 56 | + ms.setOutputSize(QSize(100, 50)) |
| 57 | + r = QgsRenderContext.fromMapSettings(ms) |
| 58 | + |
| 59 | + #renderer scale should be about 1:291937841 |
| 60 | + |
| 61 | + #start with no min/max scale |
| 62 | + c = QgsMapUnitScale() |
| 63 | + |
| 64 | + mup = c.computeMapUnitsPerPixel(r) |
| 65 | + self.assertAlmostEqual(mup, 2.0, places=5) |
| 66 | + |
| 67 | + #add a minimum scale less than the renderer scale, so should be no change |
| 68 | + c.minScale = 1 / 350000000.0 |
| 69 | + mup = c.computeMapUnitsPerPixel(r) |
| 70 | + self.assertAlmostEqual(mup, 2.0, places=5) |
| 71 | + |
| 72 | + #minimum scale greater than the renderer scale, so should be limited to minScale |
| 73 | + c.minScale = 1 / 150000000.0 |
| 74 | + mup = c.computeMapUnitsPerPixel(r) |
| 75 | + self.assertAlmostEqual(mup, 1.0276160, places=5) |
| 76 | + c.minScale = 1 / 50000000.0 |
| 77 | + mup = c.computeMapUnitsPerPixel(r) |
| 78 | + self.assertAlmostEqual(mup, 0.3425386, places=5) |
| 79 | + c.minScale = 1 / 350000000.0 |
| 80 | + |
| 81 | + #add a maximum scale greater than the renderer scale, so should be no change |
| 82 | + c.maxScale = 1 / 150000000.0 |
| 83 | + mup = c.computeMapUnitsPerPixel(r) |
| 84 | + self.assertAlmostEqual(mup, 2.0, places=5) |
| 85 | + |
| 86 | + #maximum scale less than the renderer scale, so should be limited to maxScale |
| 87 | + c.maxScale = 1 / 350000000.0 |
| 88 | + mup = c.computeMapUnitsPerPixel(r) |
| 89 | + self.assertAlmostEqual(mup, 2.3977706, places=5) |
| 90 | + c.maxScale = 1 / 500000000.0 |
| 91 | + mup = c.computeMapUnitsPerPixel(r) |
| 92 | + self.assertAlmostEqual(mup, 3.4253867, places=5) |
| 93 | + |
| 94 | + #test resetting to min/max |
| 95 | + c.minScale = 0 |
| 96 | + c.maxScale = 0 |
| 97 | + mup = c.computeMapUnitsPerPixel(r) |
| 98 | + self.assertAlmostEqual(mup, 2.0, places=5) |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + unittest.main() |
0 commit comments