QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 *
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 "qgis.h"
25#include "qgspointxy.h"
26
27#include <cassert>
28#include <memory>
29
30class QPoint;
31
38class CORE_EXPORT QgsMapToPixel
39{
40 public:
41
48
58 QgsMapToPixel( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
59
64 QgsMapToPixel( double mapUnitsPerPixel );
65
73 static QgsMapToPixel fromScale( double scale, Qgis::DistanceUnit mapUnits, double dpi = 96 );
74
81 bool isValid() const { return mValid; }
82
89 {
90 qreal x = p.x();
91 qreal y = p.y();
92 transformInPlace( x, y );
93 return QgsPointXY( x, y );
94 }
95
99 void transform( QgsPointXY *p ) const
100 {
101 qreal x = p->x();
102 qreal y = p->y();
103 transformInPlace( x, y );
104 p->set( x, y );
105 }
106
114 QgsPointXY transform( qreal x, qreal y ) const
115 {
116 transformInPlace( x, y );
117 return QgsPointXY( x, y );
118 }
119
126 void transformInPlace( double &x, double &y ) const
127 {
128 qreal mx, my;
129 mMatrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
130 x = mx;
131 y = my;
132 }
133
142 void transformInPlace( float &x, float &y ) const SIP_SKIP
143 {
144 double mx = x, my = y;
145 transformInPlace( mx, my );
146 x = mx;
147 y = my;
148 }
149
150#ifndef SIP_RUN
151
159 template <class T> SIP_SKIP
160 void transformInPlace( QVector<T> &x, QVector<T> &y ) const
161 {
162 assert( x.size() == y.size() );
163 T *xData = x.data();
164 T *yData = y.data();
165 const auto size = x.size();
166 for ( int i = 0; i < size; ++i )
167 transformInPlace( *xData++, *yData++ );
168 }
169#endif
170
174 QgsPointXY toMapCoordinates( int x, int y ) const
175 {
177 }
178
182 QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF )
183 {
184 bool invertible;
185 const QTransform matrix = mMatrix.inverted( &invertible );
186 assert( invertible );
187 qreal mx, my;
188 matrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
189 return QgsPointXY( mx, my );
190 }
191
198 QgsPointXY toMapCoordinates( QPoint p ) const
199 {
200 const QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
201 return QgsPointXY( mapPt );
202 }
203
209 Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const SIP_DEPRECATED
210 {
212 }
213
223 void setMapUnitsPerPixel( double mapUnitsPerPixel );
224
230 double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
231
239 int mapWidth() const { return mWidth; }
240
246 int mapHeight() const { return mHeight; }
247
259 void setMapRotation( double degrees, double cx, double cy );
260
266 double mapRotation() const { return mRotation; }
267
282 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
283
299 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;
300
304 QString showParameters() const;
305
309 QTransform transform() const;
310
315 double xCenter() const { return mXCenter; }
316
321 double yCenter() const { return mYCenter; }
322
323 bool operator==( const QgsMapToPixel &other ) const
324 {
325 return mValid == other.mValid
326 && mMapUnitsPerPixel == other.mMapUnitsPerPixel
327 && mWidth == other.mWidth
328 && mHeight == other.mHeight
329 && mRotation == other.mRotation
330 && mXCenter == other.mXCenter
331 && mYCenter == other.mYCenter
332 && mXMin == other.mXMin
333 && mYMin == other.mYMin;
334 }
335
336 bool operator!=( const QgsMapToPixel &other ) const
337 {
338 return !( *this == other );
339 }
340
341 private:
342 bool mValid = false;
343 double mMapUnitsPerPixel = 1;
344 int mWidth = 1;
345 int mHeight = 1;
346 double mRotation = 0.0;
347 double mXCenter = 0.5;
348 double mYCenter = 0.5;
349 double mXMin = 0;
350 double mYMin = 0;
351 QTransform mMatrix;
352
353 bool updateMatrix();
354};
355
356
357#endif // QGSMAPTOPIXEL
DistanceUnit
Units of distance.
Definition: qgis.h:4124
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
bool isValid() const
Returns true if the object is valid (i.e.
Definition: qgsmaptopixel.h:81
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
Transforms the point specified by x,y from map (world) coordinates to device coordinates.
QgsPointXY toMapCoordinates(double x, double y) const
Transforms device coordinates to map (world) coordinates.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
bool operator==(const QgsMapToPixel &other) const
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
int mapWidth() const
Returns the current map width in pixels.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.h:88
void transform(QgsPointXY *p) const
Transforms a point p from map (world) coordinates to device coordinates in place.
Definition: qgsmaptopixel.h:99
void transformInPlace(QVector< T > &x, QVector< T > &y) const
Transforms device coordinates to map coordinates.
double mapRotation() const
Returns the 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
Transforms device coordinates to map (world) coordinates.
Q_DECL_DEPRECATED QgsPointXY toMapPoint(double x, double y) const
Transforms device coordinates to map (world) coordinates.
A class to represent a 2D point.
Definition: qgspointxy.h:60
void set(double x, double y)
Sets the x and y value of the point.
Definition: qgspointxy.h:137
double y
Definition: qgspointxy.h:64