Add method to create QgsMapToPixel from scale/dpi/mapunits

This commit is contained in:
Nyall Dawson 2016-10-02 22:10:01 +11:00
parent 4b7876c1f2
commit 230417c7a8
4 changed files with 41 additions and 1 deletions

View File

@ -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
*

View File

@ -24,6 +24,7 @@
#include "qgslogger.h"
#include "qgspoint.h"
QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
double xc,
double yc,
@ -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 )

View File

@ -19,7 +19,7 @@
#include <QTransform>
#include <vector>
#include "qgsunittypes.h"
#include <cassert>
class QgsPoint;
@ -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
*

View File

@ -20,6 +20,7 @@
#include <qgsmaptopixel.h>
#include <qgspoint.h>
#include "qgslogger.h"
#include "qgstestutils.h"
class TestQgsMapToPixel: public QObject
{
@ -27,6 +28,7 @@ class TestQgsMapToPixel: public QObject
private slots:
void rotation();
void getters();
void fromScale();
};
void TestQgsMapToPixel::rotation()
@ -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"