Skip to content
Permalink
Browse files

[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 e6ec1ec53ed478234f827d1536f6df2d0262763d
@@ -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/;


@@ -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;
@@ -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.

0 comments on commit e6ec1ec

Please sign in to comment.
You can’t perform that action at this time.