Skip to content

Commit

Permalink
Add method to create QgsMapToPixel from scale/dpi/mapunits
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 3, 2016
1 parent 4b7876c commit 230417c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions python/core/qgsmaptopixel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class QgsMapToPixel
*/
QgsMapToPixel( double mapUnitsPerPixel );

/** Returns a new QgsMapToPixel created using a specified scale and distance unit.
* @param scale map scale
* @param dpi screen DPI
* @param mapUnits map units
* @returns matching QgsMapToPixel
* @note added in QGIS 3.0
*/
static QgsMapToPixel fromScale( double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi = 96 );

/**
* Constructor
*
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsmaptopixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgslogger.h"
#include "qgspoint.h"


QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
double xc,
double yc,
Expand Down Expand Up @@ -56,6 +57,13 @@ QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel )
updateMatrix();
}

QgsMapToPixel QgsMapToPixel::fromScale( double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi )
{
double metresPerPixel = 25.4 / dpi / 1000.0;
double mapUnitsPerPixel = metresPerPixel * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::DistanceMeters, mapUnits );
return QgsMapToPixel( mapUnitsPerPixel / scale );
}

QgsMapToPixel::QgsMapToPixel()
: mMapUnitsPerPixel( 1 )
, mWidth( 1 )
Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsmaptopixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <QTransform>
#include <vector>

#include "qgsunittypes.h"
#include <cassert>

class QgsPoint;
Expand Down Expand Up @@ -52,6 +52,15 @@ class CORE_EXPORT QgsMapToPixel
*/
QgsMapToPixel( double mapUnitsPerPixel );

/** Returns a new QgsMapToPixel created using a specified scale and distance unit.
* @param scale map scale
* @param dpi screen DPI
* @param mapUnits map units
* @returns matching QgsMapToPixel
* @note added in QGIS 3.0
*/
static QgsMapToPixel fromScale( double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi = 96 );

/**
* Constructor
*
Expand Down
14 changes: 14 additions & 0 deletions tests/src/core/testqgsmaptopixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#include <qgsmaptopixel.h>
#include <qgspoint.h>
#include "qgslogger.h"
#include "qgstestutils.h"

class TestQgsMapToPixel: public QObject
{
Q_OBJECT
private slots:
void rotation();
void getters();
void fromScale();
};

void TestQgsMapToPixel::rotation()
Expand Down Expand Up @@ -92,6 +94,18 @@ void TestQgsMapToPixel::getters()
QCOMPARE( m2p.mapUnitsPerPixel(), 2.0 );
}

void TestQgsMapToPixel::fromScale()
{
QgsMapToPixel m2p = QgsMapToPixel::fromScale( 0.001, QgsUnitTypes::DistanceMeters, 96.0 );
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.264583, 0.000001 );
m2p = QgsMapToPixel::fromScale( 0.0001, QgsUnitTypes::DistanceMeters, 96.0 );
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 2.645833, 0.000001 );
m2p = QgsMapToPixel::fromScale( 0.001, QgsUnitTypes::DistanceMeters, 72.0 );
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.352778, 0.000001 );
m2p = QgsMapToPixel::fromScale( 0.001, QgsUnitTypes::DistanceKilometers, 96.0 );
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.000265, 0.000001 );
}

QTEST_MAIN( TestQgsMapToPixel )
#include "testqgsmaptopixel.moc"

Expand Down

0 comments on commit 230417c

Please sign in to comment.