QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 <cassert>
21
22#include "qgis.h"
23#include "qgis_core.h"
24#include "qgis_sip.h"
25#include "qgspointxy.h"
26
27#include <QTransform>
28
29class QPoint;
30
37class CORE_EXPORT QgsMapToPixel
38{
39 public:
40
47
57 QgsMapToPixel( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
58
64
72 static QgsMapToPixel fromScale( double scale, Qgis::DistanceUnit mapUnits, double dpi = 96 );
73
80 bool isValid() const { return mValid; }
81
88 {
89 qreal x = p.x();
90 qreal y = p.y();
91 transformInPlace( x, y );
92 return QgsPointXY( x, y );
93 }
94
98 void transform( QgsPointXY *p ) const
99 {
100 qreal x = p->x();
101 qreal y = p->y();
102 transformInPlace( x, y );
103 p->set( x, y );
104 }
105
113 QgsPointXY transform( qreal x, qreal y ) const
114 {
115 transformInPlace( x, y );
116 return QgsPointXY( x, y );
117 }
118
125 void transformInPlace( double &x, double &y ) const
126 {
127 qreal mx, my;
128 mMatrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
129 x = mx;
130 y = my;
131 }
132
141 QRectF transformBounds( const QRectF &bounds ) const
142 {
143 QPointF topLeft = bounds.topLeft();
144 QPointF topRight = bounds.topRight();
145 QPointF bottomLeft = bounds.bottomLeft();
146 QPointF bottomRight = bounds.bottomRight();
147
148 transformInPlace( topLeft.rx(), topLeft.ry() );
149 transformInPlace( topRight.rx(), topRight.ry() );
150 transformInPlace( bottomLeft.rx(), bottomLeft.ry() );
151 transformInPlace( bottomRight.rx(), bottomRight.ry() );
152
153 auto minMaxX = std::minmax( { topLeft.x(), topRight.x(), bottomLeft.x(), bottomRight.x() } );
154 auto minMaxY = std::minmax( { topLeft.y(), topRight.y(), bottomLeft.y(), bottomRight.y() } );
155
156 const double left = minMaxX.first;
157 const double right = minMaxX.second;
158 const double top = minMaxY.first;
159 const double bottom = minMaxY.second;
160 return QRectF( left, top, right - left, bottom - top );
161 }
162
171 void transformInPlace( float &x, float &y ) const SIP_SKIP
172 {
173 double mx = x, my = y;
174 transformInPlace( mx, my );
175 x = mx;
176 y = my;
177 }
178
179#ifndef SIP_RUN
180
188 template <class T> SIP_SKIP
189 void transformInPlace( QVector<T> &x, QVector<T> &y ) const
190 {
191 assert( x.size() == y.size() );
192 T *xData = x.data();
193 T *yData = y.data();
194 const auto size = x.size();
195 for ( int i = 0; i < size; ++i )
196 transformInPlace( *xData++, *yData++ );
197 }
198#endif
199
203 QgsPointXY toMapCoordinates( int x, int y ) const
204 {
205 return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
206 }
207
211 QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF )
212 {
213 bool invertible;
214 const QTransform matrix = mMatrix.inverted( &invertible );
215 assert( invertible );
216 qreal mx, my;
217 matrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
218 return QgsPointXY( mx, my );
219 }
220
227 QgsPointXY toMapCoordinates( QPoint p ) const
228 {
229 const QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
230 return QgsPointXY( mapPt );
231 }
232
238 Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const SIP_DEPRECATED
239 {
240 return toMapCoordinates( x, y );
241 }
242
252 void setMapUnitsPerPixel( double mapUnitsPerPixel );
253
259 double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
260
268 int mapWidth() const { return mWidth; }
269
275 int mapHeight() const { return mHeight; }
276
288 void setMapRotation( double degrees, double cx, double cy );
289
295 double mapRotation() const { return mRotation; }
296
311 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
312
328 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;
329
333 QString showParameters() const;
334
338 QTransform transform() const;
339
344 double xCenter() const { return mXCenter; }
345
350 double yCenter() const { return mYCenter; }
351
352 bool operator==( const QgsMapToPixel &other ) const
353 {
354 return mValid == other.mValid
355 && mMapUnitsPerPixel == other.mMapUnitsPerPixel
356 && mWidth == other.mWidth
357 && mHeight == other.mHeight
358 && mRotation == other.mRotation
359 && mXCenter == other.mXCenter
360 && mYCenter == other.mYCenter
361 && mXMin == other.mXMin
362 && mYMin == other.mYMin;
363 }
364
365 bool operator!=( const QgsMapToPixel &other ) const
366 {
367 return !( *this == other );
368 }
369
370 private:
371 bool mValid = false;
372 double mMapUnitsPerPixel = 1;
373 int mWidth = 1;
374 int mHeight = 1;
375 double mRotation = 0.0;
376 double mXCenter = 0.5;
377 double mYCenter = 0.5;
378 double mXMin = 0;
379 double mYMin = 0;
380 QTransform mMatrix;
381
382 bool updateMatrix();
383};
384
385
386#endif // QGSMAPTOPIXEL
DistanceUnit
Units of distance.
Definition qgis.h:5013
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.
QgsMapToPixel()
Constructor for an invalid QgsMapToPixel.
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
static QgsMapToPixel fromScale(double scale, Qgis::DistanceUnit mapUnits, double dpi=96)
Returns a new QgsMapToPixel created using a specified scale and distance unit.
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.
Represents 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:114
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_PYNAME(name)
Definition qgis_sip.h:89