QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
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 *
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 "qgis.h"
24#include "qgspointxy.h"
25
26#include <cassert>
27
28class QPoint;
29
36class CORE_EXPORT QgsMapToPixel
37{
38 public:
39
46
56 QgsMapToPixel( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
57
62 QgsMapToPixel( double mapUnitsPerPixel );
63
71 static QgsMapToPixel fromScale( double scale, Qgis::DistanceUnit mapUnits, double dpi = 96 );
72
79 bool isValid() const { return mValid; }
80
87 {
88 qreal x = p.x();
89 qreal y = p.y();
90 transformInPlace( x, y );
91 return QgsPointXY( x, y );
92 }
93
97 void transform( QgsPointXY *p ) const
98 {
99 qreal x = p->x();
100 qreal y = p->y();
101 transformInPlace( x, y );
102 p->set( x, y );
103 }
104
112 QgsPointXY transform( qreal x, qreal y ) const
113 {
114 transformInPlace( x, y );
115 return QgsPointXY( x, y );
116 }
117
124 void transformInPlace( double &x, double &y ) const
125 {
126 qreal mx, my;
127 mMatrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
128 x = mx;
129 y = my;
130 }
131
140 QRectF transformBounds( const QRectF &bounds ) const
141 {
142 QPointF topLeft = bounds.topLeft();
143 QPointF topRight = bounds.topRight();
144 QPointF bottomLeft = bounds.bottomLeft();
145 QPointF bottomRight = bounds.bottomRight();
146
147 transformInPlace( topLeft.rx(), topLeft.ry() );
148 transformInPlace( topRight.rx(), topRight.ry() );
149 transformInPlace( bottomLeft.rx(), bottomLeft.ry() );
150 transformInPlace( bottomRight.rx(), bottomRight.ry() );
151
152 auto minMaxX = std::minmax( { topLeft.x(), topRight.x(), bottomLeft.x(), bottomRight.x() } );
153 auto minMaxY = std::minmax( { topLeft.y(), topRight.y(), bottomLeft.y(), bottomRight.y() } );
154
155 const double left = minMaxX.first;
156 const double right = minMaxX.second;
157 const double top = minMaxY.first;
158 const double bottom = minMaxY.second;
159 return QRectF( left, top, right - left, bottom - top );
160 }
161
170 void transformInPlace( float &x, float &y ) const SIP_SKIP
171 {
172 double mx = x, my = y;
173 transformInPlace( mx, my );
174 x = mx;
175 y = my;
176 }
177
178#ifndef SIP_RUN
179
187 template <class T> SIP_SKIP
188 void transformInPlace( QVector<T> &x, QVector<T> &y ) const
189 {
190 assert( x.size() == y.size() );
191 T *xData = x.data();
192 T *yData = y.data();
193 const auto size = x.size();
194 for ( int i = 0; i < size; ++i )
195 transformInPlace( *xData++, *yData++ );
196 }
197#endif
198
202 QgsPointXY toMapCoordinates( int x, int y ) const
203 {
204 return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
205 }
206
210 QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF )
211 {
212 bool invertible;
213 const QTransform matrix = mMatrix.inverted( &invertible );
214 assert( invertible );
215 qreal mx, my;
216 matrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
217 return QgsPointXY( mx, my );
218 }
219
226 QgsPointXY toMapCoordinates( QPoint p ) const
227 {
228 const QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
229 return QgsPointXY( mapPt );
230 }
231
237 Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const SIP_DEPRECATED
238 {
239 return toMapCoordinates( x, y );
240 }
241
251 void setMapUnitsPerPixel( double mapUnitsPerPixel );
252
258 double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
259
267 int mapWidth() const { return mWidth; }
268
274 int mapHeight() const { return mHeight; }
275
287 void setMapRotation( double degrees, double cx, double cy );
288
294 double mapRotation() const { return mRotation; }
295
310 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
311
327 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;
328
332 QString showParameters() const;
333
337 QTransform transform() const;
338
343 double xCenter() const { return mXCenter; }
344
349 double yCenter() const { return mYCenter; }
350
351 bool operator==( const QgsMapToPixel &other ) const
352 {
353 return mValid == other.mValid
354 && mMapUnitsPerPixel == other.mMapUnitsPerPixel
355 && mWidth == other.mWidth
356 && mHeight == other.mHeight
357 && mRotation == other.mRotation
358 && mXCenter == other.mXCenter
359 && mYCenter == other.mYCenter
360 && mXMin == other.mXMin
361 && mYMin == other.mYMin;
362 }
363
364 bool operator!=( const QgsMapToPixel &other ) const
365 {
366 return !( *this == other );
367 }
368
369 private:
370 bool mValid = false;
371 double mMapUnitsPerPixel = 1;
372 int mWidth = 1;
373 int mHeight = 1;
374 double mRotation = 0.0;
375 double mXCenter = 0.5;
376 double mYCenter = 0.5;
377 double mXMin = 0;
378 double mYMin = 0;
379 QTransform mMatrix;
380
381 bool updateMatrix();
382};
383
384
385#endif // QGSMAPTOPIXEL
DistanceUnit
Units of distance.
Definition qgis.h:4677
Perform transforms between map coordinates and device coordinates.
QRectF transformBounds(const QRectF &bounds) const
Transforms a bounding box from map coordinates to 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 map coordinates to device 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 map coordinates to device coordinates.
void transformInPlace(float &x, float &y) const
Transforms map coordinates to device 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:136
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
#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