QGIS API Documentation  2.99.0-Master (7d4f81d)
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 QGSPOINTV2_H
19 #define QGSPOINTV2_H
20 
21 #include "qgis_core.h"
22 #include "qgis.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 
36 class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
37 {
38  Q_GADGET
39 
40  Q_PROPERTY( double x READ x WRITE setX )
41  Q_PROPERTY( double y READ y WRITE setY )
42  Q_PROPERTY( double z READ z WRITE setZ )
43  Q_PROPERTY( double m READ m WRITE setM )
44 
45  public:
46 
72 #ifndef SIP_RUN
73  QgsPoint( double x = 0.0, double y = 0.0, double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN(), QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown );
74 #else
75  QgsPoint( double x = 0.0, double y = 0.0, SIP_PYOBJECT z = Py_None, SIP_PYOBJECT m = Py_None, QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown ) [( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0, QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown )];
76  % MethodCode
77  double z;
78  double m;
79 
80  if ( a2 == Py_None )
81  {
82  z = std::numeric_limits<double>::quiet_NaN();
83  }
84  else
85  {
86  z = PyFloat_AsDouble( a2 );
87  }
88 
89  if ( a3 == Py_None )
90  {
91  m = std::numeric_limits<double>::quiet_NaN();
92  }
93  else
94  {
95  m = PyFloat_AsDouble( a3 );
96  }
97 
98  sipCpp = new sipQgsPoint( a0, a1, z, m, a4 );
99  % End
100 #endif
101 
104  explicit QgsPoint( const QgsPointXY &p );
105 
108  explicit QgsPoint( QPointF p );
109 
115  explicit QgsPoint( QgsWkbTypes::Type wkbType, double x = 0.0, double y = 0.0, double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN() ) SIP_SKIP;
116 
117  bool operator==( const QgsPoint &pt ) const;
118  bool operator!=( const QgsPoint &pt ) const;
119 
124  double x() const { return mX; }
125 
130  double y() const { return mY; }
131 
136  double z() const { return mZ; }
137 
142  double m() const { return mM; }
143 
150  double &rx() SIP_SKIP { clearCache(); return mX; }
151 
158  double &ry() SIP_SKIP { clearCache(); return mY; }
159 
166  double &rz() SIP_SKIP { clearCache(); return mZ; }
167 
174  double &rm() SIP_SKIP { clearCache(); return mM; }
175 
180  void setX( double x )
181  {
182  clearCache();
183  mX = x;
184  }
185 
190  void setY( double y )
191  {
192  clearCache();
193  mY = y;
194  }
195 
202  void setZ( double z )
203  {
204  if ( !is3D() )
205  return;
206  clearCache();
207  mZ = z;
208  }
209 
216  void setM( double m )
217  {
218  if ( !isMeasure() )
219  return;
220  clearCache();
221  mM = m;
222  }
223 
227  QPointF toQPointF() const;
228 
236  double distance( double x, double y ) const;
237 
244  double distance( const QgsPoint &other ) const;
245 
253  double distanceSquared( double x, double y ) const;
254 
262  double distanceSquared( const QgsPoint &other ) const;
263 
271  double distance3D( double x, double y, double z ) const;
272 
279  double distance3D( const QgsPoint &other ) const;
280 
288  double distanceSquared3D( double x, double y, double z ) const;
289 
297  double distanceSquared3D( const QgsPoint &other ) const;
298 
303  double azimuth( const QgsPoint &other ) const;
304 
310  double inclination( const QgsPoint &other ) const;
311 
339  QgsPoint project( double distance, double azimuth, double inclination = 90.0 ) const;
340 
345  QgsVector operator-( const QgsPoint &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
346 
351  QgsPoint &operator+=( QgsVector v ) { mX += v.x(); mY += v.y(); return *this; }
352 
357  QgsPoint &operator-=( QgsVector v ) { mX -= v.x(); mY -= v.y(); return *this; }
358 
363  QgsPoint operator+( QgsVector v ) const { QgsPoint r = *this; r.rx() += v.x(); r.ry() += v.y(); return r; }
364 
369  QgsPoint operator-( QgsVector v ) const { QgsPoint r = *this; r.rx() -= v.x(); r.ry() -= v.y(); return r; }
370 
371  //implementation of inherited methods
372  bool isEmpty() const override { return false; }
373  virtual QgsRectangle boundingBox() const override { return QgsRectangle( mX, mY, mX, mY ); }
374  virtual QString geometryType() const override { return QStringLiteral( "Point" ); }
375  virtual int dimension() const override { return 0; }
376  virtual QgsPoint *clone() const override SIP_FACTORY;
377  void clear() override;
378  virtual bool fromWkb( QgsConstWkbPtr &wkb ) override;
379  virtual bool fromWkt( const QString &wkt ) override;
380  QByteArray asWkb() const override;
381  QString asWkt( int precision = 17 ) const override;
382  QDomElement asGML2( QDomDocument &doc, int precision = 17, const QString &ns = "gml" ) const override;
383  QDomElement asGML3( QDomDocument &doc, int precision = 17, const QString &ns = "gml" ) const override;
384  QString asJSON( int precision = 17 ) const override;
385  void draw( QPainter &p ) const override;
387  bool transformZ = false ) override;
388  void transform( const QTransform &t ) override;
389  virtual QgsCoordinateSequence coordinateSequence() const override;
390  virtual int nCoordinates() const override { return 1; }
391  virtual QgsAbstractGeometry *boundary() const override SIP_FACTORY;
392 
393  //low-level editing
394  virtual bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override { Q_UNUSED( position ); Q_UNUSED( vertex ); return false; }
395  virtual bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
396  virtual bool deleteVertex( QgsVertexId position ) override { Q_UNUSED( position ); return false; }
397 
398  virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt SIP_OUT,
399  QgsVertexId &vertexAfter SIP_OUT, bool *leftOf SIP_OUT,
400  double epsilon ) const override;
401  bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const override;
402 
406  double vertexAngle( QgsVertexId vertex ) const override { Q_UNUSED( vertex ); return 0.0; }
407 
408  virtual int vertexCount( int /*part*/ = 0, int /*ring*/ = 0 ) const override { return 1; }
409  virtual int ringCount( int /*part*/ = 0 ) const override { return 1; }
410  virtual int partCount() const override { return 1; }
411  virtual QgsPoint vertexAt( QgsVertexId /*id*/ ) const override { return *this; }
412 
413  virtual bool addZValue( double zValue = 0 ) override;
414  virtual bool addMValue( double mValue = 0 ) override;
415  virtual bool dropZValue() override;
416  virtual bool dropMValue() override;
417  bool convertTo( QgsWkbTypes::Type type ) override;
418 
419 #ifndef SIP_RUN
420 
428  inline const QgsPoint *cast( const QgsAbstractGeometry *geom ) const
429  {
430  if ( geom && QgsWkbTypes::flatType( geom->wkbType() ) == QgsWkbTypes::Point )
431  return static_cast<const QgsPoint *>( geom );
432  return nullptr;
433  }
434 #endif
435  private:
436  double mX;
437  double mY;
438  double mZ;
439  double mM;
440 };
441 
442 #endif // QGSPOINTV2_H
bool isMeasure() const
Returns true if the geometry contains m values.
virtual QString asWkt(int precision=17) const =0
Returns a WKT representation of the geometry.
A rectangle specified with double values.
Definition: qgsrectangle.h:38
virtual int dimension() const override
Returns the inherent dimension of the geometry.
Definition: qgspoint.h:375
QgsVector operator-(const QgsPoint &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspoint.h:345
virtual QByteArray asWkb() const =0
Returns a WKB representation of the geometry.
double & rz()
Returns a reference to the z-coordinate of this point.
Definition: qgspoint.h:166
virtual int vertexCount(int=0, int=0) const override
Returns the number of vertexes of which this geometry is built.
Definition: qgspoint.h:408
void setZ(double z)
Sets the point&#39;s z-coordinate.
Definition: qgspoint.h:202
double ANALYSIS_EXPORT leftOf(QgsPoint *thepoint, QgsPoint *p1, QgsPoint *p2)
Returns whether &#39;thepoint&#39; is left or right of the line from &#39;p1&#39; to &#39;p2&#39;. Negativ values mean left a...
Definition: MathUtils.cc:310
double vertexAngle(QgsVertexId vertex) const override
Angle undefined.
Definition: qgspoint.h:406
virtual QgsPoint vertexAt(QgsVertexId) const override
Returns the point corresponding to a specified vertex id.
Definition: qgspoint.h:411
A class to represent a 2D point.
Definition: qgspointxy.h:42
virtual double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, bool *leftOf, double epsilon) const =0
Searches for the closest segment of the geometry to a given point.
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
virtual QgsAbstractGeometry * boundary() const =0
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
virtual void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false)=0
Transforms the geometry using a coordinate transform.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
virtual QString geometryType() const override
Returns a unique string representing the geometry type.
Definition: qgspoint.h:374
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspoint.h:130
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:66
QgsPoint & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspoint.h:357
void setM(double m)
Sets the point&#39;s m-value.
Definition: qgspoint.h:216
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Utility class for identifying a unique vertex within a geometry.
#define SIP_SKIP
Definition: qgis_sip.h:119
QgsPoint operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspoint.h:363
virtual bool insertVertex(QgsVertexId position, const QgsPoint &vertex) override
Inserts a vertex into the geometry.
Definition: qgspoint.h:394
virtual int ringCount(int=0) const override
Returns the number of rings of which this geometry is built.
Definition: qgspoint.h:409
virtual QgsRectangle boundingBox() const override
Returns the minimal bounding box for the geometry.
Definition: qgspoint.h:373
#define SIP_FACTORY
Definition: qgis_sip.h:69
double & rx()
Returns a reference to the x-coordinate of this point.
Definition: qgspoint.h:150
virtual int nCoordinates() const override
Returns the number of nodes contained in the geometry.
Definition: qgspoint.h:390
Abstract base class for all geometries.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:36
A class to represent a vector.
Definition: qgsvector.h:26
void setX(double x)
Sets the point&#39;s x-coordinate.
Definition: qgspoint.h:180
void setY(double y)
Sets the point&#39;s y-coordinate.
Definition: qgspoint.h:190
virtual int partCount() const override
Returns count of parts contained in the geometry.
Definition: qgspoint.h:410
double m() const
Returns the point&#39;s m value.
Definition: qgspoint.h:142
virtual QgsCoordinateSequence coordinateSequence() const =0
Retrieves the sequence of geometries, rings and nodes.
double & rm()
Returns a reference to the m value of this point.
Definition: qgspoint.h:174
bool isEmpty() const override
Returns true if the geometry is empty.
Definition: qgspoint.h:372
virtual void clear()=0
Clears the geometry, ie reset it to a null geometry.
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
const QgsPoint * cast(const QgsAbstractGeometry *geom) const
Cast the geom to a QgsPoint.
Definition: qgspoint.h:428
#define SIP_OUT
Definition: qgis_sip.h:51
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
virtual QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const =0
Returns a GML3 representation of the geometry.
virtual bool deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
Definition: qgspoint.h:396
Class for doing transforms between two map coordinate systems.
virtual bool convertTo(QgsWkbTypes::Type type)
Converts the geometry to a specified type.
double z() const
Returns the point&#39;s z-coordinate.
Definition: qgspoint.h:136
QgsPoint operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspoint.h:369
Transform from source to destination CRS.
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:82
QList< QgsRingSequence > QgsCoordinateSequence
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
virtual bool fromWkt(const QString &wkt)=0
Sets the geometry from a WKT string.
virtual QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const =0
Returns a GML2 representation of the geometry.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:423
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:87
double & ry()
Returns a reference to the y-coordinate of this point.
Definition: qgspoint.h:158
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
virtual QString asJSON(int precision=17) const =0
Returns a GeoJSON representation of the geometry.
QgsPoint & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspoint.h:351
virtual bool fromWkb(QgsConstWkbPtr &wkb)=0
Sets the geometry from a WKB string.