QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgspoint.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointv2.h
3  --------------
4  begin : September 2014
5  copyright : (C) 2014 by Marco Hugentobler
6  email : marco at sourcepole dot ch
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 #ifndef QGSPOINT_H
19 #define QGSPOINT_H
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include "qgsabstractgeometry.h"
24 #include "qgsrectangle.h"
25 
26 /***************************************************************************
27  * This class is considered CRITICAL and any change MUST be accompanied with
28  * full unit tests in testqgsgeometry.cpp.
29  * See details in QEP #17
30  ****************************************************************************/
31 
37 class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
38 {
39  Q_GADGET
40 
41  Q_PROPERTY( double x READ x WRITE setX )
42  Q_PROPERTY( double y READ y WRITE setY )
43  Q_PROPERTY( double z READ z WRITE setZ )
44  Q_PROPERTY( double m READ m WRITE setM )
45 
46  public:
47 
74 #ifndef SIP_RUN
75  QgsPoint( double x = std::numeric_limits<double>::quiet_NaN(), double y = std::numeric_limits<double>::quiet_NaN(), double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN(), QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown );
76 #else
77  QgsPoint( SIP_PYOBJECT x = Py_None, SIP_PYOBJECT y = Py_None, SIP_PYOBJECT z = Py_None, SIP_PYOBJECT m = Py_None, SIP_PYOBJECT wkbType = Py_None ) [( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0, QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown )];
78  % MethodCode
79  if ( sipCanConvertToType( a0, sipType_QgsPointXY, SIP_NOT_NONE ) && a1 == Py_None && a2 == Py_None && a3 == Py_None && a4 == Py_None )
80  {
81  int state;
82  sipIsErr = 0;
83 
84  QgsPointXY *p = reinterpret_cast<QgsPointXY *>( sipConvertToType( a0, sipType_QgsPointXY, 0, SIP_NOT_NONE, &state, &sipIsErr ) );
85  if ( sipIsErr )
86  {
87  sipReleaseType( p, sipType_QgsPointXY, state );
88  }
89  else
90  {
91  sipCpp = new sipQgsPoint( QgsPoint( *p ) );
92  }
93  }
94  else if ( sipCanConvertToType( a0, sipType_QPointF, SIP_NOT_NONE ) && a1 == Py_None && a2 == Py_None && a3 == Py_None && a4 == Py_None )
95  {
96  int state;
97  sipIsErr = 0;
98 
99  QPointF *p = reinterpret_cast<QPointF *>( sipConvertToType( a0, sipType_QPointF, 0, SIP_NOT_NONE, &state, &sipIsErr ) );
100  if ( sipIsErr )
101  {
102  sipReleaseType( p, sipType_QPointF, state );
103  }
104  else
105  {
106  sipCpp = new sipQgsPoint( QgsPoint( *p ) );
107  }
108  }
109  else if (
110  ( a0 == Py_None || PyFloat_AsDouble( a0 ) != -1.0 || !PyErr_Occurred() ) &&
111  ( a1 == Py_None || PyFloat_AsDouble( a1 ) != -1.0 || !PyErr_Occurred() ) &&
112  ( a2 == Py_None || PyFloat_AsDouble( a2 ) != -1.0 || !PyErr_Occurred() ) &&
113  ( a3 == Py_None || PyFloat_AsDouble( a3 ) != -1.0 || !PyErr_Occurred() ) &&
114  ( a4 == Py_None || sipCanConvertToEnum( a4, sipType_QgsWkbTypes_Type ) ) )
115  {
116  double x = a0 == Py_None ? std::numeric_limits<double>::quiet_NaN() : PyFloat_AsDouble( a0 );
117  double y = a1 == Py_None ? std::numeric_limits<double>::quiet_NaN() : PyFloat_AsDouble( a1 );
118  double z = a2 == Py_None ? std::numeric_limits<double>::quiet_NaN() : PyFloat_AsDouble( a2 );
119  double m = a3 == Py_None ? std::numeric_limits<double>::quiet_NaN() : PyFloat_AsDouble( a3 );
120  QgsWkbTypes::Type wkbType = a4 == Py_None ? QgsWkbTypes::Unknown : static_cast<QgsWkbTypes::Type>( sipConvertToEnum( a4, sipType_QgsWkbTypes_Type ) );
121  sipCpp = new sipQgsPoint( QgsPoint( x, y, z, m, wkbType ) );
122  }
123  else // Invalid ctor arguments
124  {
125  PyErr_SetString( PyExc_TypeError, QStringLiteral( "Invalid type in constructor arguments." ).toUtf8().constData() );
126  sipIsErr = 1;
127  }
128  % End
129 #endif
130 
134  explicit QgsPoint( const QgsPointXY &p ) SIP_SKIP;
135 
139  explicit QgsPoint( QPointF p ) SIP_SKIP;
140 
146  explicit QgsPoint( QgsWkbTypes::Type wkbType, double x = std::numeric_limits<double>::quiet_NaN(), double y = std::numeric_limits<double>::quiet_NaN(), double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN() ) SIP_SKIP;
147 
148  bool operator==( const QgsAbstractGeometry &other ) const override
149  {
150  const QgsPoint *pt = qgsgeometry_cast< const QgsPoint * >( &other );
151  if ( !pt )
152  return false;
153 
154  const QgsWkbTypes::Type type = wkbType();
155 
156  if ( pt->wkbType() != type )
157  return false;
158 
159  const bool nan1X = std::isnan( mX );
160  const bool nan2X = std::isnan( pt->x() );
161  if ( nan1X != nan2X )
162  return false;
163  if ( !nan1X && !qgsDoubleNear( mX, pt->x(), 1E-8 ) )
164  return false;
165 
166  const bool nan1Y = std::isnan( mY );
167  const bool nan2Y = std::isnan( pt->y() );
168  if ( nan1Y != nan2Y )
169  return false;
170  if ( !nan1Y && !qgsDoubleNear( mY, pt->y(), 1E-8 ) )
171  return false;
172 
173  if ( QgsWkbTypes::hasZ( type ) )
174  {
175  const bool nan1Z = std::isnan( mZ );
176  const bool nan2Z = std::isnan( pt->z() );
177  if ( nan1Z != nan2Z )
178  return false;
179  if ( !nan1Z && !qgsDoubleNear( mZ, pt->z(), 1E-8 ) )
180  return false;
181  }
182 
183  if ( QgsWkbTypes::hasM( type ) )
184  {
185  const bool nan1M = std::isnan( mM );
186  const bool nan2M = std::isnan( pt->m() );
187  if ( nan1M != nan2M )
188  return false;
189  if ( !nan1M && !qgsDoubleNear( mM, pt->m(), 1E-8 ) )
190  return false;
191  }
192 
193  return true;
194  }
195 
196  bool operator!=( const QgsAbstractGeometry &other ) const override
197  {
198  return !operator==( other );
199  }
200 
206  double x() const { return mX; }
207 
213  double y() const { return mY; }
214 
220  double z() const { return mZ; }
221 
227  double m() const { return mM; }
228 
236  double &rx() SIP_SKIP { clearCache(); return mX; }
237 
245  double &ry() SIP_SKIP { clearCache(); return mY; }
246 
254  double &rz() SIP_SKIP { clearCache(); return mZ; }
255 
263  double &rm() SIP_SKIP { clearCache(); return mM; }
264 
270  void setX( double x )
271  {
272  clearCache();
273  mX = x;
274  }
275 
281  void setY( double y )
282  {
283  clearCache();
284  mY = y;
285  }
286 
294  void setZ( double z )
295  {
296  if ( !is3D() )
297  return;
299  mZ = z;
300  }
301 
309  void setM( double m )
310  {
311  if ( !isMeasure() )
312  return;
313  clearCache();
314  mM = m;
315  }
316 
321  QPointF toQPointF() const
322  {
323  return QPointF( mX, mY );
324  }
325 
333  double distance( double x, double y ) const
334  {
335  return std::sqrt( ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) );
336  }
337 
344  double distance( const QgsPoint &other ) const
345  {
346  return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() ) );
347  }
348 
356  double distanceSquared( double x, double y ) const
357  {
358  return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
359  }
360 
368  double distanceSquared( const QgsPoint &other ) const
369  {
370  return ( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() );
371  }
372 
380  double distance3D( double x, double y, double z ) const;
381 
388  double distance3D( const QgsPoint &other ) const;
389 
397  double distanceSquared3D( double x, double y, double z ) const;
398 
406  double distanceSquared3D( const QgsPoint &other ) const;
407 
412  double azimuth( const QgsPoint &other ) const;
413 
419  double inclination( const QgsPoint &other ) const;
420 
451  QgsPoint project( double distance, double azimuth, double inclination = 90.0 ) const;
452 
457  QgsVector operator-( const QgsPoint &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
458 
463  QgsPoint &operator+=( QgsVector v ) { mX += v.x(); mY += v.y(); return *this; }
464 
469  QgsPoint &operator-=( QgsVector v ) { mX -= v.x(); mY -= v.y(); return *this; }
470 
475  QgsPoint operator+( QgsVector v ) const { QgsPoint r = *this; r.rx() += v.x(); r.ry() += v.y(); return r; }
476 
481  QgsPoint operator-( QgsVector v ) const { QgsPoint r = *this; r.rx() -= v.x(); r.ry() -= v.y(); return r; }
482 
483  //implementation of inherited methods
484  bool isEmpty() const override;
485  QgsRectangle boundingBox() const override;
486  QString geometryType() const override;
487  int dimension() const override;
488  QgsPoint *clone() const override SIP_FACTORY;
489  QgsPoint *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
490  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
491  void clear() override;
492  bool fromWkb( QgsConstWkbPtr &wkb ) override;
493  bool fromWkt( const QString &wkt ) override;
494  QByteArray asWkb( QgsAbstractGeometry::WkbFlags = nullptr ) const override;
495  QString asWkt( int precision = 17 ) const override;
496  QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
497  QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
498  json asJsonObject( int precision = 17 ) const override SIP_SKIP;
499  QString asKml( int precision = 17 ) const override;
500  void draw( QPainter &p ) const override;
501  void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform, bool transformZ = false ) override SIP_THROW( QgsCsException );
502  void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
503  QgsCoordinateSequence coordinateSequence() const override;
504  int nCoordinates() const override;
505  int vertexNumberFromVertexId( QgsVertexId id ) const override;
506  QgsAbstractGeometry *boundary() const override SIP_FACTORY;
507  bool isValid( QString &error SIP_OUT, int flags = 0 ) const override;
508 
509  //low-level editing
510  bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
511  bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
512  bool deleteVertex( QgsVertexId position ) override;
513 
514  double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt SIP_OUT, QgsVertexId &vertexAfter SIP_OUT, int *leftOf SIP_OUT = nullptr, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const override;
515  bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const override;
516  void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const override;
517 
522  double vertexAngle( QgsVertexId vertex ) const override;
523 
524  int vertexCount( int /*part*/ = 0, int /*ring*/ = 0 ) const override;
525  int ringCount( int /*part*/ = 0 ) const override;
526  int partCount() const override;
527  QgsPoint vertexAt( QgsVertexId /*id*/ ) const override;
528  QgsPoint *toCurveType() const override SIP_FACTORY;
529  double segmentLength( QgsVertexId startVertex ) const override;
530 
531  bool addZValue( double zValue = 0 ) override;
532  bool addMValue( double mValue = 0 ) override;
533  bool dropZValue() override;
534  bool dropMValue() override;
535  void swapXy() override;
536  bool convertTo( QgsWkbTypes::Type type ) override;
537 
538 #ifndef SIP_RUN
539 
540  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
541  void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) override;
542 
550  inline const QgsPoint *cast( const QgsAbstractGeometry *geom ) const
551  {
552  if ( geom && QgsWkbTypes::flatType( geom->wkbType() ) == QgsWkbTypes::Point )
553  return static_cast<const QgsPoint *>( geom );
554  return nullptr;
555  }
556 #endif
557 
558  QgsPoint *createEmptyWithSameType() const override SIP_FACTORY;
559 
560 #ifdef SIP_RUN
561  SIP_PYOBJECT __repr__();
562  % MethodCode
563  QString str = QStringLiteral( "<QgsPoint: %1>" ).arg( sipCpp->asWkt() );
564  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
565  % End
566 #endif
567 
568  protected:
569 
570  int childCount() const override;
571  QgsPoint childPoint( int index ) const override;
572 
573  private:
574  double mX;
575  double mY;
576  double mZ;
577  double mM;
578 };
579 
580 // clazy:excludeall=qstring-allocations
581 
582 #endif // QGSPOINT_H
QgsCoordinateSequence
QVector< QgsRingSequence > QgsCoordinateSequence
Definition: qgsabstractgeometry.h:49
QgsAbstractGeometry::wkbType
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Definition: qgsabstractgeometry.h:189
QgsAbstractGeometry::clearCache
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Definition: qgsabstractgeometry.cpp:112
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
qgsrectangle.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsAbstractGeometry::filterVertices
virtual void filterVertices(const std::function< bool(const QgsPoint &) > &filter)
Filters the vertices from the geometry in place, removing any which do not return true for the filter...
Definition: qgsabstractgeometry.cpp:250
QgsPoint::z
double z
Definition: qgspoint.h:60
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsWkbTypes::hasZ
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1042
closestSegment
double closestSegment(const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon)
Definition: qgstracer.cpp:68
QgsRectangle
Definition: qgsrectangle.h:41
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
MathUtils::leftOf
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const QgsPoint *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'. Negative values mean left ...
Definition: MathUtils.cpp:292
QgsPoint::y
double y
Definition: qgspoint.h:59
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsPoint::rx
double & rx()
Returns a reference to the x-coordinate of this point.
Definition: qgspoint.h:253
QgsAbstractGeometry::dimension
virtual int dimension() const =0
Returns the inherent dimension of the geometry.
QgsCsException
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:70
QgsAbstractGeometry::AxisOrder
AxisOrder
Axis order for GML generation.
Definition: qgsabstractgeometry.h:128
QgsVector::x
double x() const
Returns the vector's x-component.
Definition: qgsvector.cpp:76
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsConstWkbPtr
Definition: qgswkbptr.h:127
QgsAbstractGeometry::childPoint
virtual QgsPoint childPoint(int index) const
Returns point at index (for geometries without child geometries - i.e.
Definition: qgsabstractgeometry.cpp:292
QgsAbstractGeometry::clone
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
qgis_sip.h
QgsPoint::m
double m
Definition: qgspoint.h:61
QgsAbstractGeometry::is3D
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
Definition: qgsabstractgeometry.h:202
QgsAbstractGeometry::isEmpty
virtual bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgsabstractgeometry.cpp:298
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
QgsAbstractGeometry::geometryType
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
QgsPointXY
Definition: qgspointxy.h:43
QgsAbstractGeometry::boundingBox
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
QgsWkbTypes::hasM
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1092
QgsAbstractGeometry::createEmptyWithSameType
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
QgsVector::y
double y() const
Returns the vector's y-component.
Definition: qgsvector.cpp:81
QgsAbstractGeometry::operator!=
virtual bool operator!=(const QgsAbstractGeometry &other) const =0
operator-
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
Definition: qgsinterval.cpp:138
QgsVector
Definition: qgsvector.h:29
QgsAbstractGeometry::childCount
virtual int childCount() const
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Definition: qgsabstractgeometry.h:988
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1033
operator+
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
Definition: qgsinterval.cpp:144
QgsAbstractGeometry::transformVertices
virtual void transformVertices(const std::function< QgsPoint(const QgsPoint &) > &transform)
Transforms the vertices from the geometry in place, applying the transform function to every vertex.
Definition: qgsabstractgeometry.cpp:255
QgsAbstractGeometry::isMeasure
bool isMeasure() const
Returns true if the geometry contains m values.
Definition: qgsabstractgeometry.h:211
QgsAbstractGeometry::operator==
virtual bool operator==(const QgsAbstractGeometry &other) const =0
QgsWkbTypes
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
QgsPoint::ry
double & ry()
Returns a reference to the y-coordinate of this point.
Definition: qgspoint.h:262
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
qgsabstractgeometry.h
QgsPoint::x
double x
Definition: qgspoint.h:58
QgsWkbTypes::flatType
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:701