Class: QgsGeometryCollection

class qgis.core.QgsGeometryCollection

Bases: QgsAbstractGeometry

Geometry collection

QgsGeometryCollection() Constructor for an empty geometry collection.

QgsGeometryCollection(c: QgsGeometryCollection)

Methods

addGeometries

Adds a list of geometries to the collection, transferring ownership to the collection.

addGeometry

Adds a geometry and takes ownership.

addMValue

param mValue:

addZValue

param zValue:

adjacentVertices

param vertex:

area

rtype:

float

asGml2

param doc:

asGml3

param doc:

asKml

param precision:

asQPainterPath

rtype:

QPainterPath

asWkb

param flags:

asWkt

param precision:

boundary

rtype:

Optional[QgsAbstractGeometry]

boundingBox3D

rtype:

QgsBox3D

boundingBoxIntersects

param box3d:

calculateBoundingBox

Default calculator for the minimal bounding box for the geometry.

calculateBoundingBox3D

rtype:

QgsBox3D

childCount

rtype:

int

childGeometry

param index:

childPoint

Returns point at index (for geometries without child geometries - i.e. curve / point).

clear

clearCache

clone

rtype:

Optional[QgsGeometryCollection]

closestSegment

param pt:

compareToSameClass

param other:

coordinateSequence

rtype:

Any

createEmptyWithSameType

rtype:

Optional[QgsGeometryCollection]

deleteVertex

param position:

dimension

rtype:

int

draw

param p:

dropMValue

rtype:

bool

dropZValue

rtype:

bool

extractPartsByType

Returns a new QgsGeometryCollection subclass which consists of the parts of this collection which match the specified WKB type.

fromCollectionWkt

Reads a collection from a WKT string.

fromWkb

param wkb:

fromWkt

param wkt:

fuzzyDistanceEqual

param other:

fuzzyEqual

param other:

geometryN

Returns a geometry from within the collection.

geometryType

rtype:

str

hasChildGeometries

Returns whether the geometry has any child geometries (False for point / curve, True otherwise)

hasCurvedSegments

rtype:

bool

insertGeometry

Inserts a geometry before a specified index and takes ownership.

insertVertex

param position:

isEmpty

rtype:

bool

isValid

param flags:

length

rtype:

float

moveVertex

param position:

nCoordinates

rtype:

int

nextVertex

param id:

normalize

numGeometries

Returns the number of geometries within the collection.

partCount

rtype:

int

perimeter

rtype:

float

removeDuplicateNodes

param epsilon:

removeGeometry

Removes a geometry from the collection by index.

reserve

Attempts to allocate memory for at least size geometries.

ringCount

param part:

segmentLength

param startVertex:

segmentize

Returns a geometry without curves.

setZMTypeFromSubGeometry

Updates the geometry type based on whether sub geometries contain z or m values.

simplifiedTypeRef

rtype:

Optional[QgsAbstractGeometry]

snappedToGrid

param hSpacing:

sortIndex

Returns the sort index for the geometry, used in the compareTo() method to compare geometries of different types.

swapXy

takeGeometries

Removes all geometries from the collection, returning them and their ownership to the caller.

toCurveType

rtype:

Optional[QgsGeometryCollection]

transform

param ct:

vertexAngle

param vertex:

vertexAt

param id:

vertexCount

param part:

vertexNumberFromVertexId

param id:

wkbSize

param flags:

wktOmitChildType

Returns whether child type names are omitted from Wkt representations of the collection

addGeometries(self, geometries: Iterable[QgsAbstractGeometry]) bool

Adds a list of geometries to the collection, transferring ownership to the collection.

Returns True in case of success.

Added in version 3.38.

Parameters:

geometries (Iterable[QgsAbstractGeometry])

Return type:

bool

addGeometry(self, g: QgsAbstractGeometry | None) bool

Adds a geometry and takes ownership. Returns True in case of success.

Parameters:

g (Optional[QgsAbstractGeometry])

Return type:

bool

addMValue(self, mValue: float = 0) bool
Parameters:

mValue (float = 0)

Return type:

bool

addZValue(self, zValue: float = 0) bool
Parameters:

zValue (float = 0)

Return type:

bool

adjacentVertices(self, vertex: QgsVertexId)
Parameters:

vertex (QgsVertexId) -> (QgsVertexId)

area(self) float
Return type:

float

asGml2(self, doc: QDomDocument, precision: int = 17, ns: str | None = '', axisOrder: QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY) QDomElement
Parameters:
  • doc (QDomDocument)

  • precision (int = 17)

  • ns (Optional[str] = '')

  • axisOrder (QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY)

Return type:

QDomElement

asGml3(self, doc: QDomDocument, precision: int = 17, ns: str | None = '', axisOrder: QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY) QDomElement
Parameters:
  • doc (QDomDocument)

  • precision (int = 17)

  • ns (Optional[str] = '')

  • axisOrder (QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY)

Return type:

QDomElement

asKml(self, precision: int = 17) str
Parameters:

precision (int = 17)

Return type:

str

asQPainterPath(self) QPainterPath
Return type:

QPainterPath

asWkb(self, flags: QgsAbstractGeometry.WkbFlags | QgsAbstractGeometry.WkbFlag = QgsAbstractGeometry.WkbFlags()) QByteArray
Parameters:

flags (Union[QgsAbstractGeometry.WkbFlags)

Return type:

QByteArray

asWkt(self, precision: int = 17) str
Parameters:

precision (int = 17)

Return type:

str

boundary(self) QgsAbstractGeometry | None
Return type:

Optional[QgsAbstractGeometry]

boundingBox3D(self) QgsBox3D
Return type:

QgsBox3D

boundingBoxIntersects(self, box3d: QgsBox3D) bool
Parameters:

box3d (QgsBox3D)

Return type:

bool

calculateBoundingBox(self) QgsRectangle

Default calculator for the minimal bounding box for the geometry. Derived classes should override this method if a more efficient bounding box calculation is available.

calculateBoundingBox3D(self) QgsBox3D
Return type:

QgsBox3D

childCount(self) int
Return type:

int

childGeometry(self, index: int) QgsAbstractGeometry | None
Parameters:

index (int)

Return type:

Optional[QgsAbstractGeometry]

childPoint(self, index: int) QgsPoint

Returns point at index (for geometries without child geometries - i.e. curve / point)

Note

used for vertex_iterator implementation

clear(self)
clearCache(self)
clone(self) QgsGeometryCollection | None
Return type:

Optional[QgsGeometryCollection]

closestSegment(self, pt: QgsPoint, epsilon: float = 4 * DBL_EPSILON)
Parameters:
  • pt (QgsPoint)

  • epsilon (float = 4*DBL_EPSILON) -> (float)

compareToSameClass(self, other: QgsAbstractGeometry | None) int
Parameters:

other (Optional[QgsAbstractGeometry])

Return type:

int

coordinateSequence(self) Any
Return type:

Any

createEmptyWithSameType(self) QgsGeometryCollection | None
Return type:

Optional[QgsGeometryCollection]

deleteVertex(self, position: QgsVertexId) bool
Parameters:

position (QgsVertexId)

Return type:

bool

dimension(self) int
Return type:

int

draw(self, p: QPainter)
Parameters:

p (QPainter)

dropMValue(self) bool
Return type:

bool

dropZValue(self) bool
Return type:

bool

extractPartsByType(self, type: Qgis.WkbType, useFlatType: bool = True) QgsGeometryCollection | None

Returns a new QgsGeometryCollection subclass which consists of the parts of this collection which match the specified WKB type.

For instance, if type is Qgis.WkbType.Polygon, then the returned object will be a QgsMultiPolygon object containing just the polygons from this collection.

If useFlatType is True, then the WKB types of component geometries from this collection will be flattened prior to comparing with type. (I.e. the presence of Z / M dimensions will be ignored when comparing against type).

Added in version 3.36.

Parameters:
Return type:

Optional[QgsGeometryCollection]

fromCollectionWkt(self, wkt: str | None, subtypes: Iterable[QgsAbstractGeometry], defaultChildWkbType: str | None = '') bool

Reads a collection from a WKT string.

Parameters:
  • wkt (Optional[str])

  • subtypes (Iterable[QgsAbstractGeometry])

  • defaultChildWkbType (Optional[str] = '')

Return type:

bool

fromWkb(self, wkb: QgsConstWkbPtr) bool
Parameters:

wkb (QgsConstWkbPtr)

Return type:

bool

fromWkt(self, wkt: str | None) bool
Parameters:

wkt (Optional[str])

Return type:

bool

fuzzyDistanceEqual(self, other: QgsAbstractGeometry, epsilon: float = 1e-08) bool
Parameters:
Return type:

bool

fuzzyEqual(self, other: QgsAbstractGeometry, epsilon: float = 1e-08) bool
Parameters:
Return type:

bool

geometryN(self, n: int) QgsAbstractGeometry

Returns a geometry from within the collection.

Parameters:

n (int) – index of geometry to return.

Raises:

IndexError – if no geometry with the specified index exists.

Return type:

QgsAbstractGeometry

geometryType(self) str
Return type:

str

hasChildGeometries(self) bool

Returns whether the geometry has any child geometries (False for point / curve, True otherwise)

Note

used for vertex_iterator implementation

hasCurvedSegments(self) bool
Return type:

bool

insertGeometry(self, g: QgsAbstractGeometry | None, index: int) bool

Inserts a geometry before a specified index and takes ownership. Returns True in case of success.

Parameters:
  • g (Optional[QgsAbstractGeometry]) – geometry to insert. Ownership is transferred to the collection.

  • index (int) – position to insert geometry before

Return type:

bool

insertVertex(self, position: QgsVertexId, vertex: QgsPoint) bool
Parameters:
Return type:

bool

isEmpty(self) bool
Return type:

bool

isValid(self, flags: Qgis.GeometryValidityFlags | Qgis.GeometryValidityFlag = Qgis.GeometryValidityFlags())
Parameters:

flags (Union[Qgis.GeometryValidityFlags)

length(self) float
Return type:

float

moveVertex(self, position: QgsVertexId, newPos: QgsPoint) bool
Parameters:
Return type:

bool

nCoordinates(self) int
Return type:

int

nextVertex(self, id: QgsVertexId)
Parameters:

id (QgsVertexId) -> (bool)

normalize(self)
numGeometries(self) int

Returns the number of geometries within the collection.

Return type:

int

partCount(self) int
Return type:

int

perimeter(self) float
Return type:

float

removeDuplicateNodes(self, epsilon: float = 4 * DBL_EPSILON, useZValues: bool = False) bool
Parameters:
  • epsilon (float = 4*DBL_EPSILON)

  • useZValues (bool = False)

Return type:

bool

removeGeometry(self, nr: int) bool

Removes a geometry from the collection by index.

Return type:

bool

Returns:

True if removal was successful.

Raises:

IndexError – if no geometry with the specified index exists.

Parameters:

nr (int)

reserve(self, size: int)

Attempts to allocate memory for at least size geometries.

If the number of geometries is known in advance, calling this function prior to adding geometries will prevent reallocations and memory fragmentation.

Added in version 3.10.

Parameters:

size (int)

ringCount(self, part: int = 0) int
Parameters:

part (int = 0)

Return type:

int

segmentLength(self, startVertex: QgsVertexId) float
Parameters:

startVertex (QgsVertexId)

Return type:

float

segmentize(self, tolerance: float = M_PI_2 / 90, toleranceType: QgsAbstractGeometry.SegmentationToleranceType = QgsAbstractGeometry.MaximumAngle) QgsAbstractGeometry | None

Returns a geometry without curves. Caller takes ownership

Parameters:
  • tolerance (float = M_PI_2/90) – segmentation tolerance

  • toleranceType (QgsAbstractGeometry.SegmentationToleranceType = QgsAbstractGeometry.MaximumAngle) – maximum segmentation angle or maximum difference between approximation and curve

Return type:

Optional[QgsAbstractGeometry]

setZMTypeFromSubGeometry(self, subggeom: QgsAbstractGeometry | None, baseGeomType: Qgis.WkbType)

Updates the geometry type based on whether sub geometries contain z or m values.

simplifiedTypeRef(self) QgsAbstractGeometry | None
Return type:

Optional[QgsAbstractGeometry]

snappedToGrid(self, hSpacing: float, vSpacing: float, dSpacing: float = 0, mSpacing: float = 0, removeRedundantPoints: bool = False) QgsGeometryCollection | None
Parameters:
  • hSpacing (float)

  • vSpacing (float)

  • dSpacing (float = 0)

  • mSpacing (float = 0)

  • removeRedundantPoints (bool = False)

Return type:

Optional[QgsGeometryCollection]

sortIndex(self) int

Returns the sort index for the geometry, used in the compareTo() method to compare geometries of different types.

Added in version 3.20.

swapXy(self)
takeGeometries(self) List[QgsAbstractGeometry]

Removes all geometries from the collection, returning them and their ownership to the caller.

Added in version 3.38.

Return type:

List[QgsAbstractGeometry]

toCurveType(self) QgsGeometryCollection | None
Return type:

Optional[QgsGeometryCollection]

transform(self, ct: QgsCoordinateTransform, d: Qgis.TransformDirection = Qgis.TransformDirection.Forward, transformZ: bool = False)
transform(self, t: QTransform, zTranslate: float = 0, zScale: float = 1, mTranslate: float = 0, mScale: float = 1) None
transform(self, transformer: QgsAbstractGeometryTransformer | None, feedback: QgsFeedback | None = None) bool
Parameters:
  • ct (QgsCoordinateTransform)

  • d (Qgis.TransformDirection = Qgis.TransformDirection.Forward)

  • transformZ (bool = False)

vertexAngle(self, vertex: QgsVertexId) float
Parameters:

vertex (QgsVertexId)

Return type:

float

vertexAt(self, id: QgsVertexId) QgsPoint
Parameters:

id (QgsVertexId)

Return type:

QgsPoint

vertexCount(self, part: int = 0, ring: int = 0) int
Parameters:
  • part (int = 0)

  • ring (int = 0)

Return type:

int

vertexNumberFromVertexId(self, id: QgsVertexId) int
Parameters:

id (QgsVertexId)

Return type:

int

wkbSize(self, flags: QgsAbstractGeometry.WkbFlags | QgsAbstractGeometry.WkbFlag = QgsAbstractGeometry.WkbFlags()) int
Parameters:

flags (Union[QgsAbstractGeometry.WkbFlags)

Return type:

int

wktOmitChildType(self) bool

Returns whether child type names are omitted from Wkt representations of the collection

Return type:

bool