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: Enums
Methods
Adds a measure to the geometry, initialized to a preset value.
Adds a z-dimension to the geometry, initialized to a preset value.
Returns the vertices adjacent to a specified
vertex
within a geometry.Returns the area of the geometry.
Returns a GML2 representation of the geometry.
Returns a GML3 representation of the geometry.
Returns a GeoJSON representation of the geometry.
Returns a WKB representation of the geometry.
Returns a WKT representation of the geometry.
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the geometry).
Returns the minimal bounding box for the geometry
Default calculator for the minimal bounding box for the geometry.
Returns the centroid of the geometry
Returns number of child geometries (for geometries with child geometries) or child points (for geometries without child geometries - i.e.
Returns pointer to child geometry (for geometries with child geometries - i.e.
Returns point at index (for geometries without child geometries - i.e.
Clears the geometry, ie reset it to a null geometry
Clears any cached parameters associated with the geometry, e.g., bounding boxes
Clones the geometry by performing a deep copy
Searches for the closest segment of the geometry to a given point.
Converts the geometry to a specified type.
Retrieves the sequence of geometries, rings and nodes.
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
Deletes a vertex within the geometry
Returns the inherent dimension of the geometry.
Draws the geometry using the specified QPainter.
Drops any measure values which exist in the geometry.
Drops any z-dimensions which exist in the geometry.
Sets the geometry from a WKB string.
Sets the geometry from a WKT string.
Returns a unique string representing the geometry type.
Returns whether the geometry has any child geometries (false for point / curve, true otherwise)
Returns true if the geometry contains curved segments
Inserts a vertex into the geometry
Returns true if the geometry is 3D and contains a z-value.
Returns true if the geometry is empty
Returns true if the geometry contains m values.
Returns the length of the geometry.
Moves a vertex within the geometry
Returns the number of nodes contained in the geometry
Returns next vertex id and coordinates
Returns count of parts contained in the geometry.
Returns Java-style iterator for traversal of parts of the geometry.
Returns the perimeter of the geometry.
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerate geometry.
Returns the number of rings of which this geometry is built.
Returns the length of the segment of the geometry which begins at
startVertex
.Returns a version of the geometry without curves.
Updates the geometry type based on whether sub geometries contain z or m values.
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
Swaps the x and y coordinates from the geometry.
Returns the geometry converted to the more generic curve type.
Transforms the geometry using a coordinate transform
Returns approximate angle at a vertex.
Returns the point corresponding to a specified vertex id
Returns the number of vertices of which this geometry is built.
Returns the vertex number corresponding to a vertex
id
.Returns a read-only, Java-style iterator for traversal of vertices of all the geometry, including all geometry parts and rings.
Returns the WKB type of the geometry.
Returns the WKT type string of the geometry.
Signals
Attributes
-
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
See also
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
See also
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
See also
-
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
See also
See also
See also
-
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
See also
See also
See also
-
asJson
(self, precision: int = 17) → str¶ Returns a GeoJSON representation of the geometry.
- Parameters
precision – number of decimal places for coordinates
See also
See also
See also
See also
-
asWkb
(self) → QByteArray¶ Returns a WKB representation of the geometry.
See also
See also
See also
See also
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
See also
See also
See also
-
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
epsilon – epsilon for segment snapping
- Returns
squared distance to closest segment or negative value on error
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)
-
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
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
See also
-
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
See also
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
See also
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
-
geometryType
(self) → str¶ Returns a unique string representing the geometry type.
See also
See also
-
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
See also
-
is3D
(self) → bool¶ Returns true if the geometry is 3D and contains a z-value.
See also
-
isEmpty
(self) → bool¶ Returns true if the geometry is empty
-
length
(self) → float¶ Returns the length of the geometry.
See also
See also
-
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
See also
-
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.
- Returns
false if at end
vertex: container for found node
-
partCount
(self) → int¶ Returns count of parts contained in the geometry.
See also
See also
-
parts
(self) → QgsGeometryPartIterator¶ Returns Java-style iterator for traversal of parts of the geometry. This iterator can safely be used to modify parts of the geometry.
Example:
# print the WKT representation of each part in a multi-point geometry geometry = QgsMultiPoint.fromWkt( 'MultiPoint( 0 0, 1 1, 2 2)' ) for part in geometry.parts(): print(part.asWkt()) # single part geometries only have one part - this loop will iterate once only geometry = QgsLineString.fromWkt( 'LineString( 0 0, 10 10 )' ) for part in geometry.parts(): print(part.asWkt()) # parts can be modified during the iteration geometry = QgsMultiPoint.fromWkt( 'MultiPoint( 0 0, 1 1, 2 2)' ) for part in geometry.parts(): part.transform(ct) # part iteration can also be combined with vertex iteration geometry = QgsMultiPolygon.fromWkt( 'MultiPolygon((( 0 0, 0 10, 10 10, 10 0, 0 0 ),( 5 5, 5 6, 6 6, 6 5, 5 5)),((20 2, 22 2, 22 4, 20 4, 20 2)))' ) for part in geometry.parts(): for v in part.vertices(): print(v.x(), v.y())
See also
New in version 3.6.
-
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 viazTranslate
. Similarly, m-values can be scaled viamScale
and translated viamTranslate
.
-
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 a read-only, Java-style iterator for traversal of vertices of all the geometry, including all geometry parts and rings.
Warning
The iterator returns a copy of individual vertices, and accordingly geometries cannot be modified using the iterator. See transformVertices() for a safe method to modify vertices “in-place”.
Example:
# print the x and y coordinate for each vertex in a LineString geometry = QgsLineString.fromWkt( 'LineString( 0 0, 1 1, 2 2)' ) for v in geometry.vertices(): print(v.x(), v.y()) # vertex iteration includes all parts and rings geometry = QgsMultiPolygon.fromWkt( 'MultiPolygon((( 0 0, 0 10, 10 10, 10 0, 0 0 ),( 5 5, 5 6, 6 6, 6 5, 5 5)),((20 2, 22 2, 22 4, 20 4, 20 2)))' ) for v in geometry.vertices(): print(v.x(), v.y())
See also
New in version 3.0.
-
wkbType
(self) → QgsWkbTypes.Type¶ Returns the WKB type of the geometry.
See also
See also
-
wktTypeStr
(self) → str¶ Returns the WKT type string of the geometry.
See also
See also
-
class