~ubuntu-branches/ubuntu/trusty/qgis/trusty

1.1.2 by William Grant
Import upstream version 0.8.0
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
 ***************************************************************************/
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
17
/* $Id$ */
1.1.2 by William Grant
Import upstream version 0.8.0
18
#include "qgsmaptopixel.h"
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
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
35
QgsMapToPixel::~QgsMapToPixel()
36
{
37
}
38
39
QgsPoint QgsMapToPixel::toMapPoint( int x, int y ) const
1.1.2 by William Grant
Import upstream version 0.8.0
40
{
41
  double mx = x * mMapUnitsPerPixel + xMin;
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
42
  double my = -1 * (( y - yMax ) * mMapUnitsPerPixel - yMin );
43
  return QgsPoint( mx, my );
44
}
45
46
QgsPoint QgsMapToPixel::toMapCoordinates( QPoint p ) const
47
{
48
  QgsPoint mapPt = toMapPoint( p.x(), p.y() );
49
  return QgsPoint( mapPt );
50
}
51
52
QgsPoint QgsMapToPixel::toMapCoordinates( int x, int y ) const
53
{
54
  return toMapPoint( x, y );
55
}
56
57
void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
58
{
59
  mMapUnitsPerPixel = mapUnitsPerPixel;
60
}
61
62
double QgsMapToPixel::mapUnitsPerPixel() const
1.1.2 by William Grant
Import upstream version 0.8.0
63
{
64
  return mMapUnitsPerPixel;
65
}
66
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
67
void QgsMapToPixel::setYMaximum( double ymax )
1.1.2 by William Grant
Import upstream version 0.8.0
68
{
69
  yMax = ymax;
70
}
71
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
72
void QgsMapToPixel::setYMinimum( double ymin )
1.1.2 by William Grant
Import upstream version 0.8.0
73
{
74
  yMin = ymin;
75
}
76
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
77
void QgsMapToPixel::setXMinimum( double xmin )
1.1.2 by William Grant
Import upstream version 0.8.0
78
{
79
  xMin = xmin;
80
}
81
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
82
void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
1.1.2 by William Grant
Import upstream version 0.8.0
83
{
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
84
  mMapUnitsPerPixel = mapUnitsPerPixel;
1.1.2 by William Grant
Import upstream version 0.8.0
85
  xMin = xmin;
86
  yMin = ymin;
87
  yMax = ymax;
88
89
}
90
91
QString QgsMapToPixel::showParameters()
92
{
93
  QString rep;
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
94
  QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel
95
  << " X minimum: " << xMin << " Y minimum: " << yMin << " Y maximum: " << yMax;
1.1.2 by William Grant
Import upstream version 0.8.0
96
  return rep;
97
98
}
1.2.1 by Francesco Paolo Lovergine
Import upstream version 1.4.0+12730
99
100
101
QgsPoint QgsMapToPixel::transform( double x, double y ) const
102
{
103
  transformInPlace( x, y );
104
  return QgsPoint( x, y );
105
}
106
107
QgsPoint QgsMapToPixel::transform( const QgsPoint& p ) const
108
{
109
  double dx = p.x();
110
  double dy = p.y();
111
  transformInPlace( dx, dy );
112
113
// QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy));
114
  return QgsPoint( dx, dy );
115
}
116
117
void QgsMapToPixel::transform( QgsPoint* p ) const
118
{
119
  double x = p->x();
120
  double y = p->y();
121
  transformInPlace( x, y );
122
123
#ifdef QGISDEBUG
124
// QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y));
125
#endif
126
  p->set( x, y );
127
}
128
129
void QgsMapToPixel::transformInPlace( double& x, double& y ) const
130
{
131
  x = ( x - xMin ) / mMapUnitsPerPixel;
132
  y = yMax - ( y - yMin ) / mMapUnitsPerPixel;
133
}
134
135
void QgsMapToPixel::transformInPlace( std::vector<double>& x,
136
                                      std::vector<double>& y ) const
137
{
138
  assert( x.size() == y.size() );
139
  for ( unsigned int i = 0; i < x.size(); ++i )
140
    transformInPlace( x[i], y[i] );
141
}