QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 degrees 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 QPointF QgsPoint::toQPointF() const
122 {
123  return QPointF( m_x, m_y );
124 }
125 
126 QString QgsPoint::toString() const
127 {
128  QString rep;
129  QTextStream ot( &rep );
130  ot.setRealNumberPrecision( 12 );
131  ot << m_x << ", " << m_y;
132  return rep;
133 }
134 
135 QString QgsPoint::toString( int thePrecision ) const
136 {
137  QString x = qIsFinite( m_x ) ? QString::number( m_x, 'f', thePrecision ) : QObject::tr( "infinite" );
138  QString y = qIsFinite( m_y ) ? QString::number( m_y, 'f', thePrecision ) : QObject::tr( "infinite" );
139  return QString( "%1,%2" ).arg( x ).arg( y );
140 }
141 
142 QString QgsPoint::toDegreesMinutesSeconds( int thePrecision, const bool useSuffix, const bool padded ) const
143 {
144  //first, limit longitude to -360 to 360 degree range
145  double myWrappedX = fmod( m_x, 360.0 );
146  //next, wrap around longitudes > 180 or < -180 degrees, so that eg "190E" -> "170W"
147  if ( myWrappedX > 180.0 )
148  {
149  myWrappedX = myWrappedX - 360.0;
150  }
151  else if ( myWrappedX < -180.0 )
152  {
153  myWrappedX = myWrappedX + 360.0;
154  }
155 
156  int myDegreesX = int( qAbs( myWrappedX ) );
157  double myFloatMinutesX = double(( qAbs( myWrappedX ) - myDegreesX ) * 60 );
158  int myIntMinutesX = int( myFloatMinutesX );
159  double mySecondsX = double( myFloatMinutesX - myIntMinutesX ) * 60;
160 
161  int myDegreesY = int( qAbs( m_y ) );
162  double myFloatMinutesY = double(( qAbs( m_y ) - myDegreesY ) * 60 );
163  int myIntMinutesY = int( myFloatMinutesY );
164  double mySecondsY = double( myFloatMinutesY - myIntMinutesY ) * 60;
165 
166  //make sure rounding to specified precision doesn't create seconds >= 60
167  if ( qRound( mySecondsX * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
168  {
169  mySecondsX = qMax( mySecondsX - 60, 0.0 );
170  myIntMinutesX++;
171  if ( myIntMinutesX >= 60 )
172  {
173  myIntMinutesX -= 60;
174  myDegreesX++;
175  }
176  }
177  if ( qRound( mySecondsY * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
178  {
179  mySecondsY = qMax( mySecondsY - 60, 0.0 );
180  myIntMinutesY++;
181  if ( myIntMinutesY >= 60 )
182  {
183  myIntMinutesY -= 60;
184  myDegreesY++;
185  }
186  }
187 
188  QString myXHemisphere;
189  QString myYHemisphere;
190  QString myXSign;
191  QString myYSign;
192  if ( useSuffix )
193  {
194  myXHemisphere = myWrappedX < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
195  myYHemisphere = m_y < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
196  }
197  else
198  {
199  if ( myWrappedX < 0 )
200  {
201  myXSign = QObject::tr( "-" );
202  }
203  if ( m_y < 0 )
204  {
205  myYSign = QObject::tr( "-" );
206  }
207  }
208  //check if coordinate is all zeros for the specified precision, and if so,
209  //remove the sign and hemisphere strings
210  if ( myDegreesX == 0 && myIntMinutesX == 0 && qRound( mySecondsX * pow( 10.0, thePrecision ) ) == 0 )
211  {
212  myXSign = QString();
213  myXHemisphere = QString();
214  }
215  if ( myDegreesY == 0 && myIntMinutesY == 0 && qRound( mySecondsY * pow( 10.0, thePrecision ) ) == 0 )
216  {
217  myYSign = QString();
218  myYHemisphere = QString();
219  }
220  //also remove directional prefix from 180 degree longitudes
221  if ( myDegreesX == 180 && myIntMinutesX == 0 && qRound( mySecondsX * pow( 10.0, thePrecision ) ) == 0 )
222  {
223  myXHemisphere = QString();
224  }
225  //pad minutes with leading digits if required
226  QString myMinutesX = padded ? QString( "%1" ).arg( myIntMinutesX, 2, 10, QChar( '0' ) ) : QString::number( myIntMinutesX );
227  QString myMinutesY = padded ? QString( "%1" ).arg( myIntMinutesY, 2, 10, QChar( '0' ) ) : QString::number( myIntMinutesY );
228  //pad seconds with leading digits if required
229  int digits = 2 + ( thePrecision == 0 ? 0 : 1 + thePrecision ); //1 for decimal place if required
230  QString myStrSecondsX = padded ? QString( "%1" ).arg( mySecondsX, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( mySecondsX, 'f', thePrecision );
231  QString myStrSecondsY = padded ? QString( "%1" ).arg( mySecondsY, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( mySecondsY, 'f', thePrecision );
232 
233  QString rep = myXSign + QString::number( myDegreesX ) + QChar( 176 ) +
234  myMinutesX + QChar( 0x2032 ) +
235  myStrSecondsX + QChar( 0x2033 ) +
236  myXHemisphere + QString( "," ) +
237  myYSign + QString::number( myDegreesY ) + QChar( 176 ) +
238  myMinutesY + QChar( 0x2032 ) +
239  myStrSecondsY + QChar( 0x2033 ) +
240  myYHemisphere;
241  return rep;
242 }
243 
244 QString QgsPoint::toDegreesMinutes( int thePrecision, const bool useSuffix, const bool padded ) const
245 {
246  //first, limit longitude to -360 to 360 degree range
247  double myWrappedX = fmod( m_x, 360.0 );
248  //next, wrap around longitudes > 180 or < -180 degrees, so that eg "190E" -> "170W"
249  if ( myWrappedX > 180.0 )
250  {
251  myWrappedX = myWrappedX - 360.0;
252  }
253  else if ( myWrappedX < -180.0 )
254  {
255  myWrappedX = myWrappedX + 360.0;
256  }
257 
258  int myDegreesX = int( qAbs( myWrappedX ) );
259  double myFloatMinutesX = double(( qAbs( myWrappedX ) - myDegreesX ) * 60 );
260 
261  int myDegreesY = int( qAbs( m_y ) );
262  double myFloatMinutesY = double(( qAbs( m_y ) - myDegreesY ) * 60 );
263 
264  //make sure rounding to specified precision doesn't create minutes >= 60
265  if ( qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
266  {
267  myFloatMinutesX = qMax( myFloatMinutesX - 60, 0.0 );
268  myDegreesX++;
269  }
270  if ( qRound( myFloatMinutesY * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
271  {
272  myFloatMinutesY = qMax( myFloatMinutesY - 60, 0.0 );
273  myDegreesY++;
274  }
275 
276  QString myXHemisphere;
277  QString myYHemisphere;
278  QString myXSign;
279  QString myYSign;
280  if ( useSuffix )
281  {
282  myXHemisphere = myWrappedX < 0 ? QObject::tr( "W" ) : QObject::tr( "E" );
283  myYHemisphere = m_y < 0 ? QObject::tr( "S" ) : QObject::tr( "N" );
284  }
285  else
286  {
287  if ( myWrappedX < 0 )
288  {
289  myXSign = QObject::tr( "-" );
290  }
291  if ( m_y < 0 )
292  {
293  myYSign = QObject::tr( "-" );
294  }
295  }
296  //check if coordinate is all zeros for the specified precision, and if so,
297  //remove the sign and hemisphere strings
298  if ( myDegreesX == 0 && qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) == 0 )
299  {
300  myXSign = QString();
301  myXHemisphere = QString();
302  }
303  if ( myDegreesY == 0 && qRound( myFloatMinutesY * pow( 10.0, thePrecision ) ) == 0 )
304  {
305  myYSign = QString();
306  myYHemisphere = QString();
307  }
308  //also remove directional prefix from 180 degree longitudes
309  if ( myDegreesX == 180 && qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) == 0 )
310  {
311  myXHemisphere = QString();
312  }
313 
314  //pad minutes with leading digits if required
315  int digits = 2 + ( thePrecision == 0 ? 0 : 1 + thePrecision ); //1 for decimal place if required
316  QString myStrMinutesX = padded ? QString( "%1" ).arg( myFloatMinutesX, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( myFloatMinutesX, 'f', thePrecision );
317  QString myStrMinutesY = padded ? QString( "%1" ).arg( myFloatMinutesY, digits, 'f', thePrecision, QChar( '0' ) ) : QString::number( myFloatMinutesY, 'f', thePrecision );
318 
319  QString rep = myXSign + QString::number( myDegreesX ) + QChar( 176 ) +
320  myStrMinutesX + QChar( 0x2032 ) +
321  myXHemisphere + QString( "," ) +
322  myYSign + QString::number( myDegreesY ) + QChar( 176 ) +
323  myStrMinutesY + QChar( 0x2032 ) +
324  myYHemisphere;
325  return rep;
326 }
327 
328 QString QgsPoint::wellKnownText() const
329 {
330  return QString( "POINT(%1 %2)" ).arg( qgsDoubleToString( m_x ) ).arg( qgsDoubleToString( m_y ) );
331 }
332 
333 double QgsPoint::sqrDist( double x, double y ) const
334 {
335  return ( m_x - x ) * ( m_x - x ) + ( m_y - y ) * ( m_y - y );
336 }
337 
338 double QgsPoint::sqrDist( const QgsPoint& other ) const
339 {
340  return sqrDist( other.x(), other.y() );
341 }
342 
343 double QgsPoint::azimuth( const QgsPoint& other )
344 {
345  double dx = other.x() - m_x;
346  double dy = other.y() - m_y;
347  return ( atan2( dx, dy ) * 180.0 / M_PI );
348 }
349 
350 // operators
351 bool QgsPoint::operator==( const QgsPoint & other )
352 {
353  if (( m_x == other.x() ) && ( m_y == other.y() ) )
354  return true;
355  else
356  return false;
357 }
358 
359 bool QgsPoint::operator!=( const QgsPoint & other ) const
360 {
361  if (( m_x == other.x() ) && ( m_y == other.y() ) )
362  return false;
363  else
364  return true;
365 }
366 
368 {
369  if ( &other != this )
370  {
371  m_x = other.x();
372  m_y = other.y();
373  }
374 
375  return *this;
376 }
377 
378 void QgsPoint::multiply( const double& scalar )
379 {
380  m_x *= scalar;
381  m_y *= scalar;
382 }
383 
384 int QgsPoint::onSegment( const QgsPoint& a, const QgsPoint& b ) const
385 {
386  //algorithm from 'graphics GEMS', A. Paeth: 'A Fast 2D Point-on-line test'
387  if (
388  qAbs(( b.y() - a.y() ) *( m_x - a.x() ) - ( m_y - a.y() ) *( b.x() - a.x() ) )
389  >= qMax( qAbs( b.x() - a.x() ), qAbs( b.y() - a.y() ) )
390  )
391  {
392  return 0;
393  }
394  if (( b.x() < a.x() && a.x() < m_x ) || ( b.y() < a.y() && a.y() < m_y ) )
395  {
396  return 1;
397  }
398  if (( m_x < a.x() && a.x() < b.x() ) || ( m_y < a.y() && a.y() < b.y() ) )
399  {
400  return 1;
401  }
402  if (( a.x() < b.x() && b.x() < m_x ) || ( a.y() < b.y() && b.y() < m_y ) )
403  {
404  return 3;
405  }
406  if (( m_x < b.x() && b.x() < a.x() ) || ( m_y < b.y() && b.y() < a.y() ) )
407  {
408  return 3;
409  }
410 
411  return 2;
412 }
413 
414 double QgsPoint::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon ) const
415 {
416  double nx, ny; //normal vector
417 
418  nx = y2 - y1;
419  ny = -( x2 - x1 );
420 
421  double t;
422  t = ( m_x * ny - m_y * nx - x1 * ny + y1 * nx ) / (( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
423 
424  if ( t < 0.0 )
425  {
426  minDistPoint.setX( x1 );
427  minDistPoint.setY( y1 );
428  }
429  else if ( t > 1.0 )
430  {
431  minDistPoint.setX( x2 );
432  minDistPoint.setY( y2 );
433  }
434  else
435  {
436  minDistPoint.setX( x1 + t *( x2 - x1 ) );
437  minDistPoint.setY( y1 + t *( y2 - y1 ) );
438  }
439 
440  double dist = sqrDist( minDistPoint );
441  //prevent rounding errors if the point is directly on the segment
442  if ( qgsDoubleNear( dist, 0.0, epsilon ) )
443  {
444  minDistPoint.setX( m_x );
445  minDistPoint.setY( m_y );
446  return 0.0;
447  }
448  return dist;
449 }
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:384
QgsVector operator-(void) const
Definition: qgspoint.cpp:39
QgsPoint & operator=(const QgsPoint &other)
Assignment.
Definition: qgspoint.cpp:367
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:333
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:325
double x() const
Definition: qgspoint.h:126
bool operator!=(const QgsPoint &other) const
Inequality operator.
Definition: qgspoint.cpp:359
QgsPoint()
Default constructor.
Definition: qgspoint.h:67
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:414
#define M_PI
bool operator==(const QgsPoint &other)
equality operator
Definition: qgspoint.cpp:351
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:126
A class to represent a point.
Definition: qgspoint.h:63
double azimuth(const QgsPoint &other)
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
Definition: qgspoint.cpp:343
A class to represent a vector.
Definition: qgspoint.h:32
void setX(double x)
Definition: qgspoint.h:103
QString qgsDoubleToString(const double &a, const int &precision=17)
Definition: qgis.h:317
double length() const
Definition: qgspoint.cpp:59
void setY(double y)
Definition: qgspoint.h:111
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:244
QgsVector operator*(double scalar) const
Definition: qgspoint.cpp:44
double y() const
Definition: qgspoint.h:134
QString toDegreesMinutesSeconds(int thePrecision, const bool useSuffix=true, const bool padded=false) const
Return a string representation as degrees minutes seconds.
Definition: qgspoint.cpp:142
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:328
void multiply(const double &scalar)
Multiply x and y by the given value.
Definition: qgspoint.cpp:378
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspoint.cpp:121
#define tr(sourceText)