QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsmaptopixel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptopixel.cpp - 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 #include "qgsmaptopixel.h"
18 
19 #include <QPoint>
20 #include <QTextStream>
21 #include <QVector>
22 #include <QTransform>
23 
24 #include "qgslogger.h"
25 
26 // @deprecated in 2.8
27 QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
28  double xc,
29  double yc,
30  int width,
31  int height,
32  double rotation )
33  : mMapUnitsPerPixel( mapUnitsPerPixel )
34  , mWidth( width )
35  , mHeight( height )
36  , mRotation( rotation )
37  , xCenter( xc )
38  , yCenter( yc )
39  , xMin( xc - ( mWidth * mMapUnitsPerPixel / 2.0 ) )
40  , yMin( yc - ( mHeight * mMapUnitsPerPixel / 2.0 ) )
41 {
42  Q_ASSERT( mapUnitsPerPixel > 0 );
43  updateMatrix();
44 }
45 
47  : mMapUnitsPerPixel( 1 )
48  , mWidth( 1 )
49  , mHeight( 1 )
50  , mRotation( 0.0 )
51  , xCenter( 0.5 )
52  , yCenter( 0.5 )
53  , xMin( 0 )
54  , yMin( 0 )
55 {
56  updateMatrix();
57 }
58 
59 QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
60  double height,
61  double ymin,
62  double xmin )
63  : mMapUnitsPerPixel( mapUnitsPerPixel )
64  , mWidth( -1 )
65  , mHeight( height )
66  , mRotation( 0.0 )
67  , xCenter( 0.0 )
68  , yCenter( 0.0 )
69  , xMin( xmin )
70  , yMin( ymin )
71 {
72  updateMatrix();
73 }
74 
76 {
77 }
78 
80 {
81  return mHeight;
82 }
83 
85 {
86  return mWidth;
87 }
88 
89 void QgsMapToPixel::updateMatrix()
90 {
91  double rotation = mapRotation();
92 
93 #if 0 // debugging
94  QgsDebugMsg( QString( "XXX %7 -- xCent:%1 yCent:%2 mWidth:%3 mHeight:%4 uPP:%5 rot:%6" )
95  .arg( xCenter ).arg( yCenter ).arg( mWidth ).arg( mHeight )
96  .arg( mMapUnitsPerPixel ).arg( rotation ).arg(( quintptr )this, QT_POINTER_SIZE *2, 15, QChar( '0' ) ) );
97 #endif
98 
99  // NOTE: operations are done in the reverse order in which
100  // they are configured, so translation to geographical
101  // center happens first, then scaling, then rotation
102  // and finally translation to output viewport center
103 
104  if ( qgsDoubleNear( rotation, 0.0 ) )
105  {
106  //no rotation, return a simplified matrix
107  mMatrix = QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
108  .translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
109  return;
110  }
111 
112  double cy = mapHeight() / 2.0;
113  double cx = mapWidth() / 2.0;
114  mMatrix = QTransform::fromTranslate( cx, cy )
115  .rotate( rotation )
116  .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
117  .translate( -xCenter, -yCenter );
118 }
119 
120 QgsPoint QgsMapToPixel::toMapPoint( qreal x, qreal y ) const
121 {
122  bool invertible;
123  QTransform matrix = mMatrix.inverted( &invertible );
124  assert( invertible );
125  qreal mx, my;
126  matrix.map( x, y, &mx, &my );
127  //QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my));
128  return QgsPoint( mx, my );
129 }
130 
132 {
133  QgsPoint mapPt = toMapPoint( p.x(), p.y() );
134  return QgsPoint( mapPt );
135 }
136 
138 {
139  return toMapPoint( x, y );
140 }
141 
142 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
143 {
144  return toMapPoint( x, y );
145 }
146 
147 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
148 {
149  mMapUnitsPerPixel = mapUnitsPerPixel;
150  updateMatrix();
151 }
152 
154 {
155  return mMapUnitsPerPixel;
156 }
157 
158 void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
159 {
160  mRotation = degrees;
161  xCenter = cx;
162  yCenter = cy;
163  if ( mWidth < 0 )
164  {
165  // set width not that we can compute it
166  mWidth = (( xCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
167  }
168  updateMatrix();
169 }
170 
172 {
173  return mRotation;
174 }
175 
176 // @deprecated in 2.8
177 void QgsMapToPixel::setYMinimum( double ymin )
178 {
179  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
180  mRotation = 0.0;
181  updateMatrix();
182 }
183 
184 // @deprecated in 2.8
185 void QgsMapToPixel::setXMinimum( double xmin )
186 {
187  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
188  mRotation = 0.0;
189  updateMatrix();
190 }
191 
192 // @deprecated in 2.8
193 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
194 {
195  mMapUnitsPerPixel = mapUnitsPerPixel;
196  xMin = xmin;
197  yMin = ymin;
198  mHeight = ymax;
199  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
200  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
201  mRotation = 0.0;
202  updateMatrix();
203 }
204 
205 void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
206  double xc,
207  double yc,
208  int width,
209  int height,
210  double rotation )
211 {
212  mMapUnitsPerPixel = mapUnitsPerPixel;
213  xCenter = xc;
214  yCenter = yc;
215  mWidth = width;
216  mHeight = height;
217  mRotation = rotation;
218  xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
219  yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
220  updateMatrix();
221 }
222 
224 {
225  QString rep;
226  QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
227  << " center: " << xCenter << "," << yCenter
228  << " rotation: " << mRotation
229  << " size: " << mWidth << "x" << mHeight;
230  return rep;
231 }
232 
233 QgsPoint QgsMapToPixel::transform( qreal x, qreal y ) const
234 {
235  transformInPlace( x, y );
236  return QgsPoint( x, y );
237 }
238 
240 {
241  qreal x = p.x(), y = p.y();
242  transformInPlace( x, y );
243 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
244  return QgsPoint( x, y );
245 }
246 
248 {
249  qreal x = p->x(), y = p->y();
250  transformInPlace( x, y );
251 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
252  p->set( x, y );
253 }
254 
255 void QgsMapToPixel::transformInPlace( qreal &x, qreal &y ) const
256 {
257  // Map 2 Pixel
258  qreal mx, my;
259  mMatrix.map( x, y, &mx, &my );
260  //QgsDebugMsg(QString("XXX transformInPlace X : %1-->%2, Y: %3 -->%4").arg(x).arg(mx).arg(y).arg(my));
261  x = mx; y = my;
262 }
int mapWidth() const
Return current map width in pixels The information is only known if setRotation was used...
void setMapRotation(double degrees, double cx, double cy)
Set map rotation in degrees (clockwise)
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void transformInPlace(qreal &x, qreal &y) const
QgsPoint transform(const QgsPoint &p) const
Q_DECL_DEPRECATED void setParameters(double mapUnitsPerPixel, double xmin, double ymin, double height)
double mapRotation() const
Return current map rotation in degrees.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:347
double x() const
Definition: qgspoint.h:126
Q_DECL_DEPRECATED void setXMinimum(double xmin)
set minimum x value
QString showParameters() const
String representation of the parameters used in the transform.
double mapUnitsPerPixel() const
Return current map units per pixel.
void set(double x, double y)
Definition: qgspoint.h:117
A class to represent a point.
Definition: qgspoint.h:63
QgsPoint toMapCoordinatesF(double x, double y) const
QgsPoint toMapCoordinates(int x, int y) const
int mapHeight() const
Return current map height in pixels.
double y() const
Definition: qgspoint.h:134
void setMapUnitsPerPixel(double mapUnitsPerPixel)
~QgsMapToPixel()
destructor
QgsPoint toMapPoint(qreal x, qreal y) const
Q_DECL_DEPRECATED void setYMinimum(double ymin)
Set minimum y value.