QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 "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
59 QgsMapToPixel( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
60
65 QgsMapToPixel( double mapUnitsPerPixel );
66
75 static QgsMapToPixel fromScale( double scale, Qgis::DistanceUnit mapUnits, double dpi = 96 );
76
83 bool isValid() const { return mValid; }
84
91 {
92 qreal x = p.x();
93 qreal y = p.y();
94 transformInPlace( x, y );
95 return QgsPointXY( x, y );
96 }
97
101 void transform( QgsPointXY *p ) const
102 {
103 qreal x = p->x();
104 qreal y = p->y();
105 transformInPlace( x, y );
106 p->set( x, y );
107 }
108
116 QgsPointXY transform( qreal x, qreal y ) const
117 {
118 transformInPlace( x, y );
119 return QgsPointXY( x, y );
120 }
121
128 void transformInPlace( double &x, double &y ) const
129 {
130 qreal mx, my;
131 mMatrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
132 x = mx;
133 y = my;
134 }
135
144 void transformInPlace( float &x, float &y ) const SIP_SKIP
145 {
146 double mx = x, my = y;
147 transformInPlace( mx, my );
148 x = mx;
149 y = my;
150 }
151
152#ifndef SIP_RUN
153
161 template <class T> SIP_SKIP
162 void transformInPlace( QVector<T> &x, QVector<T> &y ) const
163 {
164 assert( x.size() == y.size() );
165 for ( int i = 0; i < x.size(); ++i )
166 transformInPlace( x[i], y[i] );
167 }
168#endif
169
173 QgsPointXY toMapCoordinates( int x, int y ) const
174 {
175 return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
176 }
177
181 QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF )
182 {
183 bool invertible;
184 const QTransform matrix = mMatrix.inverted( &invertible );
185 assert( invertible );
186 qreal mx, my;
187 matrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
188 return QgsPointXY( mx, my );
189 }
190
197 QgsPointXY toMapCoordinates( QPoint p ) const
198 {
199 const QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
200 return QgsPointXY( mapPt );
201 }
202
208 Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const SIP_DEPRECATED
209 {
210 return toMapCoordinates( x, y );
211 }
212
222 void setMapUnitsPerPixel( double mapUnitsPerPixel );
223
229 double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
230
239 int mapWidth() const { return mWidth; }
240
247 int mapHeight() const { return mHeight; }
248
261 void setMapRotation( double degrees, double cx, double cy );
262
269 double mapRotation() const { return mRotation; }
270
286 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
287
303 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;
304
308 QString showParameters() const;
309
313 QTransform transform() const;
314
320 double xCenter() const { return mXCenter; }
321
327 double yCenter() const { return mYCenter; }
328
329 bool operator==( const QgsMapToPixel &other ) const
330 {
331 return mValid == other.mValid
332 && mMapUnitsPerPixel == other.mMapUnitsPerPixel
333 && mWidth == other.mWidth
334 && mHeight == other.mHeight
335 && mRotation == other.mRotation
336 && mXCenter == other.mXCenter
337 && mYCenter == other.mYCenter
338 && mXMin == other.mXMin
339 && mYMin == other.mYMin;
340 }
341
342 bool operator!=( const QgsMapToPixel &other ) const
343 {
344 return !( *this == other );
345 }
346
347 private:
348 bool mValid = false;
349 double mMapUnitsPerPixel = 1;
350 int mWidth = 1;
351 int mHeight = 1;
352 double mRotation = 0.0;
353 double mXCenter = 0.5;
354 double mYCenter = 0.5;
355 double mXMin = 0;
356 double mYMin = 0;
357 QTransform mMatrix;
358
359 bool updateMatrix();
360};
361
362
363#endif // QGSMAPTOPIXEL
DistanceUnit
Units of distance.
Definition: qgis.h:3047
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:83
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:90
void transform(QgsPointXY *p) const
Transforms a point p from map (world) coordinates to device coordinates in place.
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: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
#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