QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
46
56 QgsMapToPixel( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
57
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 void transformInPlace( QVector<T> &x, QVector<T> &y ) const
188 {
189 assert( x.size() == y.size() );
190 T *xData = x.data();
191 T *yData = y.data();
192 const auto size = x.size();
193 for ( int i = 0; i < size; ++i )
194 transformInPlace( *xData++, *yData++ );
195 }
196#endif
197
201 QgsPointXY toMapCoordinates( int x, int y ) const { return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) ); }
202
206 QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF )
207 {
208 bool invertible;
209 const QTransform matrix = mMatrix.inverted( &invertible );
210 assert( invertible );
211 qreal mx, my;
212 matrix.map( static_cast< qreal >( x ), static_cast< qreal >( y ), &mx, &my );
213 return QgsPointXY( mx, my );
214 }
215
222 QgsPointXY toMapCoordinates( QPoint p ) const
223 {
224 const QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
225 return QgsPointXY( mapPt );
226 }
227
233 Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const SIP_DEPRECATED { return toMapCoordinates( x, y ); }
234
244 void setMapUnitsPerPixel( double mapUnitsPerPixel );
245
251 double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
252
260 int mapWidth() const { return mWidth; }
261
267 int mapHeight() const { return mHeight; }
268
280 void setMapRotation( double degrees, double cx, double cy );
281
287 double mapRotation() const { return mRotation; }
288
303 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );
304
320 void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;
321
325 QString showParameters() const;
326
330 QTransform transform() const;
331
336 double xCenter() const { return mXCenter; }
337
342 double yCenter() const { return mYCenter; }
343
344 bool operator==( const QgsMapToPixel &other ) const
345 {
346 return mValid == other.mValid
347 && mMapUnitsPerPixel == other.mMapUnitsPerPixel
348 && mWidth == other.mWidth
349 && mHeight == other.mHeight
350 && mRotation == other.mRotation
351 && mXCenter == other.mXCenter
352 && mYCenter == other.mYCenter
353 && mXMin == other.mXMin
354 && mYMin == other.mYMin;
355 }
356
357 bool operator!=( const QgsMapToPixel &other ) const { return !( *this == other ); }
358
359 private:
360 bool mValid = false;
361 double mMapUnitsPerPixel = 1;
362 int mWidth = 1;
363 int mHeight = 1;
364 double mRotation = 0.0;
365 double mXCenter = 0.5;
366 double mYCenter = 0.5;
367 double mXMin = 0;
368 double mYMin = 0;
369 QTransform mMatrix;
370
371 bool updateMatrix();
372};
373
374
375#endif // QGSMAPTOPIXEL
DistanceUnit
Units of distance.
Definition qgis.h:5170
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:62
void set(double x, double y)
Sets the x and y value of the point.
Definition qgspointxy.h:139
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYNAME(name)
Definition qgis_sip.h:88