QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
23 #include "qgslogger.h"
24 
25 QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
26  double ymax,
27  double ymin,
28  double xmin )
29  : mMapUnitsPerPixel( mapUnitsPerPixel )
30  , yMax( ymax )
31  , yMin( ymin )
32  , xMin( xmin )
33 {
34 }
35 
37 {
38 }
39 
40 QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const
41 {
42  double mx = x * mMapUnitsPerPixel + xMin;
43  double my = -1 * (( y - yMax ) * mMapUnitsPerPixel - yMin );
44  return QgsPoint( mx, my );
45 }
46 
48 {
49  QgsPoint mapPt = toMapPoint( p.x(), p.y() );
50  return QgsPoint( mapPt );
51 }
52 
54 {
55  return toMapPoint( x, y );
56 }
57 
58 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
59 {
60  return toMapPoint( x, y );
61 }
62 
63 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
64 {
66 }
67 
69 {
70  return mMapUnitsPerPixel;
71 }
72 
73 void QgsMapToPixel::setYMaximum( double ymax )
74 {
75  yMax = ymax;
76 }
77 
78 void QgsMapToPixel::setYMinimum( double ymin )
79 {
80  yMin = ymin;
81 }
82 
83 void QgsMapToPixel::setXMinimum( double xmin )
84 {
85  xMin = xmin;
86 }
87 
88 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
89 {
91  xMin = xmin;
92  yMin = ymin;
93  yMax = ymax;
94 
95 }
96 
98 {
99  QString rep;
100  QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
101  << " X minimum: " << xMin << " Y minimum: " << yMin << " Y maximum: " << yMax;
102  return rep;
103 
104 }
105 
106 
107 QgsPoint QgsMapToPixel::transform( double x, double y ) const
108 {
109  transformInPlace( x, y );
110  return QgsPoint( x, y );
111 }
112 
114 {
115  double dx = p.x();
116  double dy = p.y();
117  transformInPlace( dx, dy );
118 
119 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
120  return QgsPoint( dx, dy );
121 }
122 
124 {
125  double x = p->x();
126  double y = p->y();
127  transformInPlace( x, y );
128 
129 #ifdef QGISDEBUG
130 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
131 #endif
132  p->set( x, y );
133 }
134 
135 void QgsMapToPixel::transformInPlace( double& x, double& y ) const
136 {
137  x = ( x - xMin ) / mMapUnitsPerPixel;
138  y = yMax - ( y - yMin ) / mMapUnitsPerPixel;
139 }
140 
141 void QgsMapToPixel::transformInPlace( QVector<double>& x,
142  QVector<double>& y ) const
143 {
144  assert( x.size() == y.size() );
145  for ( int i = 0; i < x.size(); ++i )
146  transformInPlace( x[i], y[i] );
147 }
148 
149 #ifdef ANDROID
150 void QgsMapToPixel::transformInPlace( float& x, float& y ) const
151 {
152  x = ( x - xMin ) / mMapUnitsPerPixel;
153  y = yMax - ( y - yMin ) / mMapUnitsPerPixel;
154 }
155 
156 void QgsMapToPixel::transformInPlace( QVector<float>& x,
157  QVector<float>& y ) const
158 {
159  assert( x.size() == y.size() );
160  for ( unsigned int i = 0; i < x.size(); ++i )
161  transformInPlace( x[i], y[i] );
162 }
163 #endif