QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
73 #ifndef SIP_RUN
74  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 );
75 #else
76  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 )];
77  % MethodCode
78  double z;
79  double m;
80 
81  if ( a2 == Py_None )
82  {
83  z = std::numeric_limits<double>::quiet_NaN();
84  }
85  else
86  {
87  z = PyFloat_AsDouble( a2 );
88  }
89 
90  if ( a3 == Py_None )
91  {
92  m = std::numeric_limits<double>::quiet_NaN();
93  }
94  else
95  {
96  m = PyFloat_AsDouble( a3 );
97  }
98 
99  sipCpp = new sipQgsPoint( a0, a1, z, m, a4 );
100  % End
101 #endif
102 
106  explicit QgsPoint( const QgsPointXY &p );
107 
111  explicit QgsPoint( QPointF p );
112 
118  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;
119 
120  bool operator==( const QgsAbstractGeometry &other ) const override
121  {
122  const QgsPoint *pt = qgsgeometry_cast< const QgsPoint * >( &other );
123  if ( !pt )
124  return false;
125 
126  const QgsWkbTypes::Type type = wkbType();
127 
128  bool equal = pt->wkbType() == type;
129  equal &= qgsDoubleNear( pt->x(), mX, 1E-8 );
130  equal &= qgsDoubleNear( pt->y(), mY, 1E-8 );
131  if ( QgsWkbTypes::hasZ( type ) )
132  equal &= qgsDoubleNear( pt->z(), mZ, 1E-8 ) || ( std::isnan( pt->z() ) && std::isnan( mZ ) );
133  if ( QgsWkbTypes::hasM( type ) )
134  equal &= qgsDoubleNear( pt->m(), mM, 1E-8 ) || ( std::isnan( pt->m() ) && std::isnan( mM ) );
135 
136  return equal;
137  }
138 
139  bool operator!=( const QgsAbstractGeometry &other ) const override
140  {
141  return !operator==( other );
142  }
143 
149  double x() const { return mX; }
150 
156  double y() const { return mY; }
157 
163  double z() const { return mZ; }
164 
170  double m() const { return mM; }
171 
179  double &rx() SIP_SKIP { clearCache(); return mX; }
180 
188  double &ry() SIP_SKIP { clearCache(); return mY; }
189 
197  double &rz() SIP_SKIP { clearCache(); return mZ; }
198 
206  double &rm() SIP_SKIP { clearCache(); return mM; }
207 
213  void setX( double x )
214  {
215  clearCache();
216  mX = x;
217  }
218 
224  void setY( double y )
225  {
226  clearCache();
227  mY = y;
228  }
229 
237  void setZ( double z )
238  {
239  if ( !is3D() )
240  return;
241  clearCache();
242  mZ = z;
243  }
244 
252  void setM( double m )
253  {
254  if ( !isMeasure() )
255  return;
256  clearCache();
257  mM = m;
258  }
259 
264  QPointF toQPointF() const
265  {
266  return QPointF( mX, mY );
267  }
268 
276  double distance( double x, double y ) const
277  {
278  return std::sqrt( ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) );
279  }
280 
287  double distance( const QgsPoint &other ) const
288  {
289  return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() ) );
290  }
291 
299  double distanceSquared( double x, double y ) const
300  {
301  return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
302  }
303 
311  double distanceSquared( const QgsPoint &other ) const
312  {
313  return ( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() );
314  }
315 
323  double distance3D( double x, double y, double z ) const;
324 
331  double distance3D( const QgsPoint &other ) const;
332 
340  double distanceSquared3D( double x, double y, double z ) const;
341 
349  double distanceSquared3D( const QgsPoint &other ) const;
350 
355  double azimuth( const QgsPoint &other ) const;
356 
362  double inclination( const QgsPoint &other ) const;
363 
392  QgsPoint project( double distance, double azimuth, double inclination = 90.0 ) const;
393 
398  QgsVector operator-( const QgsPoint &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
399 
404  QgsPoint &operator+=( QgsVector v ) { mX += v.x(); mY += v.y(); return *this; }
405 
410  QgsPoint &operator-=( QgsVector v ) { mX -= v.x(); mY -= v.y(); return *this; }
411 
416  QgsPoint operator+( QgsVector v ) const { QgsPoint r = *this; r.rx() += v.x(); r.ry() += v.y(); return r; }
417 
422  QgsPoint operator-( QgsVector v ) const { QgsPoint r = *this; r.rx() -= v.x(); r.ry() -= v.y(); return r; }
423 
424  //implementation of inherited methods
425  bool isEmpty() const override;
426  QgsRectangle boundingBox() const override;
427  QString geometryType() const override;
428  int dimension() const override;
429  QgsPoint *clone() const override SIP_FACTORY;
430  QgsPoint *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
431  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
432  void clear() override;
433  bool fromWkb( QgsConstWkbPtr &wkb ) override;
434  bool fromWkt( const QString &wkt ) override;
435  QByteArray asWkb() const override;
436  QString asWkt( int precision = 17 ) const override;
437  QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
438  QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
439  json asJsonObject( int precision = 17 ) const override SIP_SKIP;
440  void draw( QPainter &p ) const override;
442  void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
443  QgsCoordinateSequence coordinateSequence() const override;
444  int nCoordinates() const override;
445  int vertexNumberFromVertexId( QgsVertexId id ) const override;
446  QgsAbstractGeometry *boundary() const override SIP_FACTORY;
447  bool isValid( QString &error SIP_OUT, int flags = 0 ) const override;
448 
449  //low-level editing
450  bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
451  bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
452  bool deleteVertex( QgsVertexId position ) override;
453 
454  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;
455  bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const override;
456  void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const override;
457 
462  double vertexAngle( QgsVertexId vertex ) const override;
463 
464  int vertexCount( int /*part*/ = 0, int /*ring*/ = 0 ) const override;
465  int ringCount( int /*part*/ = 0 ) const override;
466  int partCount() const override;
467  QgsPoint vertexAt( QgsVertexId /*id*/ ) const override;
468  QgsPoint *toCurveType() const override SIP_FACTORY;
469  double segmentLength( QgsVertexId startVertex ) const override;
470 
471  bool addZValue( double zValue = 0 ) override;
472  bool addMValue( double mValue = 0 ) override;
473  bool dropZValue() override;
474  bool dropMValue() override;
475  void swapXy() override;
476  bool convertTo( QgsWkbTypes::Type type ) override;
477 
478 #ifndef SIP_RUN
479 
480  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
481  void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) override;
482 
490  inline const QgsPoint *cast( const QgsAbstractGeometry *geom ) const
491  {
492  if ( geom && QgsWkbTypes::flatType( geom->wkbType() ) == QgsWkbTypes::Point )
493  return static_cast<const QgsPoint *>( geom );
494  return nullptr;
495  }
496 #endif
497 
498  QgsPoint *createEmptyWithSameType() const override SIP_FACTORY;
499 
500 #ifdef SIP_RUN
501  SIP_PYOBJECT __repr__();
502  % MethodCode
503  QString str = QStringLiteral( "<QgsPoint: %1>" ).arg( sipCpp->asWkt() );
504  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
505  % End
506 #endif
507 
508  protected:
509 
510  int childCount() const override;
511  QgsPoint childPoint( int index ) const override;
512 
513  private:
514  double mX;
515  double mY;
516  double mZ;
517  double mM;
518 };
519 
520 // clazy:excludeall=qstring-allocations
521 
522 #endif // QGSPOINT_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.
int precision
A rectangle specified with double values.
Definition: qgsrectangle.h:41
virtual QgsAbstractGeometry * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0) const =0
Makes a new geometry with all the points or vertices snapped to the closest point of the grid...
virtual int vertexNumberFromVertexId(QgsVertexId id) const =0
Returns the vertex number corresponding to a vertex id.
double y
Definition: qgspoint.h:42
virtual int childCount() const
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
virtual bool isEmpty() const
Returns true if the geometry is empty.
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...
virtual bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within the geometry.
virtual bool isValid(QString &error, int flags=0) const =0
Checks validity of the geometry, and returns true if the geometry is valid.
virtual void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)=0
Transforms the geometry using a coordinate transform.
virtual QgsAbstractGeometry * toCurveType() const =0
Returns the geometry converted to the more generic curve type.
QgsVector operator-(const QgsPoint &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspoint.h:398
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:197
QPointF toQPointF() const
Returns the point as a QPointF.
Definition: qgspoint.h:264
void setZ(double z)
Sets the point&#39;s z-coordinate.
Definition: qgspoint.h:237
virtual bool insertVertex(QgsVertexId position, const QgsPoint &vertex)=0
Inserts a vertex into the geometry.
virtual QgsPoint childPoint(int index) const
Returns point at index (for geometries without child geometries - i.e.
QVector< QgsRingSequence > QgsCoordinateSequence
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspoint.h:276
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:265
virtual bool operator==(const QgsAbstractGeometry &other) const =0
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
virtual double vertexAngle(QgsVertexId vertex) const =0
Returns approximate angle at a vertex.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
bool operator!=(const QgsAbstractGeometry &other) const override
Definition: qgspoint.h:139
virtual QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML2 representation of the geometry.
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 QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:771
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspoint.h:156
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsPoint & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspoint.h:410
void setM(double m)
Sets the point&#39;s m-value.
Definition: qgspoint.h:252
virtual double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf=nullptr, double epsilon=4 *std::numeric_limits< double >::epsilon()) const =0
Searches for the closest segment of the geometry to a given point.
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
virtual double segmentLength(QgsVertexId startVertex) const =0
Returns the length of the segment of the geometry which begins at startVertex.
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...
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:416
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
double distance(const QgsPoint &other) const
Returns the 2D distance between this point and another point.
Definition: qgspoint.h:287
virtual int ringCount(int part=0) const =0
Returns the number of rings of which this geometry is built.
#define SIP_FACTORY
Definition: qgis_sip.h:69
double & rx()
Returns a reference to the x-coordinate of this point.
Definition: qgspoint.h:179
Abstract base class for all geometries.
virtual int dimension() const =0
Returns the inherent dimension of the geometry.
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:37
AxisOrder
Axis order for GML generation.
nlohmann::json json
Definition: qgsjsonutils.h:27
A class to represent a vector.
Definition: qgsvector.h:29
void setX(double x)
Sets the point&#39;s x-coordinate.
Definition: qgspoint.h:213
void setY(double y)
Sets the point&#39;s y-coordinate.
Definition: qgspoint.h:224
double m() const
Returns the point&#39;s m value.
Definition: qgspoint.h:170
virtual QgsCoordinateSequence coordinateSequence() const =0
Retrieves the sequence of geometries, rings and nodes.
virtual void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const =0
Returns the vertices adjacent to a specified vertex within a geometry.
double & rm()
Returns a reference to the m value of this point.
Definition: qgspoint.h:206
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:490
#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 int vertexCount(int part=0, int ring=0) const =0
Returns the number of vertices of which this geometry is built.
Class for doing transforms between two map coordinate systems.
#define SIP_THROW(name)
Definition: qgis_sip.h:177
virtual bool convertTo(QgsWkbTypes::Type type)
Converts the geometry to a specified type.
double z
Definition: qgspoint.h:43
double z() const
Returns the point&#39;s z-coordinate.
Definition: qgspoint.h:163
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:821
QgsPoint operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspoint.h:422
Transform from source to destination CRS.
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:76
virtual bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false)=0
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
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.
double distanceSquared(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspoint.h:299
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:430
virtual void swapXy()=0
Swaps the x and y coordinates from the geometry.
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:81
virtual json asJsonObject(int precision=17) const
Returns a json object representation of the geometry.
double & ry()
Returns a reference to the y-coordinate of this point.
Definition: qgspoint.h:188
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const 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.cpp:292
double distanceSquared(const QgsPoint &other) const
Returns the squared distance between this point another point.
Definition: qgspoint.h:311
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
double x() const
Returns the point&#39;s x-coordinate.
Definition: qgspoint.h:149
QgsPoint & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspoint.h:404
virtual int partCount() const =0
Returns count of parts contained in the geometry.
double m
Definition: qgspoint.h:44
virtual QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML3 representation of the geometry.
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
virtual bool fromWkb(QgsConstWkbPtr &wkb)=0
Sets the geometry from a WKB string.
double x
Definition: qgspoint.h:41