Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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())
  • Loading branch information
nyalldawson committed Dec 6, 2018
1 parent e23527b commit e6ec1ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/core/auto_generated/geometry/qgsgeometrycollection.sip.in
Expand Up @@ -270,6 +270,16 @@ corresponds to the last geometry in the collection.
}
%End

SIP_PYOBJECT __iter__() /TypeHint="QgsGeometryPartIterator"/;
%Docstring
Iterates through all geometries in the collection.

.. versionadded:: 3.6
%End
%MethodCode
sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
%End

virtual QgsGeometryCollection *createEmptyWithSameType() const /Factory/;


Expand Down
10 changes: 10 additions & 0 deletions src/core/geometry/qgsgeometrycollection.h
Expand Up @@ -277,6 +277,16 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
sipIsErr = 1;
}
% End

/**
* Iterates through all geometries in the collection.
*
* \since QGIS 3.6
*/
SIP_PYOBJECT __iter__() SIP_TYPEHINT( QgsGeometryPartIterator );
% MethodCode
sipRes = sipConvertFromNewType( new QgsGeometryPartIterator( sipCpp ), sipType_QgsGeometryPartIterator, Py_None );
% End
#endif

QgsGeometryCollection *createEmptyWithSameType() const override SIP_FACTORY;
Expand Down
6 changes: 6 additions & 0 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -498,6 +498,12 @@ def testGeometryCollectionPythonAdditions(self):
with self.assertRaises(IndexError):
del g[-3]

# iteration
g = QgsGeometryCollection()
self.assertFalse([p for p in g])
g.fromWkt('GeometryCollection( Point(1 2), Point(11 12), LineString(33 34, 44 45))')
self.assertEqual([p.asWkt() for p in g], ['Point (1 2)', 'Point (11 12)', 'LineString (33 34, 44 45)'])

def testReferenceGeometry(self):
""" Test parsing a whole range of valid reference wkt formats and variants, and checking
expected values such as length, area, centroids, bounding boxes, etc of the resultant geometry.
Expand Down

0 comments on commit e6ec1ec

Please sign in to comment.