Skip to content

Commit a1479a7

Browse files
committedNov 18, 2015
QgsMapUnitScale fixes:
- add docs - add missing SIP bindings - remove unnecessary cast from double->float->double - add unit tests (cherry-picked from f0a2fc1)
1 parent 7b59fc0 commit a1479a7

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed
 

‎python/core/qgsmapunitscale.sip

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ class QgsMapUnitScale
1313
#include <qgsmapunitscale.h>
1414
%End
1515
public:
16-
QgsMapUnitScale();
17-
QgsMapUnitScale( float _minScale, float _maxScale );
16+
17+
/** Constructor for QgsMapUnitScale
18+
* @param minScale minimum allowed scale, or 0.0 if no minimum scale set
19+
* @param maxScale maximum allowed scale, or 0.0 if no maximum scale set
20+
*/
21+
QgsMapUnitScale( double minScale = 0.0, double maxScale = 0.0 );
1822

1923
/** The minimum scale, or 0.0 if unset */
2024
double minScale;
2125
/** The maximum scale, or 0.0 if unset */
2226
double maxScale;
2327

28+
/** Computes a map units per pixel scaling factor, respecting the minimum and maximum scales
29+
* set for the object.
30+
* @param c render context
31+
* @returns map units per pixel, limited between minimum and maximum scales
32+
*/
2433
double computeMapUnitsPerPixel( const QgsRenderContext& c ) const;
34+
35+
bool operator==( const QgsMapUnitScale& other ) const;
36+
bool operator!=( const QgsMapUnitScale& other ) const;
2537
};

‎src/core/qgsmapunitscale.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,35 @@
2121
#include <QtCore>
2222
#include "qgsrendercontext.h"
2323

24+
/**
25+
* \class QgsMapUnitScale
26+
* \brief Struct for storing maximum and minimum scales for measurements in map units
27+
*
28+
* For measurements in map units, a minimum and a maximum scale can be defined.
29+
* Outside this range, the measurements aren't scaled anymore proportionally to
30+
* the map scale.
31+
*/
32+
2433
class CORE_EXPORT QgsMapUnitScale
2534
{
2635
public:
27-
QgsMapUnitScale() : minScale( 0.0 ), maxScale( 0.0 ) {}
28-
QgsMapUnitScale( float _minScale, float _maxScale ) : minScale( _minScale ), maxScale( _maxScale ) {}
36+
37+
/** Constructor for QgsMapUnitScale
38+
* @param minScale minimum allowed scale, or 0.0 if no minimum scale set
39+
* @param maxScale maximum allowed scale, or 0.0 if no maximum scale set
40+
*/
41+
QgsMapUnitScale( double minScale = 0.0, double maxScale = 0.0 ) : minScale( minScale ), maxScale( maxScale ) {}
2942

3043
/** The minimum scale, or 0.0 if unset */
3144
double minScale;
3245
/** The maximum scale, or 0.0 if unset */
3346
double maxScale;
3447

48+
/** Computes a map units per pixel scaling factor, respecting the minimum and maximum scales
49+
* set for the object.
50+
* @param c render context
51+
* @returns map units per pixel, limited between minimum and maximum scales
52+
*/
3553
double computeMapUnitsPerPixel( const QgsRenderContext& c ) const
3654
{
3755
double mup = c.mapToPixel().mapUnitsPerPixel();

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
1616
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
1717
ADD_PYTHON_TEST(PyQgsRectangle test_qgsrectangle.py)
1818
ADD_PYTHON_TEST(PyQgsRelation test_qgsrelation.py)
19+
ADD_PYTHON_TEST(PyQgsMapUnitScale test_qgsmapunitscale.py)
1920
ADD_PYTHON_TEST(PyQgsSpatialIndex test_qgsspatialindex.py)
2021
ADD_PYTHON_TEST(PyQgsComposerHtml test_qgscomposerhtml.py)
2122
ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)
Please sign in to comment.