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