Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method to create QgsMapToPixel from scale/dpi/mapunits
  • 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
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
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
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
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.