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 );
110 return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
120 double oldUnits = mMapUnitsPerPixel;
122 if ( !updateMatrix() )
124 mMapUnitsPerPixel = oldUnits;
130 return mMapUnitsPerPixel;
135 double oldRotation = mRotation;
136 double oldXCenter = mXCenter;
137 double oldYCenter = mYCenter;
138 double oldWidth = mWidth;
146 mWidth = ( ( mXCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
149 if ( !updateMatrix() )
151 mRotation = oldRotation;
152 mXCenter = oldXCenter;
153 mYCenter = oldYCenter;
170 double oldMUPP = mMapUnitsPerPixel;
171 double oldXCenter = mXCenter;
172 double oldYCenter = mYCenter;
173 double oldWidth = mWidth;
174 double oldHeight = mHeight;
175 double oldRotation = mRotation;
176 double oldXMin = xMin;
177 double oldYMin = yMin;
184 mRotation = rotation;
185 xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
186 yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
188 if ( !updateMatrix() )
190 mMapUnitsPerPixel = oldMUPP;
191 mXCenter = oldXCenter;
192 mYCenter = oldYCenter;
195 mRotation = oldRotation;
204 QTextStream( &rep ) <<
"Map units/pixel: " << mMapUnitsPerPixel
205 <<
" center: " << mXCenter <<
',' << mYCenter
206 <<
" rotation: " << mRotation
207 <<
" size: " << mWidth <<
'x' << mHeight;
219 qreal x = p.
x(), y = p.
y();
227 qreal x = p->
x(), y = p->
y();
237 qreal x_qreal = x, y_qreal = y;
238 mMatrix.map( x_qreal, y_qreal, &mx, &my );
246 double mx = x, my = y;
263 return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
264 .translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
270 return QTransform::fromTranslate( cx, cy )
272 .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
273 .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 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
double mapRotation() const
Returns 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
Returns current map width in pixels The information is only known if setRotation was used...
int mapHeight() const
Returns 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
Returns 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.
Q_DECL_DEPRECATED QgsPointXY toMapPoint(double x, double y) const
Transform device coordinates to map (world) coordinates.
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 toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.