QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsmaptopixel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptopixel.h - description
3  -------------------
4  begin : Sat Jun 22 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSMAPTOPIXEL
18 #define QGSMAPTOPIXEL
19 
20 #include "qgis_core.h"
21 #include "qgis_sip.h"
22 #include <QTransform>
23 #include <vector>
24 #include "qgsunittypes.h"
25 #include "qgspointxy.h"
26 
27 #include <cassert>
28 #include <memory>
29 
30 class QPoint;
31 
38 class CORE_EXPORT QgsMapToPixel
39 {
40  public:
41 
52  QgsMapToPixel( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
53 
58  QgsMapToPixel( double mapUnitsPerPixel );
59 
68  static QgsMapToPixel fromScale( double scale, QgsUnitTypes::DistanceUnit mapUnits, double dpi = 96 );
69 
75  QgsMapToPixel();
76 
82  QgsPointXY transform( const QgsPointXY &p ) const
83  {
84  qreal x = p.x();
85  qreal y = p.y();
86  transformInPlace( x, y );
87  return QgsPointXY( x, y );
88  }
89 
93  void transform( QgsPointXY *p ) const
94  {
95  qreal x = p->x();
96  qreal y = p->y();
97  transformInPlace( x, y );
98  p->set( x, y );
99  }
100 
108  QgsPointXY transform( qreal x, qreal y ) const
109  {
110  transformInPlace( x, y );
111  return QgsPointXY( x, y );
112  }
113 
119  void transformInPlace( double &x, double &y ) const
120  {
121  qreal mx, my;
122  mMatrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
123  x = mx;
124  y = my;
125  }
126 
133  void transformInPlace( float &x, float &y ) const SIP_SKIP
134  {
135  double mx = x, my = y;
136  transformInPlace( mx, my );
137  x = mx;
138  y = my;
139  }
140 
141 #ifndef SIP_RUN
142 
149  template <class T> SIP_SKIP
150  void transformInPlace( QVector<T> &x, QVector<T> &y ) const
151  {
152  assert( x.size() == y.size() );
153  for ( int i = 0; i < x.size(); ++i )
154  transformInPlace( x[i], y[i] );
155  }
156 #endif
157 
159  QgsPointXY toMapCoordinates( int x, int y ) const
160  {
161  return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
162  }
163 
165  QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF )
166  {
167  bool invertible;
168  QTransform matrix = mMatrix.inverted( &invertible );
169  assert( invertible );
170  qreal mx, my;
171  matrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
172  return QgsPointXY( mx, my );
173  }
174 
180  QgsPointXY toMapCoordinates( QPoint p ) const
181  {
182  QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
183  return QgsPointXY( mapPt );
184  }
185 
190  Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const SIP_DEPRECATED
191  {
192  return toMapCoordinates( x, y );
193  }
194 
199  void setMapUnitsPerPixel( double mapUnitsPerPixel );
200 
202  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
203 
209  int mapWidth() const { return mWidth; }
210 
215  int mapHeight() const { return mHeight; }
216 
224  void setMapRotation( double degrees, double cx, double cy );
225 
230  double mapRotation() const { return mRotation; }
231 
242  void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
243 
245  QString showParameters() const;
246 
250  QTransform transform() const;
251 
257  double xCenter() const { return mXCenter; }
258 
264  double yCenter() const { return mYCenter; }
265 
266  bool operator==( const QgsMapToPixel &other ) const
267  {
268  return mMapUnitsPerPixel == other.mMapUnitsPerPixel
269  && mWidth == other.mWidth
270  && mHeight == other.mHeight
271  && mRotation == other.mRotation
272  && mXCenter == other.mXCenter
273  && mYCenter == other.mYCenter
274  && mXMin == other.mXMin
275  && mYMin == other.mYMin;
276  }
277 
278  bool operator!=( const QgsMapToPixel &other ) const
279  {
280  return !( *this == other );
281  }
282 
283  private:
284  double mMapUnitsPerPixel = 1;
285  int mWidth = 1;
286  int mHeight = 1;
287  double mRotation = 0.0;
288  double mXCenter = 0.5;
289  double mYCenter = 0.5;
290  double mXMin = 0;
291  double mYMin = 0;
292  QTransform mMatrix;
293 
294  bool updateMatrix();
295 };
296 
297 
298 #endif // QGSMAPTOPIXEL
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
int mapHeight() const
Returns current map height in pixels.
double xCenter() const
Returns the center x-coordinate for the transform.
double yCenter() const
Returns the center y-coordinate for the transform.
QgsPointXY transform(qreal x, qreal y) const
Transform the point specified by x,y from map (world) coordinates to device coordinates.
QgsPointXY toMapCoordinates(double x, double y) const
Transform device coordinates to map (world) coordinates.
double mapUnitsPerPixel() const
Returns current map units per pixel.
bool operator==(const QgsMapToPixel &other) const
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
int mapWidth() const
Returns current map width in pixels The information is only known if setRotation was used.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point p from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.h:82
void transform(QgsPointXY *p) const
Transform the point p from map (world) coordinates to device coordinates in place.
Definition: qgsmaptopixel.h:93
void transformInPlace(QVector< T > &x, QVector< T > &y) const
Transform device coordinates to map coordinates.
double mapRotation() const
Returns current map rotation in degrees (clockwise)
bool operator!=(const QgsMapToPixel &other) const
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
void transformInPlace(float &x, float &y) const
Transforms device coordinates to map coordinates.
QgsPointXY toMapCoordinates(QPoint p) const
Transform device coordinates to map (world) coordinates.
Q_DECL_DEPRECATED QgsPointXY toMapPoint(double x, double y) const
Transform device coordinates to map (world) coordinates.
A class to represent a 2D point.
Definition: qgspointxy.h:44
void set(double x, double y) SIP_HOLDGIL
Sets the x and y value of the point.
Definition: qgspointxy.h:124
double y
Definition: qgspointxy.h:48
Q_GADGET double x
Definition: qgspointxy.h:47
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81