QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 
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 #ifdef QT_ARCH_ARM
142 void QgsMapToPixel::transformInPlace( qreal& x, qreal& y ) const
143 {
144  x = ( x - xMin ) / mMapUnitsPerPixel;
145  y = yMax - ( y - yMin ) / mMapUnitsPerPixel;
146 }
147 #endif
148 
149 void QgsMapToPixel::transformInPlace( QVector<double>& x,
150  QVector<double>& y ) const
151 {
152  assert( x.size() == y.size() );
153  for ( int i = 0; i < x.size(); ++i )
154  transformInPlace( x[i], y[i] );
155 }
156 
157 #ifdef ANDROID
158 void QgsMapToPixel::transformInPlace( float& x, float& y ) const
159 {
160  x = ( x - xMin ) / mMapUnitsPerPixel;
161  y = yMax - ( y - yMin ) / mMapUnitsPerPixel;
162 }
163 
164 void QgsMapToPixel::transformInPlace( QVector<float>& x,
165  QVector<float>& y ) const
166 {
167  assert( x.size() == y.size() );
168  for ( unsigned int i = 0; i < x.size(); ++i )
169  transformInPlace( x[i], y[i] );
170 }
171 #endif
172 
QgsMapToPixel(double mapUnitsPerPixel=0, double ymax=0, double ymin=0, double xmin=0)
void setYMaximum(double ymax)
Set maximum y value.
QgsPoint transform(const QgsPoint &p) const
double x() const
Definition: qgspoint.h:110
void transformInPlace(double &x, double &y) const
void setXMinimum(double xmin)
set minimum x value
QString showParameters()
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:101
A class to represent a point geometry.
Definition: qgspoint.h:63
QgsPoint toMapCoordinatesF(double x, double y) const
QgsPoint toMapCoordinates(int x, int y) const
double mMapUnitsPerPixel
double y() const
Definition: qgspoint.h:118
void setParameters(double mapUnitsPerPixel, double xmin, double ymin, double ymax)
void setMapUnitsPerPixel(double mapUnitsPerPixel)
~QgsMapToPixel()
destructor
QgsPoint toMapPoint(double x, double y) const
void setYMinimum(double ymin)
Set minimum y value.