QGIS API Documentation  2.9.0-Master
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  mMatrix = transform();
92 }
93 
94 QgsPoint QgsMapToPixel::toMapPoint( qreal x, qreal y ) const
95 {
96  bool invertible;
97  QTransform matrix = mMatrix.inverted( &invertible );
98  assert( invertible );
99  qreal mx, my;
100  matrix.map( x, y, &mx, &my );
101  //QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my));
102  return QgsPoint( mx, my );
103 }
104 
106 {
107  QgsPoint mapPt = toMapPoint( p.x(), p.y() );
108  return QgsPoint( mapPt );
109 }
110 
112 {
113  return toMapPoint( x, y );
114 }
115 
116 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
117 {
118  return toMapPoint( x, y );
119 }
120 
121 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
122 {
123  mMapUnitsPerPixel = mapUnitsPerPixel;
124  updateMatrix();
125 }
126 
128 {
129  return mMapUnitsPerPixel;
130 }
131 
132 void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
133 {
134  mRotation = degrees;
135  xCenter = cx;
136  yCenter = cy;
137  if ( mWidth < 0 )
138  {
139  // set width not that we can compute it
140  mWidth = (( xCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
141  }
142  updateMatrix();
143 }
144 
146 {
147  return mRotation;
148 }
149 
150 // @deprecated in 2.8
151 void QgsMapToPixel::setYMinimum( double ymin )
152 {
153  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
154  mRotation = 0.0;
155  updateMatrix();
156 }
157 
158 // @deprecated in 2.8
159 void QgsMapToPixel::setXMinimum( double xmin )
160 {
161  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
162  mRotation = 0.0;
163  updateMatrix();
164 }
165 
166 // @deprecated in 2.8
167 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
168 {
169  mMapUnitsPerPixel = mapUnitsPerPixel;
170  xMin = xmin;
171  yMin = ymin;
172  mHeight = ymax;
173  xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
174  yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
175  mRotation = 0.0;
176  updateMatrix();
177 }
178 
179 void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
180  double xc,
181  double yc,
182  int width,
183  int height,
184  double rotation )
185 {
186  mMapUnitsPerPixel = mapUnitsPerPixel;
187  xCenter = xc;
188  yCenter = yc;
189  mWidth = width;
190  mHeight = height;
191  mRotation = rotation;
192  xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
193  yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
194  updateMatrix();
195 }
196 
198 {
199  QString rep;
200  QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
201  << " center: " << xCenter << "," << yCenter
202  << " rotation: " << mRotation
203  << " size: " << mWidth << "x" << mHeight;
204  return rep;
205 }
206 
207 QgsPoint QgsMapToPixel::transform( qreal x, qreal y ) const
208 {
209  transformInPlace( x, y );
210  return QgsPoint( x, y );
211 }
212 
214 {
215  qreal x = p.x(), y = p.y();
216  transformInPlace( x, y );
217 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
218  return QgsPoint( x, y );
219 }
220 
222 {
223  qreal x = p->x(), y = p->y();
224  transformInPlace( x, y );
225 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
226  p->set( x, y );
227 }
228 
229 void QgsMapToPixel::transformInPlace( qreal &x, qreal &y ) const
230 {
231  // Map 2 Pixel
232  qreal mx, my;
233  mMatrix.map( x, y, &mx, &my );
234  //QgsDebugMsg(QString("XXX transformInPlace X : %1-->%2, Y: %3 -->%4").arg(x).arg(mx).arg(y).arg(my));
235  x = mx; y = my;
236 }
237 
238 QTransform QgsMapToPixel::transform() const
239 {
240  double rotation = mapRotation();
241  if ( qgsDoubleNear( rotation, 0.0 ) )
242  {
243  //no rotation, return a simplified matrix
244  return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
245  .translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
246  }
247 
248  double cy = mapHeight() / 2.0;
249  double cx = mapWidth() / 2.0;
250  return QTransform::fromTranslate( cx, cy )
251  .rotate( rotation )
252  .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
253  .translate( -xCenter, -yCenter );
254 }
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)
void transformInPlace(qreal &x, qreal &y) 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
QTransform transform() const
void setMapUnitsPerPixel(double mapUnitsPerPixel)
~QgsMapToPixel()
destructor
QgsPoint toMapPoint(qreal x, qreal y) const
Q_DECL_DEPRECATED void setYMinimum(double ymin)
Set minimum y value.