QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsabstractgeometry.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsabstractgeometry.h
3 -------------------------------------------------------------------
4Date : 04 Sept 2014
5Copyright : (C) 2014 by Marco Hugentobler
6email : 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 <array>
20#include <functional>
21#include <type_traits>
22#include <QString>
23
24#include "qgis_core.h"
25#include "qgis.h"
26#include "qgswkbtypes.h"
27#include "qgswkbptr.h"
28
29#ifndef SIP_RUN
30#include "json_fwd.hpp"
31using namespace nlohmann;
32#endif
33
34class QgsMapToPixel;
35class QgsCurve;
36class QgsMultiCurve;
37class QgsMultiPoint;
38
39struct QgsVertexId;
41class QPainter;
42class QDomDocument;
43class QDomElement;
46class QgsConstWkbPtr;
47class QPainterPath;
49class QgsFeedback;
51class QgsPoint;
52class QgsRectangle;
53class QgsBox3D;
54
55typedef QVector< QgsPoint > QgsPointSequence;
56#ifndef SIP_RUN
57typedef QVector< QgsPointSequence > QgsRingSequence;
58typedef QVector< QgsRingSequence > QgsCoordinateSequence;
59#else
60typedef QVector< QVector< QgsPoint > > QgsRingSequence;
61typedef QVector< QVector< QVector< QgsPoint > > > QgsCoordinateSequence;
62#endif
63
64
80class CORE_EXPORT QgsAbstractGeometry
81{
82
83#ifdef SIP_RUN
85 if ( qgsgeometry_cast<QgsPoint *>( sipCpp ) != nullptr )
86 sipType = sipType_QgsPoint;
87 else if ( qgsgeometry_cast<QgsLineString *>( sipCpp ) != nullptr )
88 sipType = sipType_QgsLineString;
89 else if ( qgsgeometry_cast<QgsCircularString *>( sipCpp ) != nullptr )
90 sipType = sipType_QgsCircularString;
91 else if ( qgsgeometry_cast<QgsCompoundCurve *>( sipCpp ) != nullptr )
92 sipType = sipType_QgsCompoundCurve;
93 else if ( qgsgeometry_cast<QgsTriangle *>( sipCpp ) != nullptr )
94 sipType = sipType_QgsTriangle;
95 else if ( qgsgeometry_cast<QgsPolygon *>( sipCpp ) != nullptr )
96 sipType = sipType_QgsPolygon;
97 else if ( qgsgeometry_cast<QgsCurvePolygon *>( sipCpp ) != nullptr )
98 sipType = sipType_QgsCurvePolygon;
99 else if ( qgsgeometry_cast<QgsMultiPoint *>( sipCpp ) != nullptr )
100 sipType = sipType_QgsMultiPoint;
101 else if ( qgsgeometry_cast<QgsMultiLineString *>( sipCpp ) != nullptr )
102 sipType = sipType_QgsMultiLineString;
103 else if ( qgsgeometry_cast<QgsMultiPolygon *>( sipCpp ) != nullptr )
104 sipType = sipType_QgsMultiPolygon;
105 else if ( qgsgeometry_cast<QgsMultiSurface *>( sipCpp ) != nullptr )
106 sipType = sipType_QgsMultiSurface;
107 else if ( qgsgeometry_cast<QgsMultiCurve *>( sipCpp ) != nullptr )
108 sipType = sipType_QgsMultiCurve;
109 else if ( qgsgeometry_cast<QgsGeometryCollection *>( sipCpp ) != nullptr )
110 sipType = sipType_QgsGeometryCollection;
111 else
112 sipType = 0;
113 SIP_END
114#endif
115
116 Q_GADGET
117
118 public:
119
122 {
123
128 MaximumAngle = 0,
129
134 MaximumDifference
135 };
136 Q_ENUM( SegmentationToleranceType )
137
138
140 {
141
145 XY = 0,
146
150 YX
151 };
153
154
158 virtual ~QgsAbstractGeometry() = default;
160 QgsAbstractGeometry &operator=( const QgsAbstractGeometry &geom );
161
162 virtual bool operator==( const QgsAbstractGeometry &other ) const = 0;
163 virtual bool operator!=( const QgsAbstractGeometry &other ) const = 0;
164
168 virtual QgsAbstractGeometry *clone() const = 0 SIP_FACTORY;
169
175 virtual int compareTo( const QgsAbstractGeometry *other ) const;
176
180 virtual void clear() = 0;
181
185 virtual QgsRectangle boundingBox() const;
186
192 virtual QgsBox3D boundingBox3D() const = 0;
193
194 //mm-sql interface
195
200 virtual int dimension() const = 0;
201
207 virtual QString geometryType() const = 0;
208
214 inline Qgis::WkbType wkbType() const SIP_HOLDGIL { return mWkbType; }
215
221 QString wktTypeStr() const;
222
227 bool is3D() const SIP_HOLDGIL
228 {
229 return QgsWkbTypes::hasZ( mWkbType );
230 }
231
237 {
238 return QgsWkbTypes::hasM( mWkbType );
239 }
240
248
258 virtual void normalize() = 0;
259
260 //import
261
267 virtual bool fromWkb( QgsConstWkbPtr &wkb ) = 0;
268
273 virtual bool fromWkt( const QString &wkt ) = 0;
274
275 //export
276
282 {
283 FlagExportTrianglesAsPolygons = 1 << 0,
284 FlagExportNanAsDoubleMin = 1 << 1,
285 };
286 Q_DECLARE_FLAGS( WkbFlags, WkbFlag )
287
288
295 virtual int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const = 0;
296
308 virtual QByteArray asWkb( WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const = 0;
309
318 virtual QString asWkt( int precision = 17 ) const = 0;
319
331 virtual QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const = 0;
332
344 virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const = 0;
345
355 QString asJson( int precision = 17 );
356
367 virtual json asJsonObject( int precision = 17 ) SIP_SKIP const;
368
373 virtual QString asKml( int precision = 17 ) const = 0;
374
375
376 //render pipeline
377
388 virtual void transform( const QgsCoordinateTransform &ct, Qgis::TransformDirection d = Qgis::TransformDirection::Forward, bool transformZ = false ) SIP_THROW( QgsCsException ) = 0;
389
396 virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0,
397 double mTranslate = 0.0, double mScale = 1.0 ) = 0;
398
403 virtual void draw( QPainter &p ) const = 0;
404
413 virtual QPainterPath asQPainterPath() const = 0;
414
425 virtual int vertexNumberFromVertexId( QgsVertexId id ) const = 0;
426
434 virtual bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const = 0;
435
440 virtual void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const = 0;
441
446 virtual QgsCoordinateSequence coordinateSequence() const = 0;
447
451 virtual int nCoordinates() const;
452
456 virtual QgsPoint vertexAt( QgsVertexId id ) const = 0;
457
470 virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt SIP_OUT,
471 QgsVertexId &vertexAfter SIP_OUT,
472 int *leftOf SIP_OUT = nullptr, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const = 0;
473
474 //low-level editing
475
484 virtual bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) = 0;
485
494 virtual bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) = 0;
495
503 virtual bool deleteVertex( QgsVertexId position ) = 0;
504
517 virtual double length() const;
518
531 virtual double perimeter() const;
532
545 virtual double area() const;
546
555 virtual double segmentLength( QgsVertexId startVertex ) const = 0;
556
558 virtual QgsPoint centroid() const;
559
563 virtual bool isEmpty() const;
564
568 virtual bool hasCurvedSegments() const;
569
578 virtual bool boundingBoxIntersects( const QgsRectangle &rectangle ) const SIP_HOLDGIL;
579
588 virtual bool boundingBoxIntersects( const QgsBox3D &box3d ) const SIP_HOLDGIL;
589
596 virtual QgsAbstractGeometry *segmentize( double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const SIP_FACTORY;
597
604 virtual QgsAbstractGeometry *toCurveType() const = 0 SIP_FACTORY;
605
628 virtual QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const = 0 SIP_FACTORY;
629
650 virtual bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) = 0;
651
659 virtual double vertexAngle( QgsVertexId vertex ) const = 0;
660
664 virtual int vertexCount( int part = 0, int ring = 0 ) const = 0;
665
669 virtual int ringCount( int part = 0 ) const = 0;
670
676 virtual int partCount() const = 0;
677
686 virtual bool addZValue( double zValue = 0 ) = 0;
687
696 virtual bool addMValue( double mValue = 0 ) = 0;
697
705 virtual bool dropZValue() = 0;
706
714 virtual bool dropMValue() = 0;
715
722 virtual void swapXy() = 0;
723
729 virtual bool convertTo( Qgis::WkbType type );
730
749 virtual const QgsAbstractGeometry *simplifiedTypeRef() const SIP_HOLDGIL;
750
762 virtual bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const = 0;
763
777 virtual bool transform( QgsAbstractGeometryTransformer *transformer, QgsFeedback *feedback = nullptr ) = 0;
778
779#ifndef SIP_RUN
780
790 virtual void filterVertices( const std::function< bool( const QgsPoint & ) > &filter );
791
806 virtual void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform );
807
813 class CORE_EXPORT part_iterator
814 {
815 private:
816
817 int mIndex = 0;
818 QgsAbstractGeometry *mGeometry = nullptr;
819
820 public:
822 part_iterator() = default;
823
825 part_iterator( QgsAbstractGeometry *g, int index );
826
831 part_iterator &operator++();
832
834 part_iterator operator++( int );
835
838
840 int partNumber() const;
841
842 bool operator==( part_iterator other ) const;
843 bool operator!=( part_iterator other ) const { return !( *this == other ); }
844 };
845
855 {
856 return part_iterator( this, 0 );
857 }
858
867 part_iterator parts_end();
868
876 QgsGeometryConstPartIterator parts() const;
877
883 class CORE_EXPORT const_part_iterator
884 {
885 private:
886
887 int mIndex = 0;
888 const QgsAbstractGeometry *mGeometry = nullptr;
889
890 public:
893
895 const_part_iterator( const QgsAbstractGeometry *g, int index );
896
901 const_part_iterator &operator++();
902
904 const_part_iterator operator++( int );
905
907 const QgsAbstractGeometry *operator*() const;
908
910 int partNumber() const;
911
912 bool operator==( const_part_iterator other ) const;
913 bool operator!=( const_part_iterator other ) const { return !( *this == other ); }
914 };
915
924 {
925 return const_part_iterator( this, 0 );
926 }
927
935 const_part_iterator const_parts_end() const;
936
937
943 class CORE_EXPORT vertex_iterator
944 {
945 private:
946
952 struct Level
953 {
954 const QgsAbstractGeometry *g = nullptr;
955 int index = 0;
956
957 bool operator==( const Level &other ) const;
958 };
959
960 std::array<Level, 3> levels;
961 int depth = -1;
962
963 void digDown();
964
965 public:
967 vertex_iterator() = default;
968
970 vertex_iterator( const QgsAbstractGeometry *g, int index );
971
976 vertex_iterator &operator++();
977
979 vertex_iterator operator++( int );
980
982 QgsPoint operator*() const;
983
985 QgsVertexId vertexId() const;
986
987 bool operator==( const vertex_iterator &other ) const;
988 bool operator!=( const vertex_iterator &other ) const { return !( *this == other ); }
989 };
990
1000 {
1001 return vertex_iterator( this, 0 );
1002 }
1003
1013 {
1014 return vertex_iterator( this, childCount() );
1015 }
1016#endif
1017
1052
1053
1077 QgsVertexIterator vertices() const;
1078
1086
1087 protected:
1088
1095 int sortIndex() const;
1096
1107 virtual int compareToSameClass( const QgsAbstractGeometry *other ) const = 0;
1108
1114 virtual bool hasChildGeometries() const;
1115
1121 virtual int childCount() const { return 0; }
1122
1128 virtual QgsAbstractGeometry *childGeometry( int index ) const { Q_UNUSED( index ) return nullptr; }
1129
1135 virtual QgsPoint childPoint( int index ) const;
1136
1137 protected:
1139
1143 void setZMTypeFromSubGeometry( const QgsAbstractGeometry *subggeom, Qgis::WkbType baseGeomType );
1144
1149 virtual QgsRectangle calculateBoundingBox() const;
1150
1157 virtual QgsBox3D calculateBoundingBox3D() const;
1158
1162 virtual void clearCache() const;
1163
1164 friend class TestQgsGeometry;
1165};
1166
1167
1168#ifndef SIP_RUN
1169
1170template <class T>
1172{
1173 return const_cast<T>( std::remove_pointer<T>::type::cast( geom ) );
1174}
1175
1176#endif
1177
1178// clazy:excludeall=qstring-allocations
1179
1185class CORE_EXPORT QgsVertexIterator
1186{
1187 public:
1190
1193 : g( geometry )
1194 , i( g->vertices_begin() )
1195 , n( g->vertices_end() )
1196 {
1197 }
1198
1200 bool hasNext() const
1201 {
1202 return g && g->vertices_end() != i;
1203 }
1204
1206 QgsPoint next();
1207
1208#ifdef SIP_RUN
1209 QgsVertexIterator *__iter__();
1210 % MethodCode
1211 sipRes = sipCpp;
1212 % End
1213
1214 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsPoint );
1215 % MethodCode
1216 if ( sipCpp->hasNext() )
1217 sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None );
1218 else
1219 PyErr_SetString( PyExc_StopIteration, "" );
1220 % End
1221#endif
1222
1223 private:
1224 const QgsAbstractGeometry *g = nullptr;
1226
1227};
1228
1235{
1236 public:
1239
1242 : g( geometry )
1243 , i( g->parts_begin() )
1244 , n( g->parts_end() )
1245 {
1246 }
1247
1250 {
1251 return g && g->parts_end() != i;
1252 }
1253
1255 QgsAbstractGeometry *next();
1256
1257#ifdef SIP_RUN
1258 QgsGeometryPartIterator *__iter__();
1259 % MethodCode
1260 sipRes = sipCpp;
1261 % End
1262
1263 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
1264 % MethodCode
1265 if ( sipCpp->hasNext() )
1266 sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL );
1267 else
1268 PyErr_SetString( PyExc_StopIteration, "" );
1269 % End
1270#endif
1271
1272 private:
1273 QgsAbstractGeometry *g = nullptr;
1275
1276};
1277
1278
1285{
1286 public:
1289
1292 : g( geometry )
1293 , i( g->const_parts_begin() )
1294 , n( g->const_parts_end() )
1295 {
1296 }
1297
1300 {
1301 return g && g->const_parts_end() != i;
1302 }
1303
1305 const QgsAbstractGeometry *next();
1306
1307#ifdef SIP_RUN
1308 QgsGeometryConstPartIterator *__iter__();
1309 % MethodCode
1310 sipRes = sipCpp;
1311 % End
1312
1313 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
1314 % MethodCode
1315 if ( sipCpp->hasNext() )
1316 sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL );
1317 else
1318 PyErr_SetString( PyExc_StopIteration, "" );
1319 % End
1320#endif
1321
1322 private:
1323 const QgsAbstractGeometry *g = nullptr;
1325
1326};
1327
1328Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractGeometry::WkbFlags )
1329
1330#endif //QGSABSTRACTGEOMETRYV2
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:182
@ Unknown
Unknown.
An abstract base class for classes which transform geometries by transforming input points to output ...
The part_iterator class provides STL-style iterator for const references to geometry parts.
bool operator!=(const_part_iterator other) const
const_part_iterator()=default
Create invalid iterator.
The part_iterator class provides STL-style iterator for geometry parts.
part_iterator()=default
Create invalid iterator.
bool operator!=(part_iterator other) const
The vertex_iterator class provides STL-style iterator for vertices.
vertex_iterator()=default
Create invalid iterator.
bool operator!=(const vertex_iterator &other) const
Abstract base class for all geometries.
virtual bool fromWkb(QgsConstWkbPtr &wkb)=0
Sets the geometry from a WKB string.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
virtual QgsAbstractGeometry * boundary() const =0
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
bool isMeasure() const
Returns true if the geometry contains m values.
virtual int childCount() const
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
AxisOrder
Axis order for GML generation.
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
virtual bool fromWkt(const QString &wkt)=0
Sets the geometry from a WKT string.
virtual void normalize()=0
Reorganizes the geometry into a normalized form (or "canonical" form).
vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
virtual QgsAbstractGeometry * childGeometry(int index) const
Returns pointer to child geometry (for geometries with child geometries - i.e.
const_part_iterator const_parts_begin() const
Returns STL-style iterator pointing to the const first part of the geometry.
virtual int compareToSameClass(const QgsAbstractGeometry *other) const =0
Compares to an other geometry of the same class, and returns a integer for sorting of the two geometr...
part_iterator parts_begin()
Returns STL-style iterator pointing to the first part of the geometry.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:44
A const WKB pointer.
Definition qgswkbptr.h:138
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Abstract base class for curved geometry type.
Definition qgscurve.h:36
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:45
Java-style iterator for const traversal of parts of a geometry.
QgsGeometryConstPartIterator(const QgsAbstractGeometry *geometry)
Constructs iterator for the given geometry.
bool hasNext() const
Find out whether there are more parts.
QgsGeometryConstPartIterator()=default
Constructor for QgsGeometryConstPartIterator.
Java-style iterator for traversal of parts of a geometry.
QgsGeometryPartIterator()=default
Constructor for QgsGeometryPartIterator.
bool hasNext() const
Find out whether there are more parts.
QgsGeometryPartIterator(QgsAbstractGeometry *geometry)
Constructs iterator for the given geometry.
Perform transforms between map coordinates and device coordinates.
Multi curve geometry collection.
Multi point geometry collection.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A rectangle specified with double values.
Java-style iterator for traversal of vertices of a geometry.
bool hasNext() const
Find out whether there are more vertices.
QgsVertexIterator()=default
Constructor for QgsVertexIterator.
QgsVertexIterator(const QgsAbstractGeometry *geometry)
Constructs iterator for the given geometry.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:227
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:186
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:166
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_THROW(name,...)
Definition qgis_sip.h:198
#define SIP_END
Definition qgis_sip.h:203
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
QVector< QgsRingSequence > QgsCoordinateSequence
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
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
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
double closestSegment(const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon)
Definition qgstracer.cpp:69
int precision
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:31