Project: QGIS

Version: 3.6.0

Feature: PyQGIS Geometry Collection API improvements

The PyQGIS API for working with geometry collections has been improved:

  • Calling removeGeometry with an invalid index will now raise an IndexError
  • Calling collection[0] will return the first geometry in the collection, collection[1] the second, etc. And negative indices return from the end of the collection, so collection[-1] returns the last geometry in the collection.
  • Geometries can be deleted by calling del collection[1] (deletes the second geometry from the collection). Also supports negative indices to count from the end of the collection.

Additionally, you can now easily iterate 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())

This feature was funded by North Road

This feature was developed by Nyall Dawson (North Road)