QGIS API Documentation  2.7.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  updateMatrix();
43 }
44 
46  : mMapUnitsPerPixel( 1 )
47  , mWidth( 1 )
48  , mHeight( 1 )
49  , mRotation( 0.0 )
50  , xCenter( 0.5 )
51  , yCenter( 0.5 )
52  , xMin( 0 )
53  , yMin( 0 )
54 {
55  updateMatrix();
56 }
57 
58 QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
59  double height,
60  double ymin,
61  double xmin )
62  : mMapUnitsPerPixel( mapUnitsPerPixel )
63  , mWidth( -1 )
64  , mHeight( height )
65  , mRotation( 0.0 )
66  , xCenter( 0.0 )
67  , yCenter( 0.0 )
68  , xMin( xmin )
69  , yMin( ymin )
70 {
71  updateMatrix();
72 }
73 
75 {
76 }
77 
79 {
80  return mHeight;
81 }
82 
84 {
85  return mWidth;
86 }
87 
88 void QgsMapToPixel::updateMatrix()
89 {
90  double rotation = mapRotation();
91 
92 #if 0 // debugging
93  QgsDebugMsg( QString( "XXX %7 -- xCent:%1 yCent:%2 mWidth:%3 mHeight:%4 uPP:%5 rot:%6" )
94  .arg( xCenter ).arg( yCenter ).arg( mWidth ).arg( mHeight )
95  .arg( mMapUnitsPerPixel ).arg( rotation ).arg(( quintptr )this, QT_POINTER_SIZE *2, 15, QChar( '0' ) ) );
96 #endif
97 
98  // NOTE: operations are done in the reverse order in which
99  // they are configured, so translation to geographical
100  // center happens first, then scaling, then rotation
101  // and finally translation to output viewport center
102 
103  if ( ! rotation )
104  {
105  // Returning a simplified matrix in hope it'll give expected
106  // results from an existing test, see
107  // https://travis-ci.org/qgis/QGIS/builds/42508945
108  mMatrix = QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
109  .translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
110  return;
111  }
112 
113  double cy = mapHeight() / 2.0;
114  double cx = mapWidth() / 2.0;
115  mMatrix = QTransform::fromTranslate( cx, cy )
116  .rotate( rotation )
117  .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
118  .translate( -xCenter, -yCenter )
119  ;
120 }
121 
122 QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const
123 {
124  bool invertible;
125  QTransform matrix = mMatrix.inverted( &invertible );
126  assert( invertible );
127  double mx, my;
128  matrix.map( x, y, &mx, &my );
129  QgsPoint ret( mx, my );
130 
131  //QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my));
132 
133  return ret;
134 }
135 
137 {
138  QgsPoint mapPt = toMapPoint( p.x(), p.y() );
139  return QgsPoint( mapPt );
140 }
141 
143 {
144  return toMapPoint( x, y );
145 }
146 
147 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
148 {
149  return toMapPoint( x, y );
150 }
151 
152 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
153 {
154  mMapUnitsPerPixel = mapUnitsPerPixel;
155  updateMatrix();
156 }
157 
159 {
160  return mMapUnitsPerPixel;
161 }
162 
163 void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
164 {
165  mRotation = degrees;
166  xCenter = cx;
167  yCenter = cy;
168  if ( mWidth < 0 )
169  {
170  // set width not that we can compute it
171  mWidth = (( xCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
172  }
173  updateMatrix();
174 }
175 
177 {
178  return mRotation;
179 }
180 
181 // @deprecated in 2.8
182 void QgsMapToPixel::setYMinimum( double ymin )
183 {
184  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
185  mRotation = 0.0;
186  updateMatrix();
187 }
188 
189 // @deprecated in 2.8
190 void QgsMapToPixel::setXMinimum( double xmin )
191 {
192  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
193  mRotation = 0.0;
194  updateMatrix();
195 }
196 
197 // @deprecated in 2.8
198 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
199 {
200  mMapUnitsPerPixel = mapUnitsPerPixel;
201  xMin = xmin;
202  yMin = ymin;
203  mHeight = ymax;
204  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
205  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
206  mRotation = 0.0;
207  updateMatrix();
208 }
209 
210 void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
211  double xc,
212  double yc,
213  int width,
214  int height,
215  double rotation )
216 {
217  mMapUnitsPerPixel = mapUnitsPerPixel;
218  xCenter = xc;
219  yCenter = yc;
220  mWidth = width;
221  mHeight = height;
222  mRotation = rotation;
223  xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
224  yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
225  updateMatrix();
226 }
227 
229 {
230  QString rep;
231  QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
232  << " center: " << xCenter << "," << yCenter
233  << " rotation: " << mRotation
234  << " size: " << mWidth << "x" << mHeight;
235  return rep;
236 
237 }
238 
239 
240 QgsPoint QgsMapToPixel::transform( double x, double y ) const
241 {
242  transformInPlace( x, y );
243  return QgsPoint( x, y );
244 }
245 
247 {
248  double dx = p.x();
249  double dy = p.y();
250  transformInPlace( dx, dy );
251 
252 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
253  return QgsPoint( dx, dy );
254 }
255 
257 {
258  double x = p->x();
259  double y = p->y();
260  transformInPlace( x, y );
261 
262 #ifdef QGISDEBUG
263 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
264 #endif
265  p->set( x, y );
266 }
267 
268 void QgsMapToPixel::transformInPlace( qreal& x, qreal& y ) const
269 {
270  // Map 2 Pixel
271  double mx, my;
272  mMatrix.map( x, y, &mx, &my );
273  //QgsDebugMsg(QString("XXX transformInPlace X : %1-->%2, Y: %3 -->%4").arg(x).arg(mx).arg(y).arg(my));
274  x = mx; y = my;
275 }
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
void setParameters(double mapUnitsPerPixel, double xmin, double ymin, double height)
double mapRotation() const
Return current map rotation in degrees.
double x() const
Definition: qgspoint.h:126
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
void setYMinimum(double ymin)
Set minimum y value.