20 #include <QTextStream> 34 : mMapUnitsPerPixel( mapUnitsPerPixel )
37 , mRotation( rotation )
40 , xMin( xc - ( mWidth * mMapUnitsPerPixel / 2.0 ) )
41 , yMin( yc - ( mHeight * mMapUnitsPerPixel / 2.0 ) )
43 Q_ASSERT( mapUnitsPerPixel > 0 );
48 : mMapUnitsPerPixel( mapUnitsPerPixel )
59 double metersPerPixel = 25.4 / dpi / 1000.0;
79 bool QgsMapToPixel::updateMatrix()
84 if ( !newMatrix.isInvertible() )
94 QTransform matrix = mMatrix.inverted( &invertible );
97 qreal x_qreal = x, y_qreal = y;
98 matrix.map( x_qreal, y_qreal, &mx, &my );
121 double oldUnits = mMapUnitsPerPixel;
123 if ( !updateMatrix() )
125 mMapUnitsPerPixel = oldUnits;
131 return mMapUnitsPerPixel;
136 double oldRotation = mRotation;
137 double oldXCenter = mXCenter;
138 double oldYCenter = mYCenter;
139 double oldWidth = mWidth;
147 mWidth = ( ( mXCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
150 if ( !updateMatrix() )
152 mRotation = oldRotation;
153 mXCenter = oldXCenter;
154 mYCenter = oldYCenter;
171 double oldMUPP = mMapUnitsPerPixel;
172 double oldXCenter = mXCenter;
173 double oldYCenter = mYCenter;
174 double oldWidth = mWidth;
175 double oldHeight = mHeight;
176 double oldRotation = mRotation;
177 double oldXMin = xMin;
178 double oldYMin = yMin;
185 mRotation = rotation;
186 xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
187 yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
189 if ( !updateMatrix() )
191 mMapUnitsPerPixel = oldMUPP;
192 mXCenter = oldXCenter;
193 mYCenter = oldYCenter;
196 mRotation = oldRotation;
205 QTextStream( &rep ) <<
"Map units/pixel: " << mMapUnitsPerPixel
206 <<
" center: " << mXCenter <<
',' << mYCenter
207 <<
" rotation: " << mRotation
208 <<
" size: " << mWidth <<
'x' << mHeight;
220 qreal x = p.
x(), y = p.
y();
228 qreal x = p->
x(), y = p->
y();
238 qreal x_qreal = x, y_qreal = y;
239 mMatrix.map( x_qreal, y_qreal, &mx, &my );
247 double mx = x, my = y;
264 return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
265 .translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
271 return QTransform::fromTranslate( cx, cy )
273 .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
274 .translate( -mXCenter, -mYCenter );
void set(double x, double y)
Sets the x and y value of the point.
void setMapRotation(double degrees, double cx, double cy)
Set map rotation in degrees (clockwise)
A class to represent a 2D point.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
double mapRotation() const
Return current map rotation in degrees.
void transformInPlace(double &x, double &y) const
Transform device coordinates to map coordinates.
Perform transforms between map coordinates and device coordinates.
int mapWidth() const
Return current map width in pixels The information is only known if setRotation was used...
int mapHeight() const
Return current map height in pixels.
static QgsMapToPixel fromScale(double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi=96)
Returns a new QgsMapToPixel created using a specified scale and distance unit.
QgsMapToPixel()
Constructor.
QString showParameters() const
String representation of the parameters used in the transform.
double mapUnitsPerPixel() const
Return current map units per pixel.
DistanceUnit
Units of distance.
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Set parameters for use in transforming coordinates.
QgsPointXY toMapPoint(double x, double y) const
QTransform transform() const
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
void setMapUnitsPerPixel(double mapUnitsPerPixel)
Set map units per pixel.
QgsPointXY toMapCoordinatesF(double x, double y) const
Transform device coordinates to map (world) coordinates.
QgsPointXY toMapCoordinates(int x, int y) const