QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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:
41  ~QgsGeometryCollection() override;
42 
43  bool operator==( const QgsAbstractGeometry &other ) const override;
44  bool operator!=( const QgsAbstractGeometry &other ) const override;
45 
46  QgsGeometryCollection *clone() const override SIP_FACTORY;
47 
51  int numGeometries() const
52  {
53  return mGeometries.size();
54  }
55 
56 #ifdef SIP_RUN
57 
61  int __len__() const;
62  % MethodCode
63  sipRes = sipCpp->numGeometries();
64  % End
65 
67  int __bool__() const;
68  % MethodCode
69  sipRes = true;
70  % End
71 #endif
72 
73 
79  const QgsAbstractGeometry *geometryN( int n ) const SIP_SKIP
80  {
81  return mGeometries.value( n );
82  }
83 
84 #ifndef SIP_RUN
85 
90  QgsAbstractGeometry *geometryN( int n );
91 #else
92 
97  SIP_PYOBJECT geometryN( int n ) SIP_TYPEHINT( QgsAbstractGeometry );
98  % MethodCode
99  if ( a0 < 0 || a0 >= sipCpp->numGeometries() )
100  {
101  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
102  sipIsErr = 1;
103  }
104  else
105  {
106  return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
107  }
108  % End
109 #endif
110 
111 
112  //methods inherited from QgsAbstractGeometry
113  bool isEmpty() const override;
114  int dimension() const override;
115  QString geometryType() const override;
116  void clear() override;
117  QgsGeometryCollection *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
118  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
119  QgsAbstractGeometry *boundary() const override SIP_FACTORY;
120  void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex SIP_OUT, QgsVertexId &nextVertex SIP_OUT ) const override;
121  int vertexNumberFromVertexId( QgsVertexId id ) const override;
122 
131  void reserve( int size );
132 
134  virtual bool addGeometry( QgsAbstractGeometry *g SIP_TRANSFER );
135 
141  virtual bool insertGeometry( QgsAbstractGeometry *g SIP_TRANSFER, int index );
142 
143 #ifndef SIP_RUN
144 
150  virtual bool removeGeometry( int nr );
151 #else
152 
160  virtual bool removeGeometry( int nr );
161  % MethodCode
162  const int count = sipCpp->numGeometries();
163  if ( a0 < 0 || a0 >= count )
164  {
165  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
166  sipIsErr = 1;
167  }
168  else
169  {
170  return PyBool_FromLong( sipCpp->removeGeometry( a0 ) );
171  }
172  % End
173 #endif
174 
176  void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
177 
178  void draw( QPainter &p ) const override;
179 
180  bool fromWkb( QgsConstWkbPtr &wkb ) override;
181  bool fromWkt( const QString &wkt ) override;
182  QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = QgsAbstractGeometry::WkbFlags() ) const override;
183  QString asWkt( int precision = 17 ) const override;
184  QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
185  QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
186  json asJsonObject( int precision = 17 ) const override SIP_SKIP;
187  QString asKml( int precision = 17 ) const override;
188 
189  QgsRectangle boundingBox() const override;
190 
191  QgsCoordinateSequence coordinateSequence() const override;
192  int nCoordinates() const override;
193 
194  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;
195  bool nextVertex( QgsVertexId &id, QgsPoint &vertex SIP_OUT ) const override;
196 
197  //low-level editing
198  bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
199  bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
200  bool deleteVertex( QgsVertexId position ) override;
201 
202  double length() const override;
203  double area() const override;
204  double perimeter() const override;
205 
206  bool hasCurvedSegments() const override;
207 
212  QgsAbstractGeometry *segmentize( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
213 
214  double vertexAngle( QgsVertexId vertex ) const override;
215  double segmentLength( QgsVertexId startVertex ) const override;
216  int vertexCount( int part = 0, int ring = 0 ) const override;
217  int ringCount( int part = 0 ) const override;
218  int partCount() const override;
219  QgsPoint vertexAt( QgsVertexId id ) const override;
220  bool isValid( QString &error SIP_OUT, int flags = 0 ) const override;
221 
222  bool addZValue( double zValue = 0 ) override;
223  bool addMValue( double mValue = 0 ) override;
224  bool dropZValue() override;
225  bool dropMValue() override;
226  void swapXy() override;
227  QgsGeometryCollection *toCurveType() const override SIP_FACTORY;
228 
229 #ifndef SIP_RUN
230  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
231  void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) override;
232 
240  inline const QgsGeometryCollection *cast( const QgsAbstractGeometry *geom ) const
241  {
242  if ( geom && QgsWkbTypes::isMultiType( geom->wkbType() ) )
243  return static_cast<const QgsGeometryCollection *>( geom );
244  return nullptr;
245  }
246 #endif
247 
248 
249 #ifdef SIP_RUN
250 
259  SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsAbstractGeometry );
260  % MethodCode
261  const int count = sipCpp->numGeometries();
262  if ( a0 < -count || a0 >= count )
263  {
264  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
265  sipIsErr = 1;
266  }
267  else if ( a0 >= 0 )
268  {
269  return sipConvertFromType( sipCpp->geometryN( a0 ), sipType_QgsAbstractGeometry, NULL );
270  }
271  else
272  {
273  return sipConvertFromType( sipCpp->geometryN( count + a0 ), sipType_QgsAbstractGeometry, NULL );
274  }
275  % End
276 
285  void __delitem__( int index );
286  % MethodCode
287  const int count = sipCpp->numGeometries();
288  if ( a0 >= 0 && a0 < count )
289  sipCpp->removeGeometry( a0 );
290  else if ( a0 < 0 && a0 >= -count )
291  sipCpp->removeGeometry( count + a0 );
292  else
293  {
294  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
295  sipIsErr = 1;
296  }
297  % End
298 
304  SIP_PYOBJECT __iter__() SIP_TYPEHINT( QgsGeometryPartIterator );
305  % MethodCode
306  sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
307  % End
308 #endif
309 
311 
312  protected:
313  int childCount() const override;
314  QgsAbstractGeometry *childGeometry( int index ) const override;
315 
316  protected:
317  QVector< QgsAbstractGeometry * > mGeometries;
318 
323  virtual bool wktOmitChildType() const;
324 
328  bool fromCollectionWkt( const QString &wkt, const QVector<QgsAbstractGeometry *> &subtypes, const QString &defaultChildWkbType = QString() );
329 
330  QgsRectangle calculateBoundingBox() const override;
331  void clearCache() const override;
332 
333  private:
334 
335  mutable QgsRectangle mBoundingBox;
336  mutable bool mHasCachedValidity = false;
337  mutable QString mValidityFailureReason;
338 };
339 
340 // clazy:excludeall=qstring-allocations
341 
342 #endif // QGSGEOMETRYCOLLECTION_H
QgsCoordinateSequence
QVector< QgsRingSequence > QgsCoordinateSequence
Definition: qgsabstractgeometry.h:49
QgsAbstractGeometry::wkbType
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Definition: qgsabstractgeometry.h:189
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsAbstractGeometry::snappedToGrid
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.
QgsGeometryCollection::numGeometries
int numGeometries() const
Returns the number of geometries within the collection.
Definition: qgsgeometrycollection.h:51
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsAbstractGeometry::filterVertices
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...
Definition: qgsabstractgeometry.cpp:250
QgsCoordinateTransform::TransformDirection
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
Definition: qgscoordinatetransform.h:58
QgsWkbTypes::isMultiType
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:831
closestSegment
double closestSegment(const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon)
Definition: qgstracer.cpp:68
QgsRectangle
Definition: qgsrectangle.h:41
SIP_TYPEHINT
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:213
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
MathUtils::leftOf
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
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsGeometryCollection
Geometry collection.
Definition: qgsgeometrycollection.h:35
QgsGeometryCollection::cast
const QgsGeometryCollection * cast(const QgsAbstractGeometry *geom) const
Cast the geom to a QgsGeometryCollection.
Definition: qgsgeometrycollection.h:240
QgsAbstractGeometry::dimension
virtual int dimension() const =0
Returns the inherent dimension of the geometry.
QgsCsException
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsConstWkbPtr
Definition: qgswkbptr.h:127
QgsAbstractGeometry::clone
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsAbstractGeometry::clear
virtual void clear()=0
Clears the geometry, ie reset it to a null geometry.
QgsAbstractGeometry::isEmpty
virtual bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgsabstractgeometry.cpp:298
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
QgsCoordinateTransform::ForwardTransform
@ ForwardTransform
Transform from source to destination CRS.
Definition: qgscoordinatetransform.h:60
QgsAbstractGeometry::geometryType
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
QgsAbstractGeometry::createEmptyWithSameType
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
QgsGeometryCollection::geometryN
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Definition: qgsgeometrycollection.h:79
QgsAbstractGeometry::operator=
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
Definition: qgsabstractgeometry.cpp:33
c
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
Definition: porting_processing.dox:1
QgsAbstractGeometry::operator!=
virtual bool operator!=(const QgsAbstractGeometry &other) const =0
QgsGeometryPartIterator
Java-style iterator for traversal of parts of a geometry.
Definition: qgsabstractgeometry.h:1161
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1033
QgsAbstractGeometry::transformVertices
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.
Definition: qgsabstractgeometry.cpp:255
QgsAbstractGeometry::operator==
virtual bool operator==(const QgsAbstractGeometry &other) const =0
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
QgsAbstractGeometry::transform
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.
qgsabstractgeometry.h