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;
123 double oldMUPP = mMapUnitsPerPixel;
124 double oldXCenter = mXCenter;
125 double oldYCenter = mYCenter;
126 double oldWidth = mWidth;
127 double oldHeight = mHeight;
128 double oldRotation = mRotation;
129 double oldXMin = mXMin;
130 double oldYMin = mYMin;
137 mRotation = rotation;
138 mXMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
139 mYMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
141 if ( !updateMatrix() )
143 mMapUnitsPerPixel = oldMUPP;
144 mXCenter = oldXCenter;
145 mYCenter = oldYCenter;
148 mRotation = oldRotation;
157 QTextStream( &rep ) <<
"Map units/pixel: " << mMapUnitsPerPixel
158 <<
" center: " << mXCenter <<
',' << mYCenter
159 <<
" rotation: " << mRotation
160 <<
" size: " << mWidth <<
'x' << mHeight;
175 return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
176 .translate( -mXMin, - ( mYMin + mHeight * mMapUnitsPerPixel ) );
182 return QTransform::fromTranslate( cx, cy )
184 .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
185 .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)