Skip to content

Commit e6ec1ec

Browse files
committed
[FEATURE][API] Add iterator for QgsGeometryCollection
Iterates over the geometries in the collection, allowing this type of code: gc = QgsGeometryCollection() gc.fromWkt('GeometryCollection( Point(1 2), Point(11 12), LineString(33 34, 44 45))') for part in gc: print(part.asWkt())
1 parent e23527b commit e6ec1ec

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

python/core/auto_generated/geometry/qgsgeometrycollection.sip.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ corresponds to the last geometry in the collection.
270270
}
271271
%End
272272

273+
SIP_PYOBJECT __iter__() /TypeHint="QgsGeometryPartIterator"/;
274+
%Docstring
275+
Iterates through all geometries in the collection.
276+
277+
.. versionadded:: 3.6
278+
%End
279+
%MethodCode
280+
sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
281+
%End
282+
273283
virtual QgsGeometryCollection *createEmptyWithSameType() const /Factory/;
274284

275285

src/core/geometry/qgsgeometrycollection.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
277277
sipIsErr = 1;
278278
}
279279
% End
280+
281+
/**
282+
* Iterates through all geometries in the collection.
283+
*
284+
* \since QGIS 3.6
285+
*/
286+
SIP_PYOBJECT __iter__() SIP_TYPEHINT( QgsGeometryPartIterator );
287+
% MethodCode
288+
sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
289+
% End
280290
#endif
281291

282292
QgsGeometryCollection *createEmptyWithSameType() const override SIP_FACTORY;

tests/src/python/test_qgsgeometry.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,12 @@ def testGeometryCollectionPythonAdditions(self):
498498
with self.assertRaises(IndexError):
499499
del g[-3]
500500

501+
# iteration
502+
g = QgsGeometryCollection()
503+
self.assertFalse([p for p in g])
504+
g.fromWkt('GeometryCollection( Point(1 2), Point(11 12), LineString(33 34, 44 45))')
505+
self.assertEqual([p.asWkt() for p in g], ['Point (1 2)', 'Point (11 12)', 'LineString (33 34, 44 45)'])
506+
501507
def testReferenceGeometry(self):
502508
""" Test parsing a whole range of valid reference wkt formats and variants, and checking
503509
expected values such as length, area, centroids, bounding boxes, etc of the resultant geometry.

0 commit comments

Comments
 (0)