20 #include <QTextStream>
34 : mMapUnitsPerPixel( mapUnitsPerPixel )
37 , mRotation( rotation )
40 , mXMin( xc - ( mWidth * mMapUnitsPerPixel / 2.0 ) )
41 , mYMin( yc - ( mHeight * mMapUnitsPerPixel / 2.0 ) )
48 : mMapUnitsPerPixel( mapUnitsPerPixel )
59 double metersPerPixel = 25.4 / dpi / 1000.0;
69 bool QgsMapToPixel::updateMatrix()
74 if ( !newMatrix.isInvertible() )
83 double oldUnits = mMapUnitsPerPixel;
85 if ( !updateMatrix() )
87 mMapUnitsPerPixel = oldUnits;
93 double oldRotation = mRotation;
94 double oldXCenter = mXCenter;
95 double oldYCenter = mYCenter;
96 double oldWidth = mWidth;
104 mWidth = ( ( mXCenter - mXMin ) * 2 ) / mMapUnitsPerPixel;
107 if ( !updateMatrix() )
109 mRotation = oldRotation;
110 mXCenter = oldXCenter;
111 mYCenter = oldYCenter;
124 double oldMUPP = mMapUnitsPerPixel;
125 double oldXCenter = mXCenter;
126 double oldYCenter = mYCenter;
127 double oldWidth = mWidth;
128 double oldHeight = mHeight;
129 double oldRotation = mRotation;
130 double oldXMin = mXMin;
131 double oldYMin = mYMin;
138 mRotation = rotation;
139 mXMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
140 mYMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
142 if ( !updateMatrix() )
144 mMapUnitsPerPixel = oldMUPP;
145 mXCenter = oldXCenter;
146 mYCenter = oldYCenter;
149 mRotation = oldRotation;
174 QTextStream( &rep ) <<
"Map units/pixel: " << mMapUnitsPerPixel
175 <<
" center: " << mXCenter <<
',' << mYCenter
176 <<
" rotation: " << mRotation
177 <<
" size: " << mWidth <<
'x' << mHeight;
192 return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
193 .translate( -mXMin, - ( mYMin + mHeight * mMapUnitsPerPixel ) );
199 return QTransform::fromTranslate( cx, cy )
201 .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
202 .translate( -mXCenter, -mYCenter );
Perform transforms between map coordinates and device coordinates.
int mapHeight() const
Returns current map height in pixels.
void setMapUnitsPerPixel(double mapUnitsPerPixel)
Set map units per pixel.
void setMapRotation(double degrees, double cx, double cy)
Set map rotation in degrees (clockwise)
QgsMapToPixel()
Constructor.
double mapUnitsPerPixel() const
Returns current map units per pixel.
QTransform transform() const
Returns a QTransform encapsulating the map to pixel conversion.
int mapWidth() const
Returns current map width in pixels The information is only known if setRotation was used.
double mapRotation() const
Returns current map rotation in degrees (clockwise)
static QgsMapToPixel fromScale(double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi=96)
Returns a new QgsMapToPixel created using a specified scale and distance unit.
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Set parameters for use in transforming coordinates.
QString showParameters() const
String representation of the parameters used in the transform.
DistanceUnit
Units of distance.
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)