Skip to content

Commit 230417c

Browse files
committedOct 3, 2016
Add method to create QgsMapToPixel from scale/dpi/mapunits
1 parent 4b7876c commit 230417c

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed
 

‎python/core/qgsmaptopixel.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class QgsMapToPixel
3030
*/
3131
QgsMapToPixel( double mapUnitsPerPixel );
3232

33+
/** Returns a new QgsMapToPixel created using a specified scale and distance unit.
34+
* @param scale map scale
35+
* @param dpi screen DPI
36+
* @param mapUnits map units
37+
* @returns matching QgsMapToPixel
38+
* @note added in QGIS 3.0
39+
*/
40+
static QgsMapToPixel fromScale( double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi = 96 );
41+
3342
/**
3443
* Constructor
3544
*

‎src/core/qgsmaptopixel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgslogger.h"
2525
#include "qgspoint.h"
2626

27+
2728
QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
2829
double xc,
2930
double yc,
@@ -56,6 +57,13 @@ QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel )
5657
updateMatrix();
5758
}
5859

60+
QgsMapToPixel QgsMapToPixel::fromScale( double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi )
61+
{
62+
double metresPerPixel = 25.4 / dpi / 1000.0;
63+
double mapUnitsPerPixel = metresPerPixel * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::DistanceMeters, mapUnits );
64+
return QgsMapToPixel( mapUnitsPerPixel / scale );
65+
}
66+
5967
QgsMapToPixel::QgsMapToPixel()
6068
: mMapUnitsPerPixel( 1 )
6169
, mWidth( 1 )

‎src/core/qgsmaptopixel.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include <QTransform>
2121
#include <vector>
22-
22+
#include "qgsunittypes.h"
2323
#include <cassert>
2424

2525
class QgsPoint;
@@ -52,6 +52,15 @@ class CORE_EXPORT QgsMapToPixel
5252
*/
5353
QgsMapToPixel( double mapUnitsPerPixel );
5454

55+
/** Returns a new QgsMapToPixel created using a specified scale and distance unit.
56+
* @param scale map scale
57+
* @param dpi screen DPI
58+
* @param mapUnits map units
59+
* @returns matching QgsMapToPixel
60+
* @note added in QGIS 3.0
61+
*/
62+
static QgsMapToPixel fromScale( double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi = 96 );
63+
5564
/**
5665
* Constructor
5766
*

‎tests/src/core/testqgsmaptopixel.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
#include <qgsmaptopixel.h>
2121
#include <qgspoint.h>
2222
#include "qgslogger.h"
23+
#include "qgstestutils.h"
2324

2425
class TestQgsMapToPixel: public QObject
2526
{
2627
Q_OBJECT
2728
private slots:
2829
void rotation();
2930
void getters();
31+
void fromScale();
3032
};
3133

3234
void TestQgsMapToPixel::rotation()
@@ -92,6 +94,18 @@ void TestQgsMapToPixel::getters()
9294
QCOMPARE( m2p.mapUnitsPerPixel(), 2.0 );
9395
}
9496

97+
void TestQgsMapToPixel::fromScale()
98+
{
99+
QgsMapToPixel m2p = QgsMapToPixel::fromScale( 0.001, QgsUnitTypes::DistanceMeters, 96.0 );
100+
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.264583, 0.000001 );
101+
m2p = QgsMapToPixel::fromScale( 0.0001, QgsUnitTypes::DistanceMeters, 96.0 );
102+
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 2.645833, 0.000001 );
103+
m2p = QgsMapToPixel::fromScale( 0.001, QgsUnitTypes::DistanceMeters, 72.0 );
104+
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.352778, 0.000001 );
105+
m2p = QgsMapToPixel::fromScale( 0.001, QgsUnitTypes::DistanceKilometers, 96.0 );
106+
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.000265, 0.000001 );
107+
}
108+
95109
QTEST_MAIN( TestQgsMapToPixel )
96110
#include "testqgsmaptopixel.moc"
97111

0 commit comments

Comments
 (0)
Please sign in to comment.