QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgspoint.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspoint.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 
18 
19 #include "qgspoint.h"
20 #include "qgis.h"
21 #include <cmath>
22 #include <QTextStream>
23 #include <QObject> // for tr()
24 
25 #include "qgsexception.h"
26 
27 //
28 // QgsVector
29 //
30 
31 QgsVector::QgsVector() : m_x( 0.0 ), m_y( 0.0 )
32 {
33 }
34 
35 QgsVector::QgsVector( double x, double y ) : m_x( x ), m_y( y )
36 {
37 }
38 
40 {
41  return QgsVector( -m_x, -m_y );
42 }
43 
44 QgsVector QgsVector::operator*( double scalar ) const
45 {
46  return QgsVector( m_x * scalar, m_y * scalar );
47 }
48 
49 QgsVector QgsVector::operator/( double scalar ) const
50 {
51  return *this * ( 1.0 / scalar );
52 }
53 
55 {
56  return m_x * v.m_x + m_y * v.m_y;
57 }
58 
59 double QgsVector::length() const
60 {
61  return sqrt( m_x * m_x + m_y * m_y );
62 }
63 
64 double QgsVector::x() const
65 {
66  return m_x;
67 }
68 
69 double QgsVector::y() const
70 {
71  return m_y;
72 }
73 
74 // perpendicular vector (rotated 90 counter-clockwise)
76 {
77  return QgsVector( -m_y, m_x );
78 }
79 
80 double QgsVector::angle( void ) const
81 {
82  double ang = atan2( m_y, m_x );
83  return ang < 0.0 ? ang + 2.0 * M_PI : ang;
84 }
85 
86 double QgsVector::angle( QgsVector v ) const
87 {
88  return v.angle() - angle();
89 }
90 
91 QgsVector QgsVector::rotateBy( double rot ) const
92 {
93  double ang = atan2( m_y, m_x ) + rot;
94  double len = length();
95  return QgsVector( len * cos( ang ), len * sin( ang ) );
96 }
97 
99 {
100  double len = length();
101 
102  if ( len == 0.0 )
103  {
104  throw QgsException( "normal vector of null vector undefined" );
105  }
106 
107  return *this / len;
108 }
109 
110 
111 //
112 // QgsPoint
113 //
114 
116 {
117  m_x = p.x();
118  m_y = p.y();
119 }
120 
121 QString QgsPoint::toString() const
122 {
123  QString rep;
124  QTextStream ot( &rep );
125  ot.setRealNumberPrecision( 12 );
126  ot << m_x << ", " << m_y;
127  return rep;
128 }
129 
130 QString QgsPoint::toString( int thePrecision ) const
131 {
132  QString x = qIsFinite( m_x ) ? QString::number( m_x, 'f', thePrecision ) : QObject::tr( "infinite" );
133  QString y = qIsFinite( m_y ) ? QString::number( m_y, 'f', thePrecision ) : QObject::tr( "infinite" );
134  return QString( "%1,%2" ).arg( x ).arg( y );
135 }
136 
137 QString QgsPoint::toDegreesMinutesSeconds( int thePrecision, const bool useSuffix, const bool padded ) const
138 {
139  //first, limit longitude to -360 to 360 degree range
140  double myWrappedX = fmod( m_x, 360.0 );
141  //next, wrap around longitudes > 180 or < -180 degrees, so that eg "190E" -> "170W"
142  if ( myWrappedX > 180.0 )
143  {
144  myWrappedX = myWrappedX - 360.0;
145  }
146  else if ( myWrappedX < -180.0 )
147  {
148  myWrappedX = myWrappedX + 360.0;
149  }
150 
151  int myDegreesX = int( qAbs( myWrappedX ) );
152  double myFloatMinutesX = double(( qAbs( myWrappedX ) - myDegreesX ) * 60 );
153  int myIntMinutesX = int( myFloatMinutesX );
154  double mySecondsX = double( myFloatMinutesX - myIntMinutesX ) * 60;
155 
156  int myDegreesY = int( qAbs( m_y ) );
157  double myFloatMinutesY = double(( qAbs( m_y ) - myDegreesY ) * 60 );
158  int myIntMinutesY = int( myFloatMinutesY );
159  double mySecondsY = double( myFloatMinutesY - myIntMinutesY ) * 60;
160 
161  //make sure rounding to specified precision doesn't create seconds >= 60
162  if ( qRound( mySecondsX * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
163  {
164  mySecondsX = qMax( mySecondsX - 60, 0.0 );
165  myIntMinutesX++;
166  if ( myIntMinutesX >= 60 )
167  {
168  myIntMinutesX -= 60;
169  myDegreesX++;
170  }
171  }
172  if ( qRound( mySecondsY * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
173  {
174  mySecondsY = qMax( mySecondsY - 60, 0.0 );
175  myIntMinutesY++;
176  if ( myIntMinutesY >= 60 )
177  {
178  myIntMinutesY -= 60;
179  myDegreesY++;
180  }
181  }
182 
183  QString myXHemisphere;
184  QString myYHemisphere;
185  QString myXSign;
186  QString myYSign;
187  if ( useSuffix )
188  {
189  myXHemisphere = myWrappedX < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
190  myYHemisphere = m_y < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
191  }
192  else
193  {
194  if ( myWrappedX < 0 )
195  {
196  myXSign = QObject::tr( "-" );
197  }
198  if ( m_y < 0 )
199  {
200  myYSign = QObject::tr( "-" );
201  }
202  }
203  //check if coordinate is all zeros for the specified precision, and if so,
204  //remove the sign and hemisphere strings
205  if ( myDegreesX == 0 && myIntMinutesX == 0 && qRound( mySecondsX * pow( 10.0, thePrecision ) ) == 0 )
206  {
207  myXSign = QString();
208  myXHemisphere = QString();
209  }
210  if ( myDegreesY == 0 && myIntMinutesY == 0 && qRound( mySecondsY * pow( 10.0, thePrecision ) ) == 0 )
211  {
212  myYSign = QString();
213  myYHemisphere = QString();
214  }
215  //also remove directional prefix from 180 degree longitudes
216  if ( myDegreesX == 180 && myIntMinutesX == 0 && qRound( mySecondsX * pow( 10.0, thePrecision ) ) == 0 )
217  {
218  myXHemisphere = QString();
219  }
220  //pad minutes with leading digits if required
221  QString myMinutesX = padded ? QString( "%1" ).arg( myIntMinutesX, 2, 10, QChar( '0' ) ) : QString::number( myIntMinutesX );
222  QString myMinutesY = padded ? QString( "%1" ).arg( myIntMinutesY, 2, 10, QChar( '0' ) ) : QString::number( myIntMinutesY );
223  //pad seconds with leading digits if required
224  int digits = 2 + ( thePrecision == 0 ? 0 : 1 + thePrecision ); //1 for decimal place if required
225  QString myStrSecondsX = padded ? QString( "%1" ).arg( mySecondsX, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( mySecondsX, 'f', thePrecision );
226  QString myStrSecondsY = padded ? QString( "%1" ).arg( mySecondsY, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( mySecondsY, 'f', thePrecision );
227 
228  QString rep = myXSign + QString::number( myDegreesX ) + QChar( 176 ) +
229  myMinutesX + QString( "'" ) +
230  myStrSecondsX + QString( "\"" ) +
231  myXHemisphere + QString( "," ) +
232  myYSign + QString::number( myDegreesY ) + QChar( 176 ) +
233  myMinutesY + QString( "'" ) +
234  myStrSecondsY + QString( "\"" ) +
235  myYHemisphere;
236  return rep;
237 }
238 
239 QString QgsPoint::toDegreesMinutes( int thePrecision, const bool useSuffix, const bool padded ) const
240 {
241  //first, limit longitude to -360 to 360 degree range
242  double myWrappedX = fmod( m_x, 360.0 );
243  //next, wrap around longitudes > 180 or < -180 degrees, so that eg "190E" -> "170W"
244  if ( myWrappedX > 180.0 )
245  {
246  myWrappedX = myWrappedX - 360.0;
247  }
248  else if ( myWrappedX < -180.0 )
249  {
250  myWrappedX = myWrappedX + 360.0;
251  }
252 
253  int myDegreesX = int( qAbs( myWrappedX ) );
254  double myFloatMinutesX = double(( qAbs( myWrappedX ) - myDegreesX ) * 60 );
255 
256  int myDegreesY = int( qAbs( m_y ) );
257  double myFloatMinutesY = double(( qAbs( m_y ) - myDegreesY ) * 60 );
258 
259  //make sure rounding to specified precision doesn't create minutes >= 60
260  if ( qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
261  {
262  myFloatMinutesX = qMax( myFloatMinutesX - 60, 0.0 );
263  myDegreesX++;
264  }
265  if ( qRound( myFloatMinutesY * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
266  {
267  myFloatMinutesY = qMax( myFloatMinutesY - 60, 0.0 );
268  myDegreesY++;
269  }
270 
271  QString myXHemisphere;
272  QString myYHemisphere;
273  QString myXSign;
274  QString myYSign;
275  if ( useSuffix )
276  {
277  myXHemisphere = myWrappedX < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
278  myYHemisphere = m_y < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
279  }
280  else
281  {
282  if ( myWrappedX < 0 )
283  {
284  myXSign = QObject::tr( "-" );
285  }
286  if ( m_y < 0 )
287  {
288  myYSign = QObject::tr( "-" );
289  }
290  }
291  //check if coordinate is all zeros for the specified precision, and if so,
292  //remove the sign and hemisphere strings
293  if ( myDegreesX == 0 && qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) == 0 )
294  {
295  myXSign = QString();
296  myXHemisphere = QString();
297  }
298  if ( myDegreesY == 0 && qRound( myFloatMinutesY * pow( 10.0, thePrecision ) ) == 0 )
299  {
300  myYSign = QString();
301  myYHemisphere = QString();
302  }
303  //also remove directional prefix from 180 degree longitudes
304  if ( myDegreesX == 180 && qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) == 0 )
305  {
306  myXHemisphere = QString();
307  }
308 
309  //pad minutes with leading digits if required
310  int digits = 2 + ( thePrecision == 0 ? 0 : 1 + thePrecision ); //1 for decimal place if required
311  QString myStrMinutesX = padded ? QString( "%1" ).arg( myFloatMinutesX, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( myFloatMinutesX, 'f', thePrecision );
312  QString myStrMinutesY = padded ? QString( "%1" ).arg( myFloatMinutesY, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( myFloatMinutesY, 'f', thePrecision );
313 
314  QString rep = myXSign + QString::number( myDegreesX ) + QChar( 176 ) +
315  myStrMinutesX + QString( "'" ) +
316  myXHemisphere + QString( "," ) +
317  myYSign + QString::number( myDegreesY ) + QChar( 176 ) +
318  myStrMinutesY + QString( "'" ) +
319  myYHemisphere;
320  return rep;
321 }
322 
323 QString QgsPoint::wellKnownText() const
324 {
325  return QString( "POINT(%1 %2)" ).arg( qgsDoubleToString( m_x ) ).arg( qgsDoubleToString( m_y ) );
326 }
327 
328 double QgsPoint::sqrDist( double x, double y ) const
329 {
330  return ( m_x - x ) * ( m_x - x ) + ( m_y - y ) * ( m_y - y );
331 }
332 
333 double QgsPoint::sqrDist( const QgsPoint& other ) const
334 {
335  return sqrDist( other.x(), other.y() );
336 }
337 
338 double QgsPoint::azimuth( const QgsPoint& other )
339 {
340  double dx = other.x() - m_x;
341  double dy = other.y() - m_y;
342  return ( atan2( dx, dy ) * 180.0 / M_PI );
343 }
344 
345 // operators
346 bool QgsPoint::operator==( const QgsPoint & other )
347 {
348  if (( m_x == other.x() ) && ( m_y == other.y() ) )
349  return true;
350  else
351  return false;
352 }
353 
354 bool QgsPoint::operator!=( const QgsPoint & other ) const
355 {
356  if (( m_x == other.x() ) && ( m_y == other.y() ) )
357  return false;
358  else
359  return true;
360 }
361 
363 {
364  if ( &other != this )
365  {
366  m_x = other.x();
367  m_y = other.y();
368  }
369 
370  return *this;
371 }
372 
373 void QgsPoint::multiply( const double& scalar )
374 {
375  m_x *= scalar;
376  m_y *= scalar;
377 }
378 
379 int QgsPoint::onSegment( const QgsPoint& a, const QgsPoint& b ) const
380 {
381  //algorithm from 'graphics GEMS', A. Paeth: 'A Fast 2D Point-on-line test'
382  if (
383  qAbs(( b.y() - a.y() ) *( m_x - a.x() ) - ( m_y - a.y() ) *( b.x() - a.x() ) )
384  >= qMax( qAbs( b.x() - a.x() ), qAbs( b.y() - a.y() ) )
385  )
386  {
387  return 0;
388  }
389  if (( b.x() < a.x() && a.x() < m_x ) || ( b.y() < a.y() && a.y() < m_y ) )
390  {
391  return 1;
392  }
393  if (( m_x < a.x() && a.x() < b.x() ) || ( m_y < a.y() && a.y() < b.y() ) )
394  {
395  return 1;
396  }
397  if (( a.x() < b.x() && b.x() < m_x ) || ( a.y() < b.y() && b.y() < m_y ) )
398  {
399  return 3;
400  }
401  if (( m_x < b.x() && b.x() < a.x() ) || ( m_y < b.y() && b.y() < a.y() ) )
402  {
403  return 3;
404  }
405 
406  return 2;
407 }
408 
409 double QgsPoint::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon ) const
410 {
411  double nx, ny; //normal vector
412 
413  nx = y2 - y1;
414  ny = -( x2 - x1 );
415 
416  double t;
417  t = ( m_x * ny - m_y * nx - x1 * ny + y1 * nx ) / (( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
418 
419  if ( t < 0.0 )
420  {
421  minDistPoint.setX( x1 );
422  minDistPoint.setY( y1 );
423  }
424  else if ( t > 1.0 )
425  {
426  minDistPoint.setX( x2 );
427  minDistPoint.setY( y2 );
428  }
429  else
430  {
431  minDistPoint.setX( x1 + t *( x2 - x1 ) );
432  minDistPoint.setY( y1 + t *( y2 - y1 ) );
433  }
434 
435  double dist = sqrDist( minDistPoint );
436  //prevent rounding errors if the point is directly on the segment
437  if ( qgsDoubleNear( dist, 0.0, epsilon ) )
438  {
439  minDistPoint.setX( m_x );
440  minDistPoint.setY( m_y );
441  return 0.0;
442  }
443  return dist;
444 }
double m_x
Definition: qgspoint.h:34
QgsVector rotateBy(double rot) const
Definition: qgspoint.cpp:91
int onSegment(const QgsPoint &a, const QgsPoint &b) const
Test if this point is on the segment defined by points a, b.
Definition: qgspoint.cpp:379
double m_y
y coordinate
Definition: qgspoint.h:204
QgsVector operator-(void) const
Definition: qgspoint.cpp:39
QgsPoint & operator=(const QgsPoint &other)
Assignment.
Definition: qgspoint.cpp:362
double y() const
Definition: qgspoint.cpp:69
double sqrDist(double x, double y) const
Returns the squared distance between this point and x,y.
Definition: qgspoint.cpp:328
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:324
double x() const
Definition: qgspoint.h:110
bool operator!=(const QgsPoint &other) const
Inequality operator.
Definition: qgspoint.cpp:354
QgsPoint()
Default constructor.
Definition: qgspoint.h:67
double m_x
x coordinate
Definition: qgspoint.h:201
QgsVector perpVector() const
Definition: qgspoint.cpp:75
double sqrDistToSegment(double x1, double y1, double x2, double y2, QgsPoint &minDistPoint, double epsilon=DEFAULT_SEGMENT_EPSILON) const
Returns the minimum distance between this point and a segment.
Definition: qgspoint.cpp:409
#define M_PI
bool operator==(const QgsPoint &other)
equality operator
Definition: qgspoint.cpp:346
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:121
A class to represent a point geometry.
Definition: qgspoint.h:63
double azimuth(const QgsPoint &other)
Calculates azimut between this point and other one (clockwise in degree, starting from north) ...
Definition: qgspoint.cpp:338
A class to represent a vector.
Definition: qgspoint.h:32
void setX(double x)
Definition: qgspoint.h:87
QString qgsDoubleToString(const double &a, const int &precision=17)
Definition: qgis.h:316
double length() const
Definition: qgspoint.cpp:59
void setY(double y)
Definition: qgspoint.h:95
double angle(void) const
Definition: qgspoint.cpp:80
QgsVector normal() const
Definition: qgspoint.cpp:98
QString toDegreesMinutes(int thePrecision, const bool useSuffix=true, const bool padded=false) const
Return a string representation as degrees minutes.
Definition: qgspoint.cpp:239
QgsVector operator*(double scalar) const
Definition: qgspoint.cpp:44
double y() const
Definition: qgspoint.h:118
QString toDegreesMinutesSeconds(int thePrecision, const bool useSuffix=true, const bool padded=false) const
Return a string representation as degrees minutes seconds.
Definition: qgspoint.cpp:137
double x() const
Definition: qgspoint.cpp:64
QgsVector operator/(double scalar) const
Definition: qgspoint.cpp:49
Defines a qgis exception class.
Definition: qgsexception.h:25
QString wellKnownText() const
Definition: qgspoint.cpp:323
double m_y
Definition: qgspoint.h:34
void multiply(const double &scalar)
Multiply x and y by the given value.
Definition: qgspoint.cpp:373
#define tr(sourceText)