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 
121 QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const
122 {
123  bool invertible;
124  QTransform matrix = mMatrix.inverted( &invertible );
125  assert( invertible );
126  double mx, my;
127  matrix.map( x, y, &mx, &my );
128  QgsPoint ret( mx, my );
129 
130  //QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my));
131 
132  return ret;
133 }
134 
136 {
137  QgsPoint mapPt = toMapPoint( p.x(), p.y() );
138  return QgsPoint( mapPt );
139 }
140 
142 {
143  return toMapPoint( x, y );
144 }
145 
146 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
147 {
148  return toMapPoint( x, y );
149 }
150 
151 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
152 {
153  mMapUnitsPerPixel = mapUnitsPerPixel;
154  updateMatrix();
155 }
156 
158 {
159  return mMapUnitsPerPixel;
160 }
161 
162 void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
163 {
164  mRotation = degrees;
165  xCenter = cx;
166  yCenter = cy;
167  if ( mWidth < 0 )
168  {
169  // set width not that we can compute it
170  mWidth = (( xCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
171  }
172  updateMatrix();
173 }
174 
176 {
177  return mRotation;
178 }
179 
180 // @deprecated in 2.8
181 void QgsMapToPixel::setYMinimum( double ymin )
182 {
183  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
184  mRotation = 0.0;
185  updateMatrix();
186 }
187 
188 // @deprecated in 2.8
189 void QgsMapToPixel::setXMinimum( double xmin )
190 {
191  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
192  mRotation = 0.0;
193  updateMatrix();
194 }
195 
196 // @deprecated in 2.8
197 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
198 {
199  mMapUnitsPerPixel = mapUnitsPerPixel;
200  xMin = xmin;
201  yMin = ymin;
202  mHeight = ymax;
203  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
204  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
205  mRotation = 0.0;
206  updateMatrix();
207 }
208 
209 void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
210  double xc,
211  double yc,
212  int width,
213  int height,
214  double rotation )
215 {
216  mMapUnitsPerPixel = mapUnitsPerPixel;
217  xCenter = xc;
218  yCenter = yc;
219  mWidth = width;
220  mHeight = height;
221  mRotation = rotation;
222  xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
223  yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
224  updateMatrix();
225 }
226 
228 {
229  QString rep;
230  QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
231  << " center: " << xCenter << "," << yCenter
232  << " rotation: " << mRotation
233  << " size: " << mWidth << "x" << mHeight;
234  return rep;
235 
236 }
237 
238 
239 QgsPoint QgsMapToPixel::transform( double x, double y ) const
240 {
241  transformInPlace( x, y );
242  return QgsPoint( x, y );
243 }
244 
246 {
247  double dx = p.x();
248  double dy = p.y();
249  transformInPlace( dx, dy );
250 
251 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
252  return QgsPoint( dx, dy );
253 }
254 
256 {
257  double x = p->x();
258  double y = p->y();
259  transformInPlace( x, y );
260 
261 #ifdef QGISDEBUG
262 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
263 #endif
264  p->set( x, y );
265 }
266 
267 void QgsMapToPixel::transformInPlace( qreal& x, qreal& y ) const
268 {
269  // Map 2 Pixel
270  double mx, my;
271  mMatrix.map( x, y, &mx, &my );
272  //QgsDebugMsg(QString("XXX transformInPlace X : %1-->%2, Y: %3 -->%4").arg(x).arg(mx).arg(y).arg(my));
273  x = mx; y = my;
274 }
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:327
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(double x, double y) const
Q_DECL_DEPRECATED void setYMinimum(double ymin)
Set minimum y value.