QGIS API Documentation  3.19.0-Master (e8efe81642)
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 
244  void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
245 
258  void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;
259 
261  QString showParameters() const;
262 
266  QTransform transform() const;
267 
273  double xCenter() const { return mXCenter; }
274 
280  double yCenter() const { return mYCenter; }
281 
282  bool operator==( const QgsMapToPixel &other ) const
283  {
284  return mMapUnitsPerPixel == other.mMapUnitsPerPixel
285  && mWidth == other.mWidth
286  && mHeight == other.mHeight
287  && mRotation == other.mRotation
288  && mXCenter == other.mXCenter
289  && mYCenter == other.mYCenter
290  && mXMin == other.mXMin
291  && mYMin == other.mYMin;
292  }
293 
294  bool operator!=( const QgsMapToPixel &other ) const
295  {
296  return !( *this == other );
297  }
298 
299  private:
300  double mMapUnitsPerPixel = 1;
301  int mWidth = 1;
302  int mHeight = 1;
303  double mRotation = 0.0;
304  double mXCenter = 0.5;
305  double mYCenter = 0.5;
306  double mXMin = 0;
307  double mYMin = 0;
308  QTransform mMatrix;
309 
310  bool updateMatrix();
311 };
312 
313 
314 #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:59
void set(double x, double y) SIP_HOLDGIL
Sets the x and y value of the point.
Definition: qgspointxy.h:139
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
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