QGIS API Documentation  3.0.2-Girona (307d082)
qgsabstractgeometry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsabstractgeometry.h
3  -------------------------------------------------------------------
4 Date : 04 Sept 2014
5 Copyright : (C) 2014 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSABSTRACTGEOMETRYV2
17 #define QGSABSTRACTGEOMETRYV2
18 
19 #include <QString>
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgscoordinatetransform.h"
24 #include "qgswkbtypes.h"
25 #include "qgswkbptr.h"
26 
27 class QgsMapToPixel;
28 class QgsCurve;
29 class QgsMultiCurve;
30 class QgsMultiPoint;
31 class QgsPoint;
32 struct QgsVertexId;
33 class QgsVertexIterator;
34 class QPainter;
35 class QDomDocument;
36 class QDomElement;
37 
38 typedef QVector< QgsPoint > QgsPointSequence;
39 #ifndef SIP_RUN
40 typedef QVector< QgsPointSequence > QgsRingSequence;
41 typedef QVector< QgsRingSequence > QgsCoordinateSequence;
42 #else
43 typedef QVector< QVector< QgsPoint > > QgsRingSequence;
44 typedef QVector< QVector< QVector< QgsPoint > > > QgsCoordinateSequence;
45 #endif
46 
53 class CORE_EXPORT QgsAbstractGeometry
54 {
55  Q_GADGET
56 
57 #ifdef SIP_RUN
59  if ( qgsgeometry_cast<QgsPoint *>( sipCpp ) != nullptr )
60  sipType = sipType_QgsPoint;
61  else if ( qgsgeometry_cast<QgsLineString *>( sipCpp ) != nullptr )
62  sipType = sipType_QgsLineString;
63  else if ( qgsgeometry_cast<QgsCircularString *>( sipCpp ) != nullptr )
64  sipType = sipType_QgsCircularString;
65  else if ( qgsgeometry_cast<QgsCompoundCurve *>( sipCpp ) != nullptr )
66  sipType = sipType_QgsCompoundCurve;
67  else if ( qgsgeometry_cast<QgsTriangle *>( sipCpp ) != nullptr )
68  sipType = sipType_QgsTriangle;
69  else if ( qgsgeometry_cast<QgsPolygon *>( sipCpp ) != nullptr )
70  sipType = sipType_QgsPolygon;
71  else if ( qgsgeometry_cast<QgsCurvePolygon *>( sipCpp ) != nullptr )
72  sipType = sipType_QgsCurvePolygon;
73  else if ( qgsgeometry_cast<QgsMultiPoint *>( sipCpp ) != nullptr )
74  sipType = sipType_QgsMultiPoint;
75  else if ( qgsgeometry_cast<QgsMultiLineString *>( sipCpp ) != nullptr )
76  sipType = sipType_QgsMultiLineString;
77  else if ( qgsgeometry_cast<QgsMultiPolygon *>( sipCpp ) != nullptr )
78  sipType = sipType_QgsMultiPolygon;
79  else if ( qgsgeometry_cast<QgsMultiSurface *>( sipCpp ) != nullptr )
80  sipType = sipType_QgsMultiSurface;
81  else if ( qgsgeometry_cast<QgsMultiCurve *>( sipCpp ) != nullptr )
82  sipType = sipType_QgsMultiCurve;
83  else if ( qgsgeometry_cast<QgsGeometryCollection *>( sipCpp ) != nullptr )
84  sipType = sipType_QgsGeometryCollection;
85  else
86  sipType = 0;
87  SIP_END
88 #endif
89  public:
90 
93  {
94 
98  MaximumAngle = 0,
99 
103  MaximumDifference
104  };
105  Q_ENUM( SegmentationToleranceType )
106 
107 
110  QgsAbstractGeometry() = default;
111  virtual ~QgsAbstractGeometry() = default;
113  QgsAbstractGeometry &operator=( const QgsAbstractGeometry &geom );
114 
115  virtual bool operator==( const QgsAbstractGeometry &other ) const = 0;
116  virtual bool operator!=( const QgsAbstractGeometry &other ) const = 0;
117 
121  virtual QgsAbstractGeometry *clone() const = 0 SIP_FACTORY;
122 
126  virtual void clear() = 0;
127 
131  virtual QgsRectangle boundingBox() const = 0;
132 
133  //mm-sql interface
134 
139  virtual int dimension() const = 0;
140 
146  virtual QString geometryType() const = 0;
147 
153  inline QgsWkbTypes::Type wkbType() const { return mWkbType; }
154 
160  QString wktTypeStr() const;
161 
166  bool is3D() const;
167 
172  bool isMeasure() const;
173 
180  virtual QgsAbstractGeometry *boundary() const = 0 SIP_FACTORY;
181 
182  //import
183 
189  virtual bool fromWkb( QgsConstWkbPtr &wkb ) = 0;
190 
195  virtual bool fromWkt( const QString &wkt ) = 0;
196 
197  //export
198 
207  virtual QByteArray asWkb() const = 0;
208 
217  virtual QString asWkt( int precision = 17 ) const = 0;
218 
229  virtual QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml" ) const = 0;
230 
241  virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml" ) const = 0;
242 
251  virtual QString asJson( int precision = 17 ) const = 0;
252 
253  //render pipeline
254 
265  virtual void transform( const QgsCoordinateTransform &ct,
267  bool transformZ = false ) = 0;
268 
275  virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0,
276  double mTranslate = 0.0, double mScale = 1.0 ) = 0;
277 
282  virtual void draw( QPainter &p ) const = 0;
283 
294  virtual int vertexNumberFromVertexId( QgsVertexId id ) const = 0;
295 
303  virtual bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const = 0;
304 
309  virtual void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const = 0;
310 
315  virtual QgsCoordinateSequence coordinateSequence() const = 0;
316 
320  virtual int nCoordinates() const;
321 
325  virtual QgsPoint vertexAt( QgsVertexId id ) const = 0;
326 
339  virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt SIP_OUT,
340  QgsVertexId &vertexAfter SIP_OUT,
341  int *leftOf SIP_OUT = nullptr, double epsilon = 4 * DBL_EPSILON ) const = 0;
342 
343  //low-level editing
344 
353  virtual bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) = 0;
354 
363  virtual bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) = 0;
364 
372  virtual bool deleteVertex( QgsVertexId position ) = 0;
373 
379  virtual double length() const;
380 
386  virtual double perimeter() const;
387 
393  virtual double area() const;
394 
399  virtual double segmentLength( QgsVertexId startVertex ) const = 0;
400 
402  virtual QgsPoint centroid() const;
403 
407  virtual bool isEmpty() const;
408 
412  virtual bool hasCurvedSegments() const;
413 
420  virtual QgsAbstractGeometry *segmentize( double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const SIP_FACTORY;
421 
428  virtual QgsAbstractGeometry *toCurveType() const = 0 SIP_FACTORY;
429 
449  virtual QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const = 0 SIP_FACTORY;
450 
471  virtual bool removeDuplicateNodes( double epsilon = 4 * DBL_EPSILON, bool useZValues = false ) = 0;
472 
480  virtual double vertexAngle( QgsVertexId vertex ) const = 0;
481 
485  virtual int vertexCount( int part = 0, int ring = 0 ) const = 0;
486 
490  virtual int ringCount( int part = 0 ) const = 0;
491 
497  virtual int partCount() const = 0;
498 
507  virtual bool addZValue( double zValue = 0 ) = 0;
508 
517  virtual bool addMValue( double mValue = 0 ) = 0;
518 
526  virtual bool dropZValue() = 0;
527 
535  virtual bool dropMValue() = 0;
536 
542  virtual bool convertTo( QgsWkbTypes::Type type );
543 
544 #ifndef SIP_RUN
545 
551  class CORE_EXPORT vertex_iterator
552  {
553  private:
554 
560  struct Level
561  {
562  const QgsAbstractGeometry *g = nullptr;
563  int index = 0;
564  };
565 
566  Level levels[3];
567  int depth = -1;
568 
569  void digDown();
570 
571  public:
573  vertex_iterator() = default;
574 
576  vertex_iterator( const QgsAbstractGeometry *g, int index );
577 
582  vertex_iterator &operator++();
583 
585  vertex_iterator operator++( int );
586 
588  QgsPoint operator*() const;
589 
591  QgsVertexId vertexId() const;
592 
593  bool operator==( const vertex_iterator &other ) const;
594  bool operator!=( const vertex_iterator &other ) const { return !( *this == other ); }
595  };
596 
602  {
603  return vertex_iterator( this, 0 );
604  }
605 
611  {
612  return vertex_iterator( this, childCount() );
613  }
614 #endif
615 
620  QgsVertexIterator vertices() const;
621 
629  virtual QgsAbstractGeometry *createEmptyWithSameType() const = 0 SIP_FACTORY;
630 
631  protected:
632 
638  virtual bool hasChildGeometries() const;
639 
645  virtual int childCount() const { return 0; }
646 
652  virtual QgsAbstractGeometry *childGeometry( int index ) const { Q_UNUSED( index ); return nullptr; }
653 
659  virtual QgsPoint childPoint( int index ) const;
660 
661  protected:
663 
667  void setZMTypeFromSubGeometry( const QgsAbstractGeometry *subggeom, QgsWkbTypes::Type baseGeomType );
668 
673  virtual QgsRectangle calculateBoundingBox() const;
674 
678  virtual void clearCache() const;
679 
680  friend class TestQgsGeometry;
681 };
682 
683 
690 struct CORE_EXPORT QgsVertexId
691 {
693  {
694  SegmentVertex = 1, //start / endpoint of a segment
695  CurveVertex
696  };
697 
698  explicit QgsVertexId( int _part = -1, int _ring = -1, int _vertex = -1, VertexType _type = SegmentVertex )
699  : part( _part )
700  , ring( _ring )
701  , vertex( _vertex )
702  , type( _type )
703  {}
704 
708  bool isValid() const { return part >= 0 && ring >= 0 && vertex >= 0; }
709 
710  bool operator==( QgsVertexId other ) const
711  {
712  return part == other.part && ring == other.ring && vertex == other.vertex;
713  }
714  bool operator!=( QgsVertexId other ) const
715  {
716  return part != other.part || ring != other.ring || vertex != other.vertex;
717  }
718  bool partEqual( QgsVertexId o ) const
719  {
720  return part >= 0 && o.part == part;
721  }
722  bool ringEqual( QgsVertexId o ) const
723  {
724  return partEqual( o ) && ( ring >= 0 && o.ring == ring );
725  }
726  bool vertexEqual( QgsVertexId o ) const
727  {
728  return ringEqual( o ) && ( vertex >= 0 && o.ring == ring );
729  }
730  bool isValid( const QgsAbstractGeometry *geom ) const
731  {
732  return ( part >= 0 && part < geom->partCount() ) &&
733  ( ring < geom->ringCount( part ) ) &&
734  ( vertex < 0 || vertex < geom->vertexCount( part, ring ) );
735  }
736 
737  int part;
738  int ring;
739  int vertex;
741 };
742 
743 #ifndef SIP_RUN
744 
745 template <class T>
746 inline T qgsgeometry_cast( const QgsAbstractGeometry *geom )
747 {
748  return const_cast<T>( reinterpret_cast<T>( 0 )->cast( geom ) );
749 }
750 
751 #endif
752 
753 // clazy:excludeall=qstring-allocations
754 
760 class CORE_EXPORT QgsVertexIterator
761 {
762  public:
764  QgsVertexIterator() = default;
765 
768  : g( geometry )
769  , i( g->vertices_begin() )
770  , n( g->vertices_end() )
771  {
772  }
773 
775  bool hasNext() const
776  {
777  return g && g->vertices_end() != i;
778  }
779 
781  QgsPoint next();
782 
783 #ifdef SIP_RUN
784  QgsVertexIterator *__iter__();
785  % MethodCode
786  sipRes = sipCpp;
787  % End
788 
789  SIP_PYOBJECT __next__();
790  % MethodCode
791  if ( sipCpp->hasNext() )
792  sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None );
793  else
794  PyErr_SetString( PyExc_StopIteration, "" );
795  % End
796 #endif
797 
798  private:
799  const QgsAbstractGeometry *g = nullptr;
801 
802 };
803 
804 #endif //QGSABSTRACTGEOMETRYV2
A rectangle specified with double values.
Definition: qgsrectangle.h:39
QgsVertexId(int _part=-1, int _ring=-1, int _vertex=-1, VertexType _type=SegmentVertex)
virtual int childCount() const
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Java-style iterator for traversal of vertices of a geometry.
Multi point geometry collection.
Definition: qgsmultipoint.h:29
bool isValid() const
Returns true if the vertex id is valid.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QVector< QgsRingSequence > QgsCoordinateSequence
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
virtual QgsAbstractGeometry * childGeometry(int index) const
Returns pointer to child geometry (for geometries with child geometries - i.e.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
bool operator!=(const vertex_iterator &other) const
bool vertexEqual(QgsVertexId o) const
bool operator!=(QgsVertexId other) const
QgsVertexIterator(const QgsAbstractGeometry *geometry)
Constructs iterator for the given geometry.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:36
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
Utility class for identifying a unique vertex within a geometry.
vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry...
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
#define SIP_END
Definition: qgis_sip.h:175
Multi curve geometry collection.
Definition: qgsmulticurve.h:29
virtual int ringCount(int part=0) const =0
Returns the number of rings of which this geometry is built.
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
#define SIP_FACTORY
Definition: qgis_sip.h:69
Abstract base class for all geometries.
The vertex_iterator class provides STL-style iterator for vertices.
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
double closestSegment(const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon)
Definition: qgstracer.cpp:65
QVector< QgsPoint > QgsPointSequence
bool operator==(QgsVertexId other) const
QVector< QgsPointSequence > QgsRingSequence
bool partEqual(QgsVertexId o) const
bool ringEqual(QgsVertexId o) const
#define SIP_OUT
Definition: qgis_sip.h:51
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
Class for doing transforms between two map coordinate systems.
Transform from source to destination CRS.
QgsMargins operator*(const QgsMargins &margins, double factor)
Returns a QgsMargins object that is formed by multiplying each component of the given margins by fact...
Definition: qgsmargins.h:242
vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
bool hasNext() const
Find out whether there are more vertices.
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
bool isValid(const QgsAbstractGeometry *geom) const