QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Searching...
No Matches
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
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 T *xData = x.data();
166 T *yData = y.data();
167 const auto size = x.size();
168 for ( int i = 0; i < size; ++i )
169 transformInPlace( *xData++, *yData++ );
170 }
171#endif
172
176 QgsPointXY toMapCoordinates( int x, int y ) const
177 {
179 }
180
184 QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF )
185 {
186 bool invertible;
187 const QTransform matrix = mMatrix.inverted( &invertible );
188 assert( invertible );
189 qreal mx, my;
190 matrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
191 return QgsPointXY( mx, my );
192 }
193
200 QgsPointXY toMapCoordinates( QPoint p ) const
201 {
202 const QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
203 return QgsPointXY( mapPt );
204 }
205
211 Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const SIP_DEPRECATED
212 {
214 }
215
225 void setMapUnitsPerPixel( double mapUnitsPerPixel );
226
232 double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
233
242 int mapWidth() const { return mWidth; }
243
250 int mapHeight() const { return mHeight; }
251
264 void setMapRotation( double degrees, double cx, double cy );
265
272 double mapRotation() const { return mRotation; }
273
289 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
290
306 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;
307
311 QString showParameters() const;
312
316 QTransform transform() const;
317
323 double xCenter() const { return mXCenter; }
324
330 double yCenter() const { return mYCenter; }
331
332 bool operator==( const QgsMapToPixel &other ) const
333 {
334 return mValid == other.mValid
335 && mMapUnitsPerPixel == other.mMapUnitsPerPixel
336 && mWidth == other.mWidth
337 && mHeight == other.mHeight
338 && mRotation == other.mRotation
339 && mXCenter == other.mXCenter
340 && mYCenter == other.mYCenter
341 && mXMin == other.mXMin
342 && mYMin == other.mYMin;
343 }
344
345 bool operator!=( const QgsMapToPixel &other ) const
346 {
347 return !( *this == other );
348 }
349
350 private:
351 bool mValid = false;
352 double mMapUnitsPerPixel = 1;
353 int mWidth = 1;
354 int mHeight = 1;
355 double mRotation = 0.0;
356 double mXCenter = 0.5;
357 double mYCenter = 0.5;
358 double mXMin = 0;
359 double mYMin = 0;
360 QTransform mMatrix;
361
362 bool updateMatrix();
363};
364
365
366#endif // QGSMAPTOPIXEL
DistanceUnit
Units of distance.
Definition qgis.h:3496
Perform transforms between map coordinates and device coordinates.
bool isValid() const
Returns true if the object is valid (i.e.
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.
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)
Sets the x and y value of the point.
Definition qgspointxy.h:139
double y
Definition qgspointxy.h:63
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