Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 /* $Id$ */
18 #include "qgsmaptopixel.h"
19 #include <QPoint>
20 #include <QTextStream>
21 #include "qgslogger.h"
22 
23 QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel,
24  double ymax,
25  double ymin,
26  double xmin )
27  : mMapUnitsPerPixel( mapUnitsPerPixel ),
28  yMax( ymax ),
29  yMin( ymin ),
30  xMin( xmin ),
31  xMax( 0 ) // XXX wasn't originally specified? Why?
32 {
33 }
34 
36 {
37 }
38 
39 QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const
40 {
41  double mx = x * mMapUnitsPerPixel + xMin;
42  double my = -1 * (( y - yMax ) * mMapUnitsPerPixel - yMin );
43  return QgsPoint( mx, my );
44 }
45 
47 {
48  QgsPoint mapPt = toMapPoint( p.x(), p.y() );
49  return QgsPoint( mapPt );
50 }
51 
53 {
54  return toMapPoint( x, y );
55 }
56 
57 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
58 {
59  return toMapPoint( x, y );
60 }
61 
62 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
63 {
65 }
66 
68 {
69  return mMapUnitsPerPixel;
70 }
71 
72 void QgsMapToPixel::setYMaximum( double ymax )
73 {
74  yMax = ymax;
75 }
76 
77 void QgsMapToPixel::setYMinimum( double ymin )
78 {
79  yMin = ymin;
80 }
81 
82 void QgsMapToPixel::setXMinimum( double xmin )
83 {
84  xMin = xmin;
85 }
86 
87 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
88 {
90  xMin = xmin;
91  yMin = ymin;
92  yMax = ymax;
93 
94 }
95 
97 {
98  QString rep;
99  QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
100  << " X minimum: " << xMin << " Y minimum: " << yMin << " Y maximum: " << yMax;
101  return rep;
102 
103 }
104 
105 
106 QgsPoint QgsMapToPixel::transform( double x, double y ) const
107 {
108  transformInPlace( x, y );
109  return QgsPoint( x, y );
110 }
111 
113 {
114  double dx = p.x();
115  double dy = p.y();
116  transformInPlace( dx, dy );
117 
118 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
119  return QgsPoint( dx, dy );
120 }
121 
123 {
124  double x = p->x();
125  double y = p->y();
126  transformInPlace( x, y );
127 
128 #ifdef QGISDEBUG
129 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
130 #endif
131  p->set( x, y );
132 }
133 
134 void QgsMapToPixel::transformInPlace( double& x, double& y ) const
135 {
136  x = ( x - xMin ) / mMapUnitsPerPixel;
137  y = yMax - ( y - yMin ) / mMapUnitsPerPixel;
138 }
139 
140 void QgsMapToPixel::transformInPlace( std::vector<double>& x,
141  std::vector<double>& y ) const
142 {
143  assert( x.size() == y.size() );
144  for ( unsigned int i = 0; i < x.size(); ++i )
145  transformInPlace( x[i], y[i] );
146 }