QGIS API Documentation 3.43.0-Master (c6edab485a4)
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 <nlohmann/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
79class CORE_EXPORT QgsAbstractGeometry
80{
81
82#ifdef SIP_RUN
84 if ( qgsgeometry_cast<QgsPoint *>( sipCpp ) != nullptr )
85 sipType = sipType_QgsPoint;
86 else if ( qgsgeometry_cast<QgsLineString *>( sipCpp ) != nullptr )
87 sipType = sipType_QgsLineString;
88 else if ( qgsgeometry_cast<QgsCircularString *>( sipCpp ) != nullptr )
89 sipType = sipType_QgsCircularString;
90 else if ( qgsgeometry_cast<QgsCompoundCurve *>( sipCpp ) != nullptr )
91 sipType = sipType_QgsCompoundCurve;
92 else if ( qgsgeometry_cast<QgsTriangle *>( sipCpp ) != nullptr )
93 sipType = sipType_QgsTriangle;
94 else if ( qgsgeometry_cast<QgsPolygon *>( sipCpp ) != nullptr )
95 sipType = sipType_QgsPolygon;
96 else if ( qgsgeometry_cast<QgsCurvePolygon *>( sipCpp ) != nullptr )
97 sipType = sipType_QgsCurvePolygon;
98 else if ( qgsgeometry_cast<QgsTriangulatedSurface *>( sipCpp ) != nullptr )
99 sipType = sipType_QgsTriangulatedSurface;
100 else if ( qgsgeometry_cast<QgsPolyhedralSurface *>( sipCpp ) != nullptr )
101 sipType = sipType_QgsPolyhedralSurface;
102 else if ( qgsgeometry_cast<QgsSurface *>( sipCpp ) != nullptr )
103 sipType = sipType_QgsSurface;
104 else if ( qgsgeometry_cast<QgsMultiPoint *>( sipCpp ) != nullptr )
105 sipType = sipType_QgsMultiPoint;
106 else if ( qgsgeometry_cast<QgsMultiLineString *>( sipCpp ) != nullptr )
107 sipType = sipType_QgsMultiLineString;
108 else if ( qgsgeometry_cast<QgsMultiPolygon *>( sipCpp ) != nullptr )
109 sipType = sipType_QgsMultiPolygon;
110 else if ( qgsgeometry_cast<QgsMultiSurface *>( sipCpp ) != nullptr )
111 sipType = sipType_QgsMultiSurface;
112 else if ( qgsgeometry_cast<QgsMultiCurve *>( sipCpp ) != nullptr )
113 sipType = sipType_QgsMultiCurve;
114 else if ( qgsgeometry_cast<QgsGeometryCollection *>( sipCpp ) != nullptr )
115 sipType = sipType_QgsGeometryCollection;
116 else
117 sipType = 0;
118 SIP_END
119#endif
120
121 Q_GADGET
122
123 public:
124
127 {
128
133 MaximumAngle = 0,
134
139 MaximumDifference
140 };
141 Q_ENUM( SegmentationToleranceType )
142
143
145 {
146
150 XY = 0,
151
155 YX
156 };
158
160 virtual ~QgsAbstractGeometry() = default;
162 QgsAbstractGeometry &operator=( const QgsAbstractGeometry &geom );
163
164 virtual bool operator==( const QgsAbstractGeometry &other ) const = 0;
165 virtual bool operator!=( const QgsAbstractGeometry &other ) const = 0;
166
177 virtual bool fuzzyEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const = 0;
178
192 virtual bool fuzzyDistanceEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const = 0;
193
197 virtual QgsAbstractGeometry *clone() const = 0 SIP_FACTORY;
198
204 virtual int compareTo( const QgsAbstractGeometry *other ) const;
205
209 virtual void clear() = 0;
210
214 virtual QgsRectangle boundingBox() const;
215
221 virtual QgsBox3D boundingBox3D() const = 0;
222
223 //mm-sql interface
224
229 virtual int dimension() const = 0;
230
236 virtual QString geometryType() const = 0;
237
243 inline Qgis::WkbType wkbType() const SIP_HOLDGIL { return mWkbType; }
244
250 QString wktTypeStr() const;
251
256 bool is3D() const SIP_HOLDGIL
257 {
258 return QgsWkbTypes::hasZ( mWkbType );
259 }
260
266 {
267 return QgsWkbTypes::hasM( mWkbType );
268 }
269
276
286 virtual void normalize() = 0;
287
288 //import
289
295 virtual bool fromWkb( QgsConstWkbPtr &wkb ) = 0;
296
301 virtual bool fromWkt( const QString &wkt ) = 0;
302
303 //export
304
310 {
311 FlagExportTrianglesAsPolygons = 1 << 0,
312 FlagExportNanAsDoubleMin = 1 << 1,
313 };
314 Q_DECLARE_FLAGS( WkbFlags, WkbFlag )
315
316
323 virtual int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const = 0;
324
335 virtual QByteArray asWkb( WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const = 0;
336
345 virtual QString asWkt( int precision = 17 ) const = 0;
346
358 virtual QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const = 0;
359
371 virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const = 0;
372
382 QString asJson( int precision = 17 );
383
394 virtual json asJsonObject( int precision = 17 ) SIP_SKIP const;
395
400 virtual QString asKml( int precision = 17 ) const = 0;
401
402
403 //render pipeline
404
415 virtual void transform( const QgsCoordinateTransform &ct, Qgis::TransformDirection d = Qgis::TransformDirection::Forward, bool transformZ = false ) SIP_THROW( QgsCsException ) = 0;
416
423 virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0,
424 double mTranslate = 0.0, double mScale = 1.0 ) = 0;
425
430 virtual void draw( QPainter &p ) const = 0;
431
440 virtual QPainterPath asQPainterPath() const = 0;
441
451 virtual int vertexNumberFromVertexId( QgsVertexId id ) const = 0;
452
460 virtual bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const = 0;
461
465 virtual void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const = 0;
466
471 virtual QgsCoordinateSequence coordinateSequence() const = 0;
472
476 virtual int nCoordinates() const;
477
481 virtual QgsPoint vertexAt( QgsVertexId id ) const = 0;
482
495 virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt SIP_OUT,
496 QgsVertexId &vertexAfter SIP_OUT,
497 int *leftOf SIP_OUT = nullptr, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const = 0;
498
499 //low-level editing
500
509 virtual bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) = 0;
510
519 virtual bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) = 0;
520
528 virtual bool deleteVertex( QgsVertexId position ) = 0;
529
542 virtual double length() const;
543
556 virtual double perimeter() const;
557
570 virtual double area() const;
571
579 virtual double segmentLength( QgsVertexId startVertex ) const = 0;
580
582 virtual QgsPoint centroid() const;
583
587 virtual bool isEmpty() const;
588
592 virtual bool hasCurvedSegments() const;
593
602 virtual bool boundingBoxIntersects( const QgsRectangle &rectangle ) const SIP_HOLDGIL;
603
612 virtual bool boundingBoxIntersects( const QgsBox3D &box3d ) const SIP_HOLDGIL;
613
620 virtual QgsAbstractGeometry *segmentize( double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const SIP_FACTORY;
621
628 virtual QgsAbstractGeometry *toCurveType() const = 0 SIP_FACTORY;
629
652 virtual QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0, bool removeRedundantPoints = false ) const = 0 SIP_FACTORY;
653
666 virtual QgsAbstractGeometry *simplifyByDistance( double tolerance ) const = 0 SIP_FACTORY;
667
687 virtual bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) = 0;
688
696 virtual double vertexAngle( QgsVertexId vertex ) const = 0;
697
701 virtual int vertexCount( int part = 0, int ring = 0 ) const = 0;
702
706 virtual int ringCount( int part = 0 ) const = 0;
707
713 virtual int partCount() const = 0;
714
722 virtual bool addZValue( double zValue = 0 ) = 0;
723
731 virtual bool addMValue( double mValue = 0 ) = 0;
732
739 virtual bool dropZValue() = 0;
740
747 virtual bool dropMValue() = 0;
748
755 virtual void swapXy() = 0;
756
761 virtual bool convertTo( Qgis::WkbType type );
762
781 virtual const QgsAbstractGeometry *simplifiedTypeRef() const SIP_HOLDGIL;
782
794 virtual bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const = 0;
795
809 virtual bool transform( QgsAbstractGeometryTransformer *transformer, QgsFeedback *feedback = nullptr ) = 0;
810
811#ifndef SIP_RUN
812
822 virtual void filterVertices( const std::function< bool( const QgsPoint & ) > &filter );
823
838 virtual void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform );
839
845 class CORE_EXPORT part_iterator
846 {
847 private:
848
849 int mIndex = 0;
850 QgsAbstractGeometry *mGeometry = nullptr;
851
852 public:
854 part_iterator() = default;
855
857 part_iterator( QgsAbstractGeometry *g, int index );
858
863 part_iterator &operator++();
864
866 part_iterator operator++( int );
867
870
872 int partNumber() const;
873
874 bool operator==( part_iterator other ) const;
875 bool operator!=( part_iterator other ) const { return !( *this == other ); }
876 };
877
887 {
888 return part_iterator( this, 0 );
889 }
890
899 part_iterator parts_end();
900
908 QgsGeometryConstPartIterator parts() const;
909
915 class CORE_EXPORT const_part_iterator
916 {
917 private:
918
919 int mIndex = 0;
920 const QgsAbstractGeometry *mGeometry = nullptr;
921
922 public:
925
927 const_part_iterator( const QgsAbstractGeometry *g, int index );
928
933 const_part_iterator &operator++();
934
936 const_part_iterator operator++( int );
937
939 const QgsAbstractGeometry *operator*() const;
940
942 int partNumber() const;
943
944 bool operator==( const_part_iterator other ) const;
945 bool operator!=( const_part_iterator other ) const { return !( *this == other ); }
946 };
947
956 {
957 return const_part_iterator( this, 0 );
958 }
959
967 const_part_iterator const_parts_end() const;
968
969
974 class CORE_EXPORT vertex_iterator
975 {
976 private:
977
983 struct Level
984 {
985 const QgsAbstractGeometry *g = nullptr;
986 int index = 0;
987
988 bool operator==( const Level &other ) const;
989 };
990
991 std::array<Level, 3> levels;
992 int depth = -1;
993
994 void digDown();
995
996 public:
998 vertex_iterator() = default;
999
1001 vertex_iterator( const QgsAbstractGeometry *g, int index );
1002
1007 vertex_iterator &operator++();
1008
1010 vertex_iterator operator++( int );
1011
1013 QgsPoint operator*() const;
1014
1016 QgsVertexId vertexId() const;
1017
1018 bool operator==( const vertex_iterator &other ) const;
1019 bool operator!=( const vertex_iterator &other ) const { return !( *this == other ); }
1020 };
1021
1030 {
1031 return vertex_iterator( this, 0 );
1032 }
1033
1042 {
1043 return vertex_iterator( this, childCount() );
1044 }
1045#endif
1046
1081
1082
1105 QgsVertexIterator vertices() const;
1106
1113
1114 protected:
1115
1122 int sortIndex() const;
1123
1134 virtual int compareToSameClass( const QgsAbstractGeometry *other ) const = 0;
1135
1140 virtual bool hasChildGeometries() const;
1141
1146 virtual int childCount() const { return 0; }
1147
1152 virtual QgsAbstractGeometry *childGeometry( int index ) const { Q_UNUSED( index ) return nullptr; }
1153
1158 virtual QgsPoint childPoint( int index ) const;
1159
1160 protected:
1162
1166 void setZMTypeFromSubGeometry( const QgsAbstractGeometry *subggeom, Qgis::WkbType baseGeomType );
1167
1172 virtual QgsRectangle calculateBoundingBox() const;
1173
1180 virtual QgsBox3D calculateBoundingBox3D() const;
1181
1185 virtual void clearCache() const;
1186
1187 friend class TestQgsGeometry;
1188};
1189
1190
1191#ifndef SIP_RUN
1192
1193template <class T>
1195{
1196 return std::remove_pointer<T>::type::cast( geom );
1197}
1198
1199template <class T>
1201{
1202 return std::remove_pointer<T>::type::cast( geom );
1203}
1204
1205#endif
1206
1207// clazy:excludeall=qstring-allocations
1208
1213class CORE_EXPORT QgsVertexIterator
1214{
1215 public:
1216
1218
1221 : g( geometry )
1222 , i( g->vertices_begin() )
1223 , n( g->vertices_end() )
1224 {
1225 }
1226
1228 bool hasNext() const
1229 {
1230 return g && g->vertices_end() != i;
1231 }
1232
1234 QgsPoint next();
1235
1236#ifdef SIP_RUN
1237 QgsVertexIterator *__iter__();
1238 % MethodCode
1239 sipRes = sipCpp;
1240 % End
1241
1242 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsPoint );
1243 % MethodCode
1244 if ( sipCpp->hasNext() )
1245 sipRes = sipConvertFromType( new QgsPoint( sipCpp->next() ), sipType_QgsPoint, Py_None );
1246 else
1247 PyErr_SetString( PyExc_StopIteration, "" );
1248 % End
1249#endif
1250
1251 private:
1252 const QgsAbstractGeometry *g = nullptr;
1254
1255};
1256
1263{
1264 public:
1265
1267
1270 : g( geometry )
1271 , i( g->parts_begin() )
1272 , n( g->parts_end() )
1273 {
1274 }
1275
1278 {
1279 return g && g->parts_end() != i;
1280 }
1281
1283 QgsAbstractGeometry *next();
1284
1285#ifdef SIP_RUN
1286 QgsGeometryPartIterator *__iter__();
1287 % MethodCode
1288 sipRes = sipCpp;
1289 % End
1290
1291 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
1292 % MethodCode
1293 if ( sipCpp->hasNext() )
1294 sipRes = sipConvertFromType( sipCpp->next(), sipType_QgsAbstractGeometry, NULL );
1295 else
1296 PyErr_SetString( PyExc_StopIteration, "" );
1297 % End
1298#endif
1299
1300 private:
1301 QgsAbstractGeometry *g = nullptr;
1303
1304};
1305
1306
1313{
1314 public:
1315
1317
1320 : g( geometry )
1321 , i( g->const_parts_begin() )
1322 , n( g->const_parts_end() )
1323 {
1324 }
1325
1328 {
1329 return g && g->const_parts_end() != i;
1330 }
1331
1333 const QgsAbstractGeometry *next();
1334
1335#ifdef SIP_RUN
1336 QgsGeometryConstPartIterator *__iter__();
1337 % MethodCode
1338 sipRes = sipCpp;
1339 % End
1340
1341 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsAbstractGeometry );
1342 % MethodCode
1343 if ( sipCpp->hasNext() )
1344 sipRes = sipConvertFromType( const_cast< QgsAbstractGeometry * >( sipCpp->next() ), sipType_QgsAbstractGeometry, NULL );
1345 else
1346 PyErr_SetString( PyExc_StopIteration, "" );
1347 % End
1348#endif
1349
1350 private:
1351 const QgsAbstractGeometry *g = nullptr;
1353
1354};
1355
1357
1358#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:256
@ 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.
QFlags< WkbFlag > WkbFlags
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:43
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:35
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
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.
Java-style iterator for traversal of parts of a geometry.
QgsGeometryPartIterator()=default
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
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:232
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:171
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
#define SIP_END
Definition qgis_sip.h:208
T qgsgeometry_cast(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:249
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
double closestSegment(const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon)
Definition qgstracer.cpp:70
int precision
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30