QGIS API Documentation  3.17.0-Master (a035f434f4)
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 
103  SIP_PYOBJECT geometryN( int n ) SIP_TYPEHINT( QgsAbstractGeometry );
104  % MethodCode
105  if ( a0 < 0 || a0 >= sipCpp->numGeometries() )
106  {
107  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
108  sipIsErr = 1;
109  }
110  else
111  {
112  return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
113  }
114  % End
115 #endif
116 
117 
118  //methods inherited from QgsAbstractGeometry
119  bool isEmpty() const override SIP_HOLDGIL;
120  int dimension() const override SIP_HOLDGIL;
121  QString geometryType() const override SIP_HOLDGIL;
122  void clear() override;
123  QgsGeometryCollection *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
124  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
125  QgsAbstractGeometry *boundary() const override SIP_FACTORY;
126  void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const override;
127  int vertexNumberFromVertexId( QgsVertexId id ) const override;
128 
137  void reserve( int size ) SIP_HOLDGIL;
138 
140  virtual bool addGeometry( QgsAbstractGeometry *g SIP_TRANSFER );
141 
147  virtual bool insertGeometry( QgsAbstractGeometry *g SIP_TRANSFER, int index );
148 
149 #ifndef SIP_RUN
150 
156  virtual bool removeGeometry( int nr );
157 #else
158 
166  virtual bool removeGeometry( int nr );
167  % MethodCode
168  const int count = sipCpp->numGeometries();
169  if ( a0 < 0 || a0 >= count )
170  {
171  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
172  sipIsErr = 1;
173  }
174  else
175  {
176  return PyBool_FromLong( sipCpp->removeGeometry( a0 ) );
177  }
178  % End
179 #endif
180 
182  void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
183 
184  void draw( QPainter &p ) const override;
185  QPainterPath asQPainterPath() const override;
186 
187  bool fromWkb( QgsConstWkbPtr &wkb ) override;
188  bool fromWkt( const QString &wkt ) override;
189 
190  int wkbSize( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
191  QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
192  QString asWkt( int precision = 17 ) const override;
193  QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
194  QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
195  json asJsonObject( int precision = 17 ) const override SIP_SKIP;
196  QString asKml( int precision = 17 ) const override;
197 
198  QgsRectangle boundingBox() const override;
199 
200  QgsCoordinateSequence coordinateSequence() const override;
201  int nCoordinates() const override;
202 
203  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;
204  bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const override;
205 
206  //low-level editing
207  bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
208  bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
209  bool deleteVertex( QgsVertexId position ) override;
210 
211  double length() const override SIP_HOLDGIL;
212  double area() const override SIP_HOLDGIL;
213  double perimeter() const override SIP_HOLDGIL;
214 
215  bool hasCurvedSegments() const override SIP_HOLDGIL;
216 
222  QgsAbstractGeometry *segmentize( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
223 
224  double vertexAngle( QgsVertexId vertex ) const override;
225  double segmentLength( QgsVertexId startVertex ) const override;
226  int vertexCount( int part = 0, int ring = 0 ) const override;
227  int ringCount( int part = 0 ) const override;
228  int partCount() const override;
229  QgsPoint vertexAt( QgsVertexId id ) const override;
230  bool isValid( QString &error SIP_OUT, int flags = 0 ) const override;
231 
232  bool addZValue( double zValue = 0 ) override;
233  bool addMValue( double mValue = 0 ) override;
234  bool dropZValue() override;
235  bool dropMValue() override;
236  void swapXy() override;
238 
239 #ifndef SIP_RUN
240  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
241  void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) override;
242 
250  inline static const QgsGeometryCollection *cast( const QgsAbstractGeometry *geom )
251  {
252  if ( geom && QgsWkbTypes::isMultiType( geom->wkbType() ) )
253  return static_cast<const QgsGeometryCollection *>( geom );
254  return nullptr;
255  }
256 #endif
257 
258 
259 #ifdef SIP_RUN
260 
269  SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsAbstractGeometry );
270  % MethodCode
271  const int count = sipCpp->numGeometries();
272  if ( a0 < -count || a0 >= count )
273  {
274  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
275  sipIsErr = 1;
276  }
277  else if ( a0 >= 0 )
278  {
279  return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
280  }
281  else
282  {
283  return sipConvertFromType( sipCpp->geometryN( count + a0 ), sipType_QgsAbstractGeometry, NULL );
284  }
285  % End
286 
295  void __delitem__( int index );
296  % MethodCode
297  const int count = sipCpp->numGeometries();
298  if ( a0 >= 0 && a0 < count )
299  sipCpp->removeGeometry( a0 );
300  else if ( a0 < 0 && a0 >= -count )
301  sipCpp->removeGeometry( count + a0 );
302  else
303  {
304  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
305  sipIsErr = 1;
306  }
307  % End
308 
314  SIP_PYOBJECT __iter__() SIP_TYPEHINT( QgsGeometryPartIterator );
315  % MethodCode
316  sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
317  % End
318 #endif
319 
321 
322  protected:
323  int childCount() const override;
324  QgsAbstractGeometry *childGeometry( int index ) const override;
325 
326  protected:
327  QVector< QgsAbstractGeometry * > mGeometries;
328 
333  virtual bool wktOmitChildType() const;
334 
338  bool fromCollectionWkt( const QString &wkt, const QVector<QgsAbstractGeometry *> &subtypes, const QString &defaultChildWkbType = QString() );
339 
340  QgsRectangle calculateBoundingBox() const override;
341  void clearCache() const override;
342 
343  private:
344 
345  mutable QgsRectangle mBoundingBox;
346  mutable bool mHasCachedValidity = false;
347  mutable QString mValidityFailureReason;
348 };
349 
350 // clazy:excludeall=qstring-allocations
351 
352 #endif // QGSGEOMETRYCOLLECTION_H
virtual QString asWkt(int precision=17) const =0
Returns a WKT representation of the geometry.
int precision
A rectangle specified with double values.
Definition: qgsrectangle.h:41
virtual QgsAbstractGeometry * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0) const =0
Makes a new geometry with all the points or vertices snapped to the closest point of the grid...
Java-style iterator for traversal of parts of a geometry.
virtual int vertexNumberFromVertexId(QgsVertexId id) const =0
Returns the vertex number corresponding to a vertex id.
virtual int childCount() const
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
virtual bool isEmpty() const
Returns true if the geometry is empty.
int numGeometries() const SIP_HOLDGIL
Returns the number of geometries within the collection.
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 bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within the geometry.
virtual bool isValid(QString &error, int flags=0) const =0
Checks validity of the geometry, and returns true if the geometry is valid.
virtual void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)=0
Transforms the geometry using a coordinate transform.
virtual QgsAbstractGeometry * toCurveType() const =0
Returns the geometry converted to the more generic curve type.
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
virtual bool insertVertex(QgsVertexId position, const QgsPoint &vertex)=0
Inserts a vertex into the geometry.
QVector< QgsRingSequence > QgsCoordinateSequence
virtual bool operator==(const QgsAbstractGeometry &other) const =0
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
virtual double vertexAngle(QgsVertexId vertex) const =0
Returns approximate angle at a vertex.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
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 * childGeometry(int index) const
Returns pointer to child geometry (for geometries with child geometries - i.e.
virtual QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML2 representation of the geometry.
virtual QgsAbstractGeometry * boundary() const =0
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
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
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
virtual bool operator!=(const QgsAbstractGeometry &other) const =0
virtual double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf=nullptr, double epsilon=4 *std::numeric_limits< double >::epsilon()) const =0
Searches for the closest segment of the geometry to a given point.
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
virtual double segmentLength(QgsVertexId startVertex) const =0
Returns the length of the segment of the geometry which begins at startVertex.
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...
Utility class for identifying a unique vertex within a geometry.
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
Geometry collection.
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
virtual QString asKml(int precision=17) const =0
Returns a KML representation of the geometry.
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
virtual int ringCount(int part=0) const =0
Returns the number of rings of which this geometry is built.
#define SIP_FACTORY
Definition: qgis_sip.h:76
Abstract base class for all geometries.
virtual int dimension() const =0
Returns the inherent dimension of the geometry.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
AxisOrder
Axis order for GML generation.
virtual int wkbSize(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const =0
Returns the length of the QByteArray returned by asWkb()
virtual QByteArray asWkb(WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const =0
Returns a WKB representation of the geometry.
virtual QgsCoordinateSequence coordinateSequence() const =0
Retrieves the sequence of geometries, rings and nodes.
virtual void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const =0
Returns the vertices adjacent to a specified vertex within a geometry.
QVector< QgsAbstractGeometry *> mGeometries
virtual void clear()=0
Clears the geometry, ie reset it to a null geometry.
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
#define SIP_OUT
Definition: qgis_sip.h:58
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
virtual double perimeter() const
Returns the planar, 2-dimensional perimeter of the geometry.
virtual int vertexCount(int part=0, int ring=0) const =0
Returns the number of vertices of which this geometry is built.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
virtual QgsRectangle calculateBoundingBox() const
Default calculator for the minimal bounding box for the geometry.
Class for doing transforms between two map coordinate systems.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
#define SIP_THROW(name)
Definition: qgis_sip.h:189
Transform from source to destination CRS.
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:832
virtual bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false)=0
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
static const QgsGeometryCollection * cast(const QgsAbstractGeometry *geom)
Cast the geom to a QgsGeometryCollection.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
virtual bool fromWkt(const QString &wkt)=0
Sets the geometry from a WKT string.
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
virtual bool hasCurvedSegments() const
Returns true if the geometry contains curved segments.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
virtual void swapXy()=0
Swaps the x and y coordinates from the geometry.
virtual QPainterPath asQPainterPath() const =0
Returns the geometry represented as a QPainterPath.
virtual json asJsonObject(int precision=17) const
Returns a json object representation of the geometry.
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;. Negative values mean left ...
Definition: MathUtils.cpp:292
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
virtual int partCount() const =0
Returns count of parts contained in the geometry.
virtual QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML3 representation of the geometry.
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
virtual bool fromWkb(QgsConstWkbPtr &wkb)=0
Sets the geometry from a WKB string.