QGIS API Documentation  3.21.0-Master (909859188c)
qgsgeometrycollection.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometrycollection.h
3  -------------------------------------------------------------------
4 Date : 28 Oct 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 QGSGEOMETRYCOLLECTION_H
17 #define QGSGEOMETRYCOLLECTION_H
18 
19 #include <QVector>
20 
21 
22 #include "qgis_core.h"
23 #include "qgis_sip.h"
24 #include "qgsabstractgeometry.h"
25 
26 class QgsPoint;
27 
28 
35 class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
36 {
37  public:
38 
39 
44 
47  ~QgsGeometryCollection() override;
48 
49  bool operator==( const QgsAbstractGeometry &other ) const override;
50  bool operator!=( const QgsAbstractGeometry &other ) const override;
51 
52  QgsGeometryCollection *clone() const override SIP_FACTORY;
53 
58  {
59  return mGeometries.size();
60  }
61 
62 #ifdef SIP_RUN
63 
67  int __len__() const;
68  % MethodCode
69  sipRes = sipCpp->numGeometries();
70  % End
71 
73  int __bool__() const;
74  % MethodCode
75  sipRes = true;
76  % End
77 #endif
78 
79 
85  const QgsAbstractGeometry *geometryN( int n ) const SIP_SKIP
86  {
87  return mGeometries.value( n );
88  }
89 
90 #ifndef SIP_RUN
91 
96  QgsAbstractGeometry *geometryN( int n ) SIP_HOLDGIL;
97 #else
98 
104  SIP_PYOBJECT geometryN( int n ) SIP_TYPEHINT( QgsAbstractGeometry );
105  % MethodCode
106  if ( a0 < 0 || a0 >= sipCpp->numGeometries() )
107  {
108  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
109  sipIsErr = 1;
110  }
111  else
112  {
113  return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
114  }
115  % End
116 #endif
117 
118 
119  //methods inherited from QgsAbstractGeometry
120  bool isEmpty() const override SIP_HOLDGIL;
121  int dimension() const override SIP_HOLDGIL;
122  QString geometryType() const override SIP_HOLDGIL;
123  void clear() override;
124  QgsGeometryCollection *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
125  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
126  QgsAbstractGeometry *boundary() const override SIP_FACTORY;
127  void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const override;
128  int vertexNumberFromVertexId( QgsVertexId id ) const override;
129  bool boundingBoxIntersects( const QgsRectangle &rectangle ) const override SIP_HOLDGIL;
130 
139  void reserve( int size ) SIP_HOLDGIL;
140 
142  virtual bool addGeometry( QgsAbstractGeometry *g SIP_TRANSFER );
143 
149  virtual bool insertGeometry( QgsAbstractGeometry *g SIP_TRANSFER, int index );
150 
151 #ifndef SIP_RUN
152 
158  virtual bool removeGeometry( int nr );
159 #else
160 
167  virtual bool removeGeometry( int nr );
168  % MethodCode
169  const int count = sipCpp->numGeometries();
170  if ( a0 < 0 || a0 >= count )
171  {
172  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
173  sipIsErr = 1;
174  }
175  else
176  {
177  return PyBool_FromLong( sipCpp->removeGeometry( a0 ) );
178  }
179  % End
180 #endif
181 
182  void normalize() final SIP_HOLDGIL;
183  void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform, bool transformZ = false ) override SIP_THROW( QgsCsException );
184  void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
185 
186  void draw( QPainter &p ) const override;
187  QPainterPath asQPainterPath() const override;
188 
189  bool fromWkb( QgsConstWkbPtr &wkb ) override;
190  bool fromWkt( const QString &wkt ) override;
191 
192  int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
193  QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
194  QString asWkt( int precision = 17 ) const override;
195  QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
196  QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
197  json asJsonObject( int precision = 17 ) const override SIP_SKIP;
198  QString asKml( int precision = 17 ) const override;
199 
200  QgsRectangle boundingBox() const override;
201 
202  QgsCoordinateSequence coordinateSequence() const override;
203  int nCoordinates() const override;
204 
205  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;
206  bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const override;
207 
208  //low-level editing
209  bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
210  bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
211  bool deleteVertex( QgsVertexId position ) override;
212 
213  double length() const override SIP_HOLDGIL;
214  double area() const override SIP_HOLDGIL;
215  double perimeter() const override SIP_HOLDGIL;
216 
217  bool hasCurvedSegments() const override SIP_HOLDGIL;
218 
224  QgsAbstractGeometry *segmentize( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
225 
226  double vertexAngle( QgsVertexId vertex ) const override;
227  double segmentLength( QgsVertexId startVertex ) const override;
228  int vertexCount( int part = 0, int ring = 0 ) const override;
229  int ringCount( int part = 0 ) const override;
230  int partCount() const override;
231  QgsPoint vertexAt( QgsVertexId id ) const override;
232  bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const override;
233 
234  bool addZValue( double zValue = 0 ) override;
235  bool addMValue( double mValue = 0 ) override;
236  bool dropZValue() override;
237  bool dropMValue() override;
238  void swapXy() override;
239  QgsGeometryCollection *toCurveType() const override SIP_FACTORY;
240  const QgsAbstractGeometry *simplifiedTypeRef() const override SIP_HOLDGIL;
241 
242  bool transform( QgsAbstractGeometryTransformer *transformer, QgsFeedback *feedback = nullptr ) override;
243 
244 #ifndef SIP_RUN
245  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
246  void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) override;
247 
255  inline static const QgsGeometryCollection *cast( const QgsAbstractGeometry *geom )
256  {
257  if ( geom && QgsWkbTypes::isMultiType( geom->wkbType() ) )
258  return static_cast<const QgsGeometryCollection *>( geom );
259  return nullptr;
260  }
261 #endif
262 
263 
264 #ifdef SIP_RUN
265 
276  SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsAbstractGeometry );
277  % MethodCode
278  const int count = sipCpp->numGeometries();
279  if ( a0 < -count || a0 >= count )
280  {
281  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
282  sipIsErr = 1;
283  }
284  else if ( a0 >= 0 )
285  {
286  return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
287  }
288  else
289  {
290  return sipConvertFromType( sipCpp->geometryN( count + a0 ), sipType_QgsAbstractGeometry, NULL );
291  }
292  % End
293 
304  void __delitem__( int index );
305  % MethodCode
306  const int count = sipCpp->numGeometries();
307  if ( a0 >= 0 && a0 < count )
308  sipCpp->removeGeometry( a0 );
309  else if ( a0 < 0 && a0 >= -count )
310  sipCpp->removeGeometry( count + a0 );
311  else
312  {
313  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
314  sipIsErr = 1;
315  }
316  % End
317 
323  SIP_PYOBJECT __iter__() SIP_TYPEHINT( QgsGeometryPartIterator );
324  % MethodCode
325  sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
326  % End
327 #endif
328 
330 
331  protected:
332  int childCount() const override;
333  QgsAbstractGeometry *childGeometry( int index ) const override;
334  int compareToSameClass( const QgsAbstractGeometry *other ) const final;
335 
336  protected:
337  QVector< QgsAbstractGeometry * > mGeometries;
338 
343  virtual bool wktOmitChildType() const;
344 
348  bool fromCollectionWkt( const QString &wkt, const QVector<QgsAbstractGeometry *> &subtypes, const QString &defaultChildWkbType = QString() );
349 
350  QgsRectangle calculateBoundingBox() const override;
351  void clearCache() const override;
352 
353  private:
354 
355  mutable QgsRectangle mBoundingBox;
356  mutable bool mHasCachedValidity = false;
357  mutable QString mValidityFailureReason;
358 };
359 
360 // clazy:excludeall=qstring-allocations
361 
362 #endif // QGSGEOMETRYCOLLECTION_H
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:63
An abstract base class for classes which transform geometries by transforming input points to output ...
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.
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
virtual bool operator!=(const QgsAbstractGeometry &other) const =0
virtual bool isEmpty() const
Returns true if the geometry is empty.
virtual void normalize()=0
Reorganizes the geometry into a normalized form (or "canonical" form).
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...
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
virtual bool operator==(const QgsAbstractGeometry &other) const =0
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.
Definition: qgsexception.h:66
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Geometry collection.
static const QgsGeometryCollection * cast(const QgsAbstractGeometry *geom)
Cast the geom to a QgsGeometryCollection.
int numGeometries() const SIP_HOLDGIL
Returns the number of geometries within the collection.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Java-style iterator for traversal of parts of a geometry.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
A rectangle specified with double values.
Definition: qgsrectangle.h:42
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:832
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const QgsPoint *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'. Negative values mean left ...
Definition: MathUtils.cpp:292
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_THROW(name)
Definition: qgis_sip.h:189
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
#define SIP_FACTORY
Definition: qgis_sip.h:76
QVector< QgsRingSequence > QgsCoordinateSequence
double closestSegment(const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon)
Definition: qgstracer.cpp:68
int precision
Utility class for identifying a unique vertex within a geometry.