QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsgeometrycollection.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometrycollection.h
3 -------------------------------------------------------------------
4Date : 28 Oct 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 QGSGEOMETRYCOLLECTION_H
17#define QGSGEOMETRYCOLLECTION_H
18
19#include "qgis_core.h"
20#include "qgis_sip.h"
21#include "qgsabstractgeometry.h"
22#include "qgsbox3d.h"
23#include "qgsrectangle.h"
24
25#include <QVector>
26
27class QgsPoint;
28
29
36{
37 public:
38
39
44
47 ~QgsGeometryCollection() override;
48
49 bool operator==( const QgsAbstractGeometry &other ) const override
50 {
51 return fuzzyEqual( other, 1e-8 );
52 }
53
54 bool operator!=( const QgsAbstractGeometry &other ) const override
55 {
56 return !operator==( other );
57 }
58
59#ifndef SIP_RUN
60 private:
61 bool fuzzyHelper( const QgsAbstractGeometry &other, double epsilon, bool useDistance ) const
62 {
64 if ( !otherCollection )
65 return false;
66
67 if ( mWkbType != otherCollection->mWkbType )
68 return false;
69
70 if ( mGeometries.count() != otherCollection->mGeometries.count() )
71 return false;
72
73 for ( int i = 0; i < mGeometries.count(); ++i )
74 {
75 QgsAbstractGeometry *g1 = mGeometries.at( i );
76 QgsAbstractGeometry *g2 = otherCollection->mGeometries.at( i );
77
78 // Quick check if the geometries are exactly the same
79 if ( g1 != g2 )
80 {
81 if ( !g1 || !g2 )
82 return false;
83
84 // Slower check, compare the contents of the geometries
85 if ( useDistance )
86 {
87 if ( !( *g1 ).fuzzyDistanceEqual( *g2, epsilon ) )
88 {
89 return false;
90 }
91 }
92 else
93 {
94 if ( !( *g1 ).fuzzyEqual( *g2, epsilon ) )
95 {
96 return false;;
97 }
98 }
99 }
100 }
101 return true;
102 }
103#endif
104 public:
105 bool fuzzyEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL
106 {
107 return fuzzyHelper( other, epsilon, false );
108 }
109 bool fuzzyDistanceEqual( const QgsAbstractGeometry &other, double epsilon = 1e-8 ) const override SIP_HOLDGIL
110 {
111 return fuzzyHelper( other, epsilon, true );
112 }
113
114 QgsGeometryCollection *clone() const override SIP_FACTORY;
115
120 {
121 return mGeometries.size();
122 }
123
124#ifdef SIP_RUN
125
129 int __len__() const;
130 % MethodCode
131 sipRes = sipCpp->numGeometries();
132 % End
133
135 int __bool__() const;
136 % MethodCode
137 sipRes = true;
138 % End
139#endif
140
141
148 {
149 return mGeometries.value( n );
150 }
151
152#ifndef SIP_RUN
153
158 QgsAbstractGeometry *geometryN( int n ) SIP_HOLDGIL;
159#else
160
166 SIP_PYOBJECT geometryN( int n ) SIP_TYPEHINT( QgsAbstractGeometry );
167 % MethodCode
168 if ( a0 < 0 || a0 >= sipCpp->numGeometries() )
169 {
170 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
171 sipIsErr = 1;
172 }
173 else
174 {
175 return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
176 }
177 % End
178#endif
179
180
181 //methods inherited from QgsAbstractGeometry
182 bool isEmpty() const override SIP_HOLDGIL;
183 int dimension() const override SIP_HOLDGIL;
184 QString geometryType() const override SIP_HOLDGIL;
185 void clear() override;
186 QgsGeometryCollection *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0, bool removeRedundantPoints = false ) const override SIP_FACTORY;
187 bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
188 QgsAbstractGeometry *boundary() const override SIP_FACTORY;
189 void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const override;
190 int vertexNumberFromVertexId( QgsVertexId id ) const override;
191 bool boundingBoxIntersects( const QgsBox3D &box3d ) const override SIP_HOLDGIL;
192
201 void reserve( int size ) SIP_HOLDGIL;
202
204 virtual bool addGeometry( QgsAbstractGeometry *g SIP_TRANSFER );
205
213 virtual bool addGeometries( const QVector< QgsAbstractGeometry * > &geometries SIP_TRANSFER );
214
220 virtual bool insertGeometry( QgsAbstractGeometry *g SIP_TRANSFER, int index );
221
222#ifndef SIP_RUN
223
229 virtual bool removeGeometry( int nr );
230#else
231
238 virtual bool removeGeometry( int nr );
239 % MethodCode
240 const int count = sipCpp->numGeometries();
241 if ( a0 < 0 || a0 >= count )
242 {
243 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
244 sipIsErr = 1;
245 }
246 else
247 {
248 return PyBool_FromLong( sipCpp->removeGeometry( a0 ) );
249 }
250 % End
251#endif
252
259 QVector< QgsAbstractGeometry * > takeGeometries() SIP_TRANSFER;
260
261 void normalize() final SIP_HOLDGIL;
262 void transform( const QgsCoordinateTransform &ct, Qgis::TransformDirection d = Qgis::TransformDirection::Forward, bool transformZ = false ) override SIP_THROW( QgsCsException );
263 void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
264
265 void draw( QPainter &p ) const override;
266 QPainterPath asQPainterPath() const override;
267
268 bool fromWkb( QgsConstWkbPtr &wkb ) override;
269 bool fromWkt( const QString &wkt ) override;
270
271 int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
272 QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
273 QString asWkt( int precision = 17 ) const override;
274 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
275 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
276 json asJsonObject( int precision = 17 ) const override SIP_SKIP;
277 QString asKml( int precision = 17 ) const override;
278
279 QgsBox3D boundingBox3D() const override;
280
281 QgsCoordinateSequence coordinateSequence() const override;
282 int nCoordinates() const override;
283
284 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;
285 bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const override;
286
287 //low-level editing
288 bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
289 bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
290 bool deleteVertex( QgsVertexId position ) override;
291
292 double length() const override SIP_HOLDGIL;
293 double area() const override SIP_HOLDGIL;
294 double perimeter() const override SIP_HOLDGIL;
295
296 bool hasCurvedSegments() const override SIP_HOLDGIL;
297
303 QgsAbstractGeometry *segmentize( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
304
305 double vertexAngle( QgsVertexId vertex ) const override;
306 double segmentLength( QgsVertexId startVertex ) const override;
307 int vertexCount( int part = 0, int ring = 0 ) const override;
308 int ringCount( int part = 0 ) const override;
309 int partCount() const override;
310 QgsPoint vertexAt( QgsVertexId id ) const override;
311 bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const override;
312
313 bool addZValue( double zValue = 0 ) override;
314 bool addMValue( double mValue = 0 ) override;
315 bool dropZValue() override;
316 bool dropMValue() override;
317 void swapXy() override;
318 QgsGeometryCollection *toCurveType() const override SIP_FACTORY;
319 const QgsAbstractGeometry *simplifiedTypeRef() const override SIP_HOLDGIL;
320 QgsGeometryCollection *simplifyByDistance( double tolerance ) const override SIP_FACTORY;
321
322 bool transform( QgsAbstractGeometryTransformer *transformer, QgsFeedback *feedback = nullptr ) override;
323
324#ifndef SIP_RUN
325 void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
326 void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) override;
327
336 inline static const QgsGeometryCollection *cast( const QgsAbstractGeometry *geom )
337 {
338 if ( geom && QgsWkbTypes::isMultiType( geom->wkbType() ) )
339 return static_cast<const QgsGeometryCollection *>( geom );
340 return nullptr;
341 }
342
352 {
353 if ( geom && QgsWkbTypes::isMultiType( geom->wkbType() ) )
354 return static_cast<QgsGeometryCollection *>( geom );
355 return nullptr;
356 }
357#endif
358
359
360#ifdef SIP_RUN
361
372 SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsAbstractGeometry );
373 % MethodCode
374 const int count = sipCpp->numGeometries();
375 if ( a0 < -count || a0 >= count )
376 {
377 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
378 sipIsErr = 1;
379 }
380 else if ( a0 >= 0 )
381 {
382 return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
383 }
384 else
385 {
386 return sipConvertFromType( sipCpp->geometryN( count + a0 ), sipType_QgsAbstractGeometry, NULL );
387 }
388 % End
389
400 void __delitem__( int index );
401 % MethodCode
402 const int count = sipCpp->numGeometries();
403 if ( a0 >= 0 && a0 < count )
404 sipCpp->removeGeometry( a0 );
405 else if ( a0 < 0 && a0 >= -count )
406 sipCpp->removeGeometry( count + a0 );
407 else
408 {
409 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
410 sipIsErr = 1;
411 }
412 % End
413
419 SIP_PYOBJECT __iter__() SIP_TYPEHINT( QgsGeometryPartIterator );
420 % MethodCode
421 sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
422 % End
423#endif
424
438 QgsGeometryCollection *extractPartsByType( Qgis::WkbType type, bool useFlatType = true ) const SIP_FACTORY;
439
440 QgsGeometryCollection *createEmptyWithSameType() const override SIP_FACTORY;
441
442 protected:
443 int childCount() const override;
444 QgsAbstractGeometry *childGeometry( int index ) const override;
445 int compareToSameClass( const QgsAbstractGeometry *other ) const final;
446
447 protected:
449
453 virtual bool wktOmitChildType() const;
454
458 SIP_SKIP bool fromCollectionWkt( const QString &wkt, const QVector<Qgis::WkbType> &subtypes, const QString &defaultChildWkbType = QString() );
459
460 QgsBox3D calculateBoundingBox3D() const override;
461 void clearCache() const override;
462
463 private:
464
465 mutable QgsBox3D mBoundingBox;
466 mutable bool mHasCachedValidity = false;
467 mutable QString mValidityFailureReason;
468};
469
470// clazy:excludeall=qstring-allocations
471
472#endif // QGSGEOMETRYCOLLECTION_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
Abstract base class for all geometries.
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.
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
virtual bool fuzzyEqual(const QgsAbstractGeometry &other, double epsilon=1e-8) const =0
Performs fuzzy comparison between this geometry and other using an epsilon.
virtual bool isEmpty() const
Returns true if the geometry is empty.
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...
virtual bool operator==(const QgsAbstractGeometry &other) const =0
QgsAbstractGeometry()=default
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:42
bool fuzzyDistanceEqual(const QgsAbstractGeometry &other, double epsilon=1e-8) const override
Performs fuzzy distance comparison between this geometry and other using an epsilon.
QVector< QgsAbstractGeometry * > mGeometries
bool fromCollectionWkt(const QString &wkt, const QVector< Qgis::WkbType > &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
QgsGeometryCollection()
Constructor for an empty geometry collection.
static QgsGeometryCollection * cast(QgsAbstractGeometry *geom)
Cast the geom to a QgsGeometryCollection.
static const QgsGeometryCollection * cast(const QgsAbstractGeometry *geom)
Cast the geom to a QgsGeometryCollection.
bool operator==(const QgsAbstractGeometry &other) const override
QgsBox3D calculateBoundingBox3D() const override
Calculates the minimal 3D bounding box for the geometry.
virtual bool wktOmitChildType() const
Returns whether child type names are omitted from Wkt representations of the collection.
bool fuzzyEqual(const QgsAbstractGeometry &other, double epsilon=1e-8) const override
Performs fuzzy comparison between this geometry and other using an epsilon.
int numGeometries() const
Returns the number of geometries within the collection.
bool operator!=(const QgsAbstractGeometry &other) const override
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:240
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:179
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_THROW(name,...)
Definition qgis_sip.h:211
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QVector< QgsRingSequence > QgsCoordinateSequence
double closestSegment(const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon)
Definition qgstracer.cpp:72