20 #include <QTextStream>
35 , mMapUnitsPerPixel( mapUnitsPerPixel )
38 , mRotation( rotation )
41 , mXMin( xc - ( mWidth * mMapUnitsPerPixel / 2.0 ) )
42 , mYMin( yc - ( mHeight * mMapUnitsPerPixel / 2.0 ) )
50 , mMapUnitsPerPixel( mapUnitsPerPixel )
61 const double metersPerPixel = 25.4 / dpi / 1000.0;
71 bool QgsMapToPixel::updateMatrix()
76 if ( !newMatrix.isInvertible() )
87 const double oldUnits = mMapUnitsPerPixel;
89 if ( !updateMatrix() )
91 mMapUnitsPerPixel = oldUnits;
99 const double oldRotation = mRotation;
100 const double oldXCenter = mXCenter;
101 const double oldYCenter = mYCenter;
102 const double oldWidth = mWidth;
110 mWidth = ( ( mXCenter - mXMin ) * 2 ) / mMapUnitsPerPixel;
113 if ( !updateMatrix() )
115 mRotation = oldRotation;
116 mXCenter = oldXCenter;
117 mYCenter = oldYCenter;
132 const double oldMUPP = mMapUnitsPerPixel;
133 const double oldXCenter = mXCenter;
134 const double oldYCenter = mYCenter;
135 const double oldWidth = mWidth;
136 const double oldHeight = mHeight;
137 const double oldRotation = mRotation;
138 const double oldXMin = mXMin;
139 const double oldYMin = mYMin;
146 mRotation = rotation;
147 mXMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
148 mYMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
150 if ( !updateMatrix() )
152 mMapUnitsPerPixel = oldMUPP;
153 mXCenter = oldXCenter;
154 mYCenter = oldYCenter;
157 mRotation = oldRotation;
183 QTextStream( &rep ) <<
"Map units/pixel: " << mMapUnitsPerPixel
184 <<
" center: " << mXCenter <<
',' << mYCenter
185 <<
" rotation: " << mRotation
186 <<
" size: " << mWidth <<
'x' << mHeight;
201 return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
202 .translate( -mXMin, - ( mYMin + mHeight * mMapUnitsPerPixel ) );
208 return QTransform::fromTranslate( cx, cy )
210 .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
211 .translate( -mXCenter, -mYCenter );
Perform transforms between map coordinates and device coordinates.
int mapHeight() const
Returns current map height in pixels.
void setMapUnitsPerPixel(double mapUnitsPerPixel)
Sets the map units per pixel.
void setMapRotation(double degrees, double cx, double cy)
Sets map rotation in degrees (clockwise).
QgsMapToPixel()
Constructor for an invalid QgsMapToPixel.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
QTransform transform() const
Returns a QTransform encapsulating the map to pixel conversion.
int mapWidth() const
Returns the current map width in pixels.
double mapRotation() const
Returns the 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)
Sets parameters for use in transforming coordinates.
QString showParameters() const
Returns a 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)