Subgroup: Abstract

Class: QgsAbstractGeometry

class qgis.core.QgsAbstractGeometry

Bases: sip.wrapper

Constructor for QgsAbstractGeometry.

QgsAbstractGeometry(geom: QgsAbstractGeometry)

Abstract base class for all geometries

New in version 2.10: Methods

addMValue Adds a measure to the geometry, initialized to a preset value.
addZValue Adds a z-dimension to the geometry, initialized to a preset value.
adjacentVertices Returns the vertices adjacent to a specified vertex within a geometry.
area Returns the area of the geometry.
asGml2 Returns a GML2 representation of the geometry.
asGml3 Returns a GML3 representation of the geometry.
asJson Returns a GeoJSON representation of the geometry.
asWkb Returns a WKB representation of the geometry.
asWkt Returns a WKT representation of the geometry.
boundary Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the geometry).
boundingBox Returns the minimal bounding box for the geometry
calculateBoundingBox Default calculator for the minimal bounding box for the geometry.
centroid Returns the centroid of the geometry
childCount Returns number of child geometries (for geometries with child geometries) or child points (for geometries without child geometries - i.e.
childGeometry Returns pointer to child geometry (for geometries with child geometries - i.e.
childPoint Returns point at index (for geometries without child geometries - i.e.
clear Clears the geometry, ie reset it to a null geometry
clearCache Clears any cached parameters associated with the geometry, e.g., bounding boxes
clone Clones the geometry by performing a deep copy
closestSegment Searches for the closest segment of the geometry to a given point.
convertTo Converts the geometry to a specified type.
coordinateSequence Retrieves the sequence of geometries, rings and nodes.
createEmptyWithSameType Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
deleteVertex Deletes a vertex within the geometry
dimension Returns the inherent dimension of the geometry.
draw Draws the geometry using the specified QPainter.
dropMValue Drops any measure values which exist in the geometry.
dropZValue Drops any z-dimensions which exist in the geometry.
fromWkb Sets the geometry from a WKB string.
fromWkt Sets the geometry from a WKT string.
geometryType Returns a unique string representing the geometry type.
hasChildGeometries Returns whether the geometry has any child geometries (false for point / curve, true otherwise)
hasCurvedSegments Returns true if the geometry contains curved segments
insertVertex Inserts a vertex into the geometry
is3D Returns true if the geometry is 3D and contains a z-value.
isEmpty Returns true if the geometry is empty
isMeasure Returns true if the geometry contains m values.
length Returns the length of the geometry.
moveVertex Moves a vertex within the geometry
nCoordinates Returns the number of nodes contained in the geometry
nextVertex Returns next vertex id and coordinates
partCount Returns count of parts contained in the geometry.
perimeter Returns the perimeter of the geometry.
removeDuplicateNodes Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerate geometry.
ringCount Returns the number of rings of which this geometry is built.
segmentLength Returns the length of the segment of the geometry which begins at startVertex.
segmentize Returns a version of the geometry without curves.
setZMTypeFromSubGeometry Updates the geometry type based on whether sub geometries contain z or m values.
snappedToGrid Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
swapXy Swaps the x and y coordinates from the geometry.
toCurveType Returns the geometry converted to the more generic curve type.
transform Transforms the geometry using a coordinate transform
vertexAngle Returns approximate angle at a vertex.
vertexAt Returns the point corresponding to a specified vertex id
vertexCount Returns the number of vertices of which this geometry is built.
vertexNumberFromVertexId Returns the vertex number corresponding to a vertex id.
vertices Returns Java-style iterator for traversal of vertices of the geometry
wkbType Returns the WKB type of the geometry.
wktTypeStr Returns the WKT type string of the geometry.

Signals

Attributes

MaximumAngle
MaximumDifference
XY
YX
staticMetaObject
class AxisOrder

Bases: int

MaximumAngle = 0
MaximumDifference = 1
class SegmentationToleranceType

Bases: int

baseClass

alias of QgsAbstractGeometry

XY = 0
YX = 1
addMValue(self, mValue: float = 0) → bool

Adds a measure to the geometry, initialized to a preset value.

Parameters:mValue – initial m-value for all nodes
Returns:true on success

See also

dropMValue()

See also

addZValue()

New in version 2.12.

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

Adds a z-dimension to the geometry, initialized to a preset value.

Parameters:zValue – initial z-value for all nodes
Returns:true on success

See also

dropZValue()

See also

addMValue()

New in version 2.12.

adjacentVertices(self, vertex: QgsVertexId) → Tuple[QgsVertexId, QgsVertexId]

Returns the vertices adjacent to a specified vertex within a geometry.

New in version 3.0.

area(self) → float

Returns the area of the geometry.

See also

length()

See also

perimeter()

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

Returns a GML2 representation of the geometry.

Parameters:
  • doc – DOM document
  • precision – number of decimal places for coordinates
  • ns – XML namespace
  • axisOrder – Axis order for generated GML

See also

asWkb()

See also

asWkt()

See also

asGml3()

See also

asJson()

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

Returns a GML3 representation of the geometry.

Parameters:
  • doc – DOM document
  • precision – number of decimal places for coordinates
  • ns – XML namespace
  • axisOrder – Axis order for generated GML

See also

asWkb()

See also

asWkt()

See also

asGml2()

See also

asJson()

asJson(self, precision: int = 17) → str

Returns a GeoJSON representation of the geometry.

Parameters:precision – number of decimal places for coordinates

See also

asWkb()

See also

asWkt()

See also

asGml2()

See also

asGml3()

asWkb(self) → QByteArray

Returns a WKB representation of the geometry.

See also

asWkt()

See also

asGml2()

See also

asGml3()

See also

asJson()

New in version 3.0.

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

Returns a WKT representation of the geometry.

Parameters:precision – number of decimal places for coordinates

See also

asWkb()

See also

asGml2()

See also

asGml3()

See also

asJson()

boundary(self) → QgsAbstractGeometry

Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the geometry). For instance, a polygon geometry will have a boundary consisting of the linestrings for each ring in the polygon.

Returns:boundary for geometry. May be null for some geometry types.

New in version 3.0.

boundingBox(self) → QgsRectangle

Returns the minimal bounding box for the geometry

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.

centroid(self) → QgsPoint

Returns the centroid of the geometry

childCount(self) → int

Returns number of child geometries (for geometries with child geometries) or child points (for geometries without child geometries - i.e. curve / point)

Note

used for vertex_iterator implementation

New in version 3.0.

childGeometry(self, index: int) → QgsAbstractGeometry

Returns pointer to child geometry (for geometries with child geometries - i.e. geom. collection / polygon)

Note

used for vertex_iterator implementation

New in version 3.0.

childPoint(self, index: int) → QgsPoint

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

Note

used for vertex_iterator implementation

New in version 3.0.

clear(self)

Clears the geometry, ie reset it to a null geometry

clearCache(self)

Clears any cached parameters associated with the geometry, e.g., bounding boxes

clone(self) → QgsAbstractGeometry

Clones the geometry by performing a deep copy

closestSegment(self, pt: QgsPoint, epsilon: float = 4*DBL_EPSILON) → Tuple[float, QgsPoint, QgsVertexId, int]

Searches for the closest segment of the geometry to a given point.

Parameters:
  • pt – specifies the point to find closest segment to
  • segmentPt – storage for the closest point within the geometry
  • vertexAfter – storage for the ID of the vertex at the end of the closest segment
  • leftOf – indicates whether the point lies on the left side of the geometry (-1 if point is to the left of the geometry, +1 if the point is to the right of the geometry, or 0 for cases where left/right could not be determined, e.g. point exactly on a line) false if point is to right of segment)
  • epsilon – epsilon for segment snapping
Returns:

squared distance to closest segment or negative value on error

convertTo(self, type: QgsWkbTypes.Type) → bool

Converts the geometry to a specified type.

Returns:true if conversion was successful

New in version 2.14.

coordinateSequence(self) → object

Retrieves the sequence of geometries, rings and nodes.

Returns:coordinate sequence
createEmptyWithSameType(self) → QgsAbstractGeometry

Creates a new geometry with the same class and same WKB type as the original and transfers ownership. To create it, the geometry is default constructed and then the WKB is changed.

See also

clone()

New in version 3.0.

deleteVertex(self, position: QgsVertexId) → bool

Deletes a vertex within the geometry

Parameters:position – vertex id for vertex to delete
Returns:true if delete was successful

See also

insertVertex()

See also

moveVertex()

dimension(self) → int

Returns the inherent dimension of the geometry. For example, this is 0 for a point geometry, 1 for a linestring and 2 for a polygon.

draw(self, p: QPainter)

Draws the geometry using the specified QPainter.

Parameters:p – destination QPainter
dropMValue(self) → bool

Drops any measure values which exist in the geometry.

Returns:true if m-values were present and have been removed

See also

addMValue()

See also

dropZValue()

New in version 2.14.

dropZValue(self) → bool

Drops any z-dimensions which exist in the geometry.

Returns:true if Z values were present and have been removed

See also

addZValue()

See also

dropMValue()

New in version 2.14.

fromWkb(self, wkb: QgsConstWkbPtr) → bool

Sets the geometry from a WKB string. After successful read the wkb argument will be at the position where the reading has stopped.

See also

fromWkt()

fromWkt(self, wkt: str) → bool

Sets the geometry from a WKT string.

See also

fromWkb()

geometryType(self) → str

Returns a unique string representing the geometry type.

See also

wkbType()

See also

wktTypeStr()

hasChildGeometries(self) → bool

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

Note

used for vertex_iterator implementation

New in version 3.0.

hasCurvedSegments(self) → bool

Returns true if the geometry contains curved segments

insertVertex(self, position: QgsVertexId, vertex: QgsPoint) → bool

Inserts a vertex into the geometry

Parameters:
  • position – vertex id for position of inserted vertex
  • vertex – vertex to insert
Returns:

true if insert was successful

See also

moveVertex()

See also

deleteVertex()

is3D(self) → bool

Returns true if the geometry is 3D and contains a z-value.

See also

isMeasure()

isEmpty(self) → bool

Returns true if the geometry is empty

isMeasure(self) → bool

Returns true if the geometry contains m values.

See also

is3D()

length(self) → float

Returns the length of the geometry.

See also

area()

See also

perimeter()

moveVertex(self, position: QgsVertexId, newPos: QgsPoint) → bool

Moves a vertex within the geometry

Parameters:
  • position – vertex id for vertex to move
  • newPos – new position of vertex
Returns:

true if move was successful

See also

insertVertex()

See also

deleteVertex()

nCoordinates(self) → int

Returns the number of nodes contained in the geometry

nextVertex(self, id: QgsVertexId) → Tuple[bool, QgsPoint]

Returns next vertex id and coordinates

Parameters:
  • id – initial value should be the starting vertex id. The next vertex id will be stored in this variable if found.
  • vertex – container for found node
Returns:

false if at end

partCount(self) → int

Returns count of parts contained in the geometry.

See also

vertexCount()

See also

ringCount()

perimeter(self) → float

Returns the perimeter of the geometry.

See also

area()

See also

length()

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

Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerate geometry.

The epsilon parameter specifies the tolerance for coordinates when determining that vertices are identical.

By default, z values are not considered when detecting duplicate nodes. E.g. two nodes with the same x and y coordinate but different z values will still be considered duplicate and one will be removed. If useZValues is true, then the z values are also tested and nodes with the same x and y but different z will be maintained.

Note that duplicate nodes are not tested between different parts of a multipart geometry. E.g. a multipoint geometry with overlapping points will not be changed by this method.

The function will return true if nodes were removed, or false if no duplicate nodes were found.

New in version 3.0.

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

Returns the number of rings of which this geometry is built.

segmentLength(self, startVertex: QgsVertexId) → float

Returns the length of the segment of the geometry which begins at startVertex.

New in version 3.0.

segmentize(self, tolerance: float = M_PI/180, toleranceType: QgsAbstractGeometry.SegmentationToleranceType = QgsAbstractGeometry.MaximumAngle) → QgsAbstractGeometry

Returns a version of the geometry without curves. Caller takes ownership of the returned geometry.

Parameters:
  • tolerance – segmentation tolerance
  • toleranceType – maximum segmentation angle or maximum difference between approximation and curve
setZMTypeFromSubGeometry(self, subggeom: QgsAbstractGeometry, baseGeomType: QgsWkbTypes.Type)

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

snappedToGrid(self, hSpacing: float, vSpacing: float, dSpacing: float = 0, mSpacing: float = 0) → QgsAbstractGeometry

Makes a new geometry with all the points or vertices snapped to the closest point of the grid. Ownership is transferred to the caller.

If the gridified geometry could not be calculated a None will be returned. It may generate an invalid geometry (in some corner cases). It can also be thought as rounding the edges and it may be useful for removing errors. Example:

In this case we use a 2D grid of 1x1 to gridify. In this case, it can be thought like rounding the x and y of all the points/vertices to full units (remove all decimals).

Parameters:
  • hSpacing – Horizontal spacing of the grid (x axis). 0 to disable.
  • vSpacing – Vertical spacing of the grid (y axis). 0 to disable.
  • dSpacing – Depth spacing of the grid (z axis). 0 (default) to disable.
  • mSpacing – Custom dimension spacing of the grid (m axis). 0 (default) to disable.

New in version 3.0.

staticMetaObject = <PyQt5.QtCore.QMetaObject object>
swapXy(self)

Swaps the x and y coordinates from the geometry. This can be used to repair geometries which have accidentally had their latitude and longitude coordinates reversed.

New in version 3.2.

toCurveType(self) → QgsAbstractGeometry

Returns the geometry converted to the more generic curve type. E.g. QgsLineString -> QgsCompoundCurve, QgsPolygon -> QgsCurvePolygon, QgsMultiLineString -> QgsMultiCurve, QgsMultiPolygon -> QgsMultiSurface

Returns:the converted geometry. Caller takes ownership
transform(self, ct: QgsCoordinateTransform, d: QgsCoordinateTransform.TransformDirection = QgsCoordinateTransform.ForwardTransform, transformZ: bool = False)

Transforms the geometry using a coordinate transform

Parameters:
  • ct – coordinate transform
  • d – transformation direction
  • transformZ – set to true to also transform z coordinates. This requires that the z coordinates in the geometry represent height relative to the vertical datum of the source CRS (generally ellipsoidal heights) and are expressed in its vertical units (generally meters). If false, then z coordinates will not be changed by the transform.

transform(self, t: QTransform, zTranslate: float = 0, zScale: float = 1, mTranslate: float = 0, mScale: float = 1) Transforms the x and y components of the geometry using a QTransform object t.

Optionally, the geometry’s z values can be scaled via zScale and translated via zTranslate. Similarly, m-values can be scaled via mScale and translated via mTranslate.

vertexAngle(self, vertex: QgsVertexId) → float

Returns approximate angle at a vertex. This is usually the average angle between adjacent segments, and can be pictured as the orientation of a line following the curvature of the geometry at the specified vertex.

Parameters:vertex – the vertex id
Returns:rotation in radians, clockwise from north
vertexAt(self, id: QgsVertexId) → QgsPoint

Returns the point corresponding to a specified vertex id

vertexCount(self, part: int = 0, ring: int = 0) → int

Returns the number of vertices of which this geometry is built.

vertexNumberFromVertexId(self, id: QgsVertexId) → int

Returns the vertex number corresponding to a vertex id.

The vertex numbers start at 0, so a return value of 0 corresponds to the first vertex.

Returns -1 if a corresponding vertex could not be found.

New in version 3.0.

vertices(self) → QgsVertexIterator

Returns Java-style iterator for traversal of vertices of the geometry

New in version 3.0.

wkbType(self) → QgsWkbTypes.Type

Returns the WKB type of the geometry.

See also

geometryType()

See also

wktTypeStr()

wktTypeStr(self) → str

Returns the WKT type string of the geometry.

See also

geometryType()

See also

wkbType()